Skip to content

Commit 313ab58

Browse files
added directory and files function
1 parent ec1d0b4 commit 313ab58

5 files changed

Lines changed: 97 additions & 29 deletions

File tree

Database_SQL_Files/database_schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ CREATE TABLE releases (
7070
-- Making this only 30 characters limit, it shouldn't be more than that.
7171
minimum_zig_version VARCHAR(30) NOT NULL,
7272
readme_url TEXT NOT NULL,
73+
directory_files TEXT NOT NULL DEFAULT '',
7374
FOREIGN KEY (repo_id) REFERENCES repos (id) ON DELETE CASCADE,
7475
UNIQUE (repo_id, version)
7576
);

src/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ pub mod limits {
3131
pub const RELEASE_MIN_ZIG_VERSION_MAX_LEN: usize = 30;
3232
pub const RELEASE_DEPENDENCY_FIELD_MAX_LEN: usize = 260;
3333
pub const INDEX_SECTION_NAME_MAX_LEN: usize = 10;
34+
pub const RELEASE_DIRECTORY_FILES_MAX_LEN: usize = 5000;
3435
}

src/github/cron_update_helper.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,48 @@ struct NeedsUpdateRow {
1212
type_of_repo: String,
1313
}
1414

15-
async fn fetch_root_folder_directory_files(
15+
pub async fn fetch_root_folder_directory_files(
1616
client: &reqwest::Client,
1717
user_name: String,
1818
repo_name: String,
19-
) -> String {
20-
// "https://api.github.com/repos/zigistry/zigistry/contents"
19+
branch_or_tag: String,
20+
) -> Result<String, Box<dyn Error + Send + Sync>> {
21+
let url = format!(
22+
"https://api.github.com/repos/{user_name}/{repo_name}/contents?ref={branch_or_tag}"
23+
);
2124
let response = client
22-
.get(format!(
23-
"https://api.github.com/repos/{user_name}/{repo_name}/contents"
24-
))
25+
.get(&url)
2526
.header("User-Agent", "zigistry")
2627
.header("Authorization", &*GITHUB_KEY)
2728
.send()
28-
.await
29-
.unwrap();
30-
let response_json: Vec<serde_json::Value> = response.json().await.unwrap();
29+
.await?;
30+
if !response.status().is_success() {
31+
return Err(format!("GitHub API returned {}", response.status()).into());
32+
}
33+
let response_json: Vec<serde_json::Value> = response.json().await?;
3134
let mut directories = Vec::new();
3235
let mut files = Vec::new();
3336

3437
for thing in response_json {
35-
let name = thing["name"].as_str().unwrap();
36-
let kind = thing["type"].as_str().unwrap();
38+
let name = match thing["name"].as_str() {
39+
Some(name) => name.to_string(),
40+
None => continue,
41+
};
42+
let kind = match thing["type"].as_str() {
43+
Some(kind) => kind,
44+
None => continue,
45+
};
3746

3847
match kind {
39-
"dir" => directories.push(name.to_string()),
40-
"file" => files.push(name.to_string()),
48+
"dir" => directories.push(name),
49+
"file" => files.push(name),
4150
_ => {}
4251
}
4352
}
4453
let dirs_string = directories.join("\n");
4554
let files_string = files.join("\n");
4655
let join_both_strings = dirs_string + "\n\n" + &files_string;
47-
join_both_strings
56+
Ok(join_both_strings)
4857
}
4958

5059
fn parse_github_repo_id(repo_id: &str) -> Option<(String, String)> {
@@ -196,3 +205,17 @@ pub async fn run_cron_update_once(pool: Arc<Connection>) -> Result<(), Box<dyn E
196205

197206
Ok(())
198207
}
208+
209+
#[tokio::test]
210+
async fn test_fetch_root_folder_directory_files() {
211+
let client = reqwest::Client::new();
212+
let result = fetch_root_folder_directory_files(
213+
&client,
214+
"zigistry".to_string(),
215+
"zigistry".to_string(),
216+
"main".to_string(),
217+
)
218+
.await
219+
.unwrap();
220+
println!("directory_files output:\n{result}");
221+
}

src/github/github_data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct ReleaseData {
99
pub published_at: String,
1010
pub minimum_zig_version: String,
1111
pub readme_url: String,
12+
pub directory_files: String,
1213
pub dependencies: Vec<Dependency>,
1314
}
1415

@@ -22,5 +23,6 @@ pub struct RepoData {
2223
pub readme_content: String,
2324
pub build_zig_zon_version: String,
2425
pub build_zig_zon_dependencies: Vec<Dependency>,
26+
pub default_branch_directory_files: String,
2527
pub releases: Vec<ReleaseData>,
2628
}

src/github/mod.rs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,21 @@ pub async fn get_repo_data(
198198
default_branch_name.as_ref()
199199
};
200200

201-
let (build_zig_zon_data, (readme_url, readme_content)) = tokio::join!(
201+
let (build_zig_zon_data, (readme_url, readme_content), default_branch_directory_files) = tokio::join!(
202202
get_build_zig_zon_data_wrapper(&repository.owner.login, &repository.name, branch, client),
203203
get_readme_url_and_content(
204204
&repository.owner.login,
205205
&repository.name,
206206
branch,
207207
true,
208208
client
209-
)
209+
),
210+
fetch_root_folder_directory_files_wrapper(
211+
&repository.owner.login,
212+
&repository.name,
213+
branch,
214+
client
215+
),
210216
);
211217

212218
let (readme_url, readme_content) = match (readme_url, readme_content) {
@@ -223,20 +229,24 @@ pub async fn get_repo_data(
223229
let release_clone = release.clone();
224230

225231
async move {
226-
let (readme_url, _) =
227-
match get_readme_url_and_content(&owner, &name, &tag, false, client).await {
228-
(Some(url), _) => (url, String::new()),
229-
_ => ("404 unable to find readme.".to_string(), String::new()),
230-
};
231-
232-
let bzz_results = get_build_zig_zon_data_wrapper(&owner, &name, &tag, client).await;
232+
let ((readme_url, _), bzz_results, directory_files) = tokio::join!(
233+
async {
234+
match get_readme_url_and_content(&owner, &name, &tag, false, client).await {
235+
(Some(url), _) => (url, String::new()),
236+
_ => ("404 unable to find readme.".to_string(), String::new()),
237+
}
238+
},
239+
get_build_zig_zon_data_wrapper(&owner, &name, &tag, client),
240+
fetch_root_folder_directory_files_wrapper(&owner, &name, &tag, client),
241+
);
233242

234243
ReleaseData {
235244
tag_name: release_clone.tag_name,
236245
is_prerelease: release_clone.is_prerelease,
237246
published_at: release_clone.published_at,
238247
minimum_zig_version: bzz_results.0,
239248
readme_url,
249+
directory_files,
240250
dependencies: bzz_results.1,
241251
}
242252
}
@@ -261,6 +271,7 @@ pub async fn get_repo_data(
261271
readme_content: readme_processed_content,
262272
build_zig_zon_version: build_zig_zon_data.0,
263273
build_zig_zon_dependencies: build_zig_zon_data.1,
274+
default_branch_directory_files,
264275
releases,
265276
}
266277
}
@@ -275,6 +286,7 @@ pub async fn persist_repo_data(transaction: &Transaction, data: RepoData) {
275286
readme_content,
276287
build_zig_zon_version,
277288
build_zig_zon_dependencies,
289+
default_branch_directory_files,
278290
releases,
279291
} = data;
280292

@@ -439,13 +451,14 @@ pub async fn persist_repo_data(transaction: &Transaction, data: RepoData) {
439451
.query(
440452
r#"
441453
INSERT INTO releases
442-
(repo_id, version, is_prerelease, published_at, minimum_zig_version, readme_url)
443-
VALUES(?, ?, ?, ?, ?, ?)
454+
(repo_id, version, is_prerelease, published_at, minimum_zig_version, readme_url, directory_files)
455+
VALUES(?, ?, ?, ?, ?, ?, ?)
444456
ON CONFLICT(repo_id, version) DO UPDATE SET
445457
is_prerelease = excluded.is_prerelease,
446458
published_at = excluded.published_at,
447459
minimum_zig_version = excluded.minimum_zig_version,
448-
readme_url = excluded.readme_url
460+
readme_url = excluded.readme_url,
461+
directory_files = excluded.directory_files
449462
RETURNING id
450463
"#,
451464
params![
@@ -461,6 +474,10 @@ pub async fn persist_repo_data(transaction: &Transaction, data: RepoData) {
461474
limits::RELEASE_MIN_ZIG_VERSION_MAX_LEN
462475
),
463476
readme_url.clone(),
477+
truncate_to_char_limit(
478+
&default_branch_directory_files,
479+
limits::RELEASE_DIRECTORY_FILES_MAX_LEN
480+
),
464481
],
465482
)
466483
.await
@@ -519,13 +536,14 @@ pub async fn persist_repo_data(transaction: &Transaction, data: RepoData) {
519536
.query(
520537
r#"
521538
INSERT INTO releases
522-
(repo_id, version, is_prerelease, published_at, minimum_zig_version, readme_url)
523-
VALUES(?, ?, ?, ?, ?, ?)
539+
(repo_id, version, is_prerelease, published_at, minimum_zig_version, readme_url, directory_files)
540+
VALUES(?, ?, ?, ?, ?, ?, ?)
524541
ON CONFLICT(repo_id, version) DO UPDATE SET
525542
is_prerelease = excluded.is_prerelease,
526543
published_at = excluded.published_at,
527544
minimum_zig_version = excluded.minimum_zig_version,
528-
readme_url = excluded.readme_url
545+
readme_url = excluded.readme_url,
546+
directory_files = excluded.directory_files
529547
RETURNING id
530548
"#,
531549
params![
@@ -538,6 +556,10 @@ pub async fn persist_repo_data(transaction: &Transaction, data: RepoData) {
538556
limits::RELEASE_MIN_ZIG_VERSION_MAX_LEN
539557
),
540558
release_data.readme_url,
559+
truncate_to_char_limit(
560+
&release_data.directory_files,
561+
limits::RELEASE_DIRECTORY_FILES_MAX_LEN
562+
),
541563
],
542564
)
543565
.await
@@ -1074,6 +1096,25 @@ async fn get_build_zig_zon_data_wrapper(
10741096
}
10751097
}
10761098

1099+
async fn fetch_root_folder_directory_files_wrapper(
1100+
owner_name: &str,
1101+
repo_name: &str,
1102+
branch_or_tag: &str,
1103+
client: &reqwest::Client,
1104+
) -> String {
1105+
match cron_update_helper::fetch_root_folder_directory_files(
1106+
client,
1107+
owner_name.to_string(),
1108+
repo_name.to_string(),
1109+
branch_or_tag.to_string(),
1110+
)
1111+
.await
1112+
{
1113+
Ok(files) => files,
1114+
Err(_) => String::new(),
1115+
}
1116+
}
1117+
10771118
/// I have added this new client, which is much more optimized
10781119
/// becuase, initially, I wasn't adding any timeouts.
10791120
fn create_optimized_client() -> reqwest::Client {

0 commit comments

Comments
 (0)