diff --git a/ampup/src/github.rs b/ampup/src/github.rs index 7c81f45..97944f9 100644 --- a/ampup/src/github.rs +++ b/ampup/src/github.rs @@ -4,7 +4,7 @@ use anyhow::{Context, Result}; use futures::StreamExt; use serde::Deserialize; -use crate::rate_limiter::GitHubRateLimiter; +use crate::{DEFAULT_REPO, DEFAULT_SELF_REPO, rate_limiter::GitHubRateLimiter}; const AMPUP_API_URL: &str = "https://ampup.sh/api"; const GITHUB_API_URL: &str = "https://api.github.com"; @@ -236,12 +236,7 @@ impl GitHubClient { .build() .context("Failed to create request client")?; - // Use custom endpoints for edgeandnode/amp and otherwise leverages the github api - let api = if repo == "edgeandnode/amp" && github_token.is_none() { - AMPUP_API_URL.to_string() - } else { - format!("{}/repos/{}/releases", GITHUB_API_URL, repo) - }; + let api = release_api_base(&repo); let rate_limiter = Arc::new(GitHubRateLimiter::new(github_token.is_some())); @@ -572,3 +567,81 @@ impl GitHubClient { Ok(buffer) } } + +fn release_api_base(repo: &str) -> String { + match repo_slug(repo) { + Some(slug) => format!("{}/{}", AMPUP_API_URL, slug), + None => format!("{}/repos/{}/releases", GITHUB_API_URL, repo), + } +} + +fn repo_slug(repo: &str) -> Option<&'static str> { + match repo { + DEFAULT_REPO => Some("amp"), + DEFAULT_SELF_REPO => Some("ampup"), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use anyhow::Result; + + use super::*; + + #[test] + fn release_api_base_with_amp_repo_uses_ampup_api_slug() { + //* When + let api_base = release_api_base(DEFAULT_REPO); + + //* Then + assert_eq!( + api_base, "https://ampup.sh/api/amp", + "amp releases should use the ampup API amp slug" + ); + } + + #[test] + fn release_api_base_with_ampup_repo_uses_ampup_api_slug() { + //* When + let api_base = release_api_base(DEFAULT_SELF_REPO); + + //* Then + assert_eq!( + api_base, "https://ampup.sh/api/ampup", + "ampup releases should use the ampup API ampup slug" + ); + } + + #[test] + fn new_with_ampup_repo_and_github_token_uses_ampup_api() -> Result<()> { + //* Given + let github_token = Some("test-token".to_string()); + + //* When + let client = GitHubClient::new(DEFAULT_SELF_REPO.to_string(), github_token)?; + + //* Then + assert_eq!( + client.api, "https://ampup.sh/api/ampup", + "supported repos should use ampup.sh API even when a GitHub token is configured" + ); + + Ok(()) + } + + #[test] + fn release_api_base_with_other_repo_uses_github_releases_api() { + //* Given + let repo = "some-owner/some-repo"; + + //* When + let api_base = release_api_base(repo); + + //* Then + assert_eq!( + api_base, "https://api.github.com/repos/some-owner/some-repo/releases", + "unsupported repos should keep using the GitHub releases API" + ); + } +} diff --git a/ampup/src/lib.rs b/ampup/src/lib.rs index 01c9f7c..fa2c81f 100644 --- a/ampup/src/lib.rs +++ b/ampup/src/lib.rs @@ -18,6 +18,9 @@ pub mod ui; /// Default GitHub repository for amp releases pub const DEFAULT_REPO: &str = "edgeandnode/amp"; +/// Default GitHub repository for ampup releases +pub const DEFAULT_SELF_REPO: &str = "edgeandnode/ampup"; + /// Default number of concurrent downloads pub const DEFAULT_DOWNLOAD_JOBS: usize = 4; diff --git a/ampup/src/main.rs b/ampup/src/main.rs index fe50bba..449cc2a 100644 --- a/ampup/src/main.rs +++ b/ampup/src/main.rs @@ -1,4 +1,4 @@ -use ampup::{DEFAULT_DOWNLOAD_JOBS, DEFAULT_REPO, commands}; +use ampup::{DEFAULT_DOWNLOAD_JOBS, DEFAULT_REPO, DEFAULT_SELF_REPO, commands}; use console::style; /// The ampd installer and version manager @@ -165,7 +165,7 @@ enum SelfCommands { /// Update ampup itself to the latest version Update { /// GitHub repository in format "owner/repo" - #[arg(long, default_value_t = DEFAULT_REPO.to_string())] + #[arg(long, default_value_t = DEFAULT_SELF_REPO.to_string())] repo: String, /// GitHub token for private repository access (defaults to $GITHUB_TOKEN)