From 7380c390f8fd536152feb4ff0211bf45c28174fc Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Wed, 14 Feb 2024 01:17:36 +0100 Subject: refactor: Use dedicated library for getting PR list (#807) Use the `octocrab` library for interacting with GitHub for getting the list of pull requests instead of writing own GitHub API library. This is done to reduce code size in the longterm even if the current change will temporarily increase it. --- src-tauri/src/constants.rs | 12 +++--- src-tauri/src/github/pull_requests.rs | 78 ++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src-tauri/src/constants.rs b/src-tauri/src/constants.rs index bd394ed0..47eeef19 100644 --- a/src-tauri/src/constants.rs +++ b/src-tauri/src/constants.rs @@ -31,12 +31,6 @@ pub const SECTION_ORDER: [&str; 11] = [ "feat", "fix", "docs", "style", "refactor", "build", "test", "i18n", "ci", "chore", "other", ]; -/// GitHub API endpoints for launcher/mods PRs -pub const PULLS_API_ENDPOINT_LAUNCHER: &str = - "https://api.github.com/repos/R2Northstar/NorthstarLauncher/pulls"; -pub const PULLS_API_ENDPOINT_MODS: &str = - "https://api.github.com/repos/R2Northstar/NorthstarMods/pulls"; - /// Statistics (players and servers counts) refresh delay pub const REFRESH_DELAY: Duration = Duration::from_secs(5 * 60); @@ -46,6 +40,12 @@ pub const FLIGHTCORE_REPO_NAME: &str = "R2NorthstarTools/FlightCore"; /// Northstar release repo name and org name on GitHub pub const NORTHSTAR_RELEASE_REPO_NAME: &str = "R2Northstar/Northstar"; +/// NorthstarLauncher repo name on GitHub +pub const NORTHSTAR_LAUNCHER_REPO_NAME: &str = "NorthstarLauncher"; + +/// NorthstarMods repo name on GitHub +pub const NORTHSTAR_MODS_REPO_NAME: &str = "NorthstarMods"; + /// URL to launcher commits API URL pub const NS_LAUNCHER_COMMITS_API_URL: &str = "https://api.github.com/repos/R2Northstar/NorthstarLauncher/commits"; 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, String> { - let mut all_pull_requests: Vec = vec![]; +pub async fn get_pull_requests( + repo: PullRequestType, +) -> Result, 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 = 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 = - 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 Result, 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> { -- cgit v1.2.3