diff options
Diffstat (limited to 'src-tauri/src/github/pull_requests.rs')
-rw-r--r-- | src-tauri/src/github/pull_requests.rs | 78 |
1 files changed, 49 insertions, 29 deletions
diff --git a/src-tauri/src/github/pull_requests.rs b/src-tauri/src/github/pull_requests.rs index 27e4f30c..bf7a8fdb 100644 --- a/src-tauri/src/github/pull_requests.rs +++ b/src-tauri/src/github/pull_requests.rs @@ -1,6 +1,4 @@ -use crate::github::release_notes::fetch_github_releases_api; - -use crate::constants::{APP_USER_AGENT, PULLS_API_ENDPOINT_LAUNCHER, PULLS_API_ENDPOINT_MODS}; +use crate::constants::{APP_USER_AGENT, NORTHSTAR_LAUNCHER_REPO_NAME, NORTHSTAR_MODS_REPO_NAME}; use crate::repair_and_verify::check_is_valid_game_path; use crate::GameInstall; use anyhow::anyhow; @@ -66,32 +64,56 @@ pub enum PullRequestType { } /// Parse pull requests from specified URL -pub async fn get_pull_requests(url: String) -> Result<Vec<PullsApiResponseElement>, String> { - let mut all_pull_requests: Vec<PullsApiResponseElement> = vec![]; +pub async fn get_pull_requests( + repo: PullRequestType, +) -> Result<Vec<PullsApiResponseElement>, anyhow::Error> { + let repo = match repo { + PullRequestType::Mods => NORTHSTAR_MODS_REPO_NAME, + PullRequestType::Launcher => NORTHSTAR_LAUNCHER_REPO_NAME, + }; - let mut i = 1; // pagination on GitHub starts with `1`. - loop { - let paginated_url = format!("{}?page={}", url, i); + // Grab list of PRs + let octocrab = octocrab::instance(); + let page = octocrab + .pulls("R2Northstar", repo) + .list() + .state(octocrab::params::State::Open) + .per_page(50) // Only grab 50 PRs + .page(1u32) + .send() + .await?; - let json_response = match fetch_github_releases_api(&paginated_url).await { - Ok(result) => result, - Err(err) => return Err(format!("Failed fetching GitHub API {err}")), + // Iterate over pull request elements and insert into struct + let mut all_pull_requests: Vec<PullsApiResponseElement> = vec![]; + for item in page.items { + let repo = Repo { + full_name: item + .head + .repo + .ok_or(anyhow!("repo not found"))? + .full_name + .ok_or(anyhow!("full_name not found"))?, }; - let pulls_response: Vec<PullsApiResponseElement> = - match serde_json::from_str(&json_response) { - Ok(res) => res, - Err(err) => return Err(err.to_string()), - }; + let head = CommitHead { + sha: item.head.sha, + gh_ref: item.head.ref_field, + repo, + }; - // Check if we still got a result - if pulls_response.is_empty() { - // Empty result means we went through all pages with content - break; - } + // TODO there's probably a way to automatically serialize into the struct but I don't know yet how to + let elem = PullsApiResponseElement { + number: item.number, + title: item.title.ok_or(anyhow!("title not found"))?, + url: item.url, + head, + html_url: item + .html_url + .ok_or(anyhow!("html_url not found"))? + .to_string(), + }; - all_pull_requests.extend(pulls_response); - i += 1; + all_pull_requests.push(elem); } Ok(all_pull_requests) @@ -102,12 +124,10 @@ pub async fn get_pull_requests(url: String) -> Result<Vec<PullsApiResponseElemen pub async fn get_pull_requests_wrapper( install_type: PullRequestType, ) -> Result<Vec<PullsApiResponseElement>, String> { - let api_pr_url = match install_type { - PullRequestType::Mods => PULLS_API_ENDPOINT_MODS, - PullRequestType::Launcher => PULLS_API_ENDPOINT_LAUNCHER, - }; - - get_pull_requests(api_pr_url.to_string()).await + match get_pull_requests(install_type).await { + Ok(res) => Ok(res), + Err(err) => Err(err.to_string()), + } } pub async fn check_github_api(url: &str) -> Result<serde_json::Value, Box<dyn std::error::Error>> { |