From 0d4392a8d50e2a44dee9a74cb82d5e1fabfc98d5 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Sun, 30 Apr 2023 20:14:33 +0200 Subject: fix: Fetch multiple pages from GitHub PR API (#298) * fix: Fetch multiple pages from GitHub PR API Previously we only fetched the first page which caused us to miss older pull requests. * feat: Only check as many pages as we need Instead of checking a static x amount of pages in the API we only wanna fetch as many pages as we actually need. --- src-tauri/src/github/pull_requests.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'src-tauri') diff --git a/src-tauri/src/github/pull_requests.rs b/src-tauri/src/github/pull_requests.rs index 96ac623f..a56fb647 100644 --- a/src-tauri/src/github/pull_requests.rs +++ b/src-tauri/src/github/pull_requests.rs @@ -66,17 +66,34 @@ pub enum PullRequestType { /// Parse pull requests from specified URL pub async fn get_pull_requests(url: String) -> Result, String> { - let json_response = match fetch_github_releases_api(&url).await { - Ok(result) => result, - Err(err) => return Err(err), - }; + let mut all_pull_requests: Vec = vec![]; - let pulls_response: Vec = match serde_json::from_str(&json_response) { - Ok(res) => res, - Err(err) => return Err(err.to_string()), - }; + let mut i = 1; // pagination on GitHub starts with `1`. + loop { + let paginated_url = format!("{}?page={}", url, i); + + let json_response = match fetch_github_releases_api(&paginated_url).await { + Ok(result) => result, + Err(err) => return Err(err), + }; + + let pulls_response: Vec = + match serde_json::from_str(&json_response) { + Ok(res) => res, + Err(err) => return Err(err.to_string()), + }; + + // Check if we still got a result + if pulls_response.is_empty() { + // Empty result means we went through all pages with content + break; + } + + all_pull_requests.extend(pulls_response); + i += 1; + } - Ok(pulls_response) + Ok(all_pull_requests) } /// Gets either launcher or mods PRs -- cgit v1.2.3