Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions ampup/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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"
);
}
}
3 changes: 3 additions & 0 deletions ampup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions ampup/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading