Skip to content

Commit c8dc91c

Browse files
committed
fix: check ampup version for self update
1 parent 6d60bbc commit c8dc91c

3 files changed

Lines changed: 120 additions & 9 deletions

File tree

ampup/src/github.rs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
44
use futures::StreamExt;
55
use serde::Deserialize;
66

7-
use crate::rate_limiter::GitHubRateLimiter;
7+
use crate::{DEFAULT_REPO, DEFAULT_SELF_REPO, rate_limiter::GitHubRateLimiter};
88

99
const AMPUP_API_URL: &str = "https://ampup.sh/api";
1010
const GITHUB_API_URL: &str = "https://api.github.com";
@@ -236,12 +236,7 @@ impl GitHubClient {
236236
.build()
237237
.context("Failed to create request client")?;
238238

239-
// Use custom endpoints for edgeandnode/amp and otherwise leverages the github api
240-
let api = if repo == "edgeandnode/amp" && github_token.is_none() {
241-
AMPUP_API_URL.to_string()
242-
} else {
243-
format!("{}/repos/{}/releases", GITHUB_API_URL, repo)
244-
};
239+
let api = release_api_base(&repo);
245240

246241
let rate_limiter = Arc::new(GitHubRateLimiter::new(github_token.is_some()));
247242

@@ -572,3 +567,81 @@ impl GitHubClient {
572567
Ok(buffer)
573568
}
574569
}
570+
571+
fn release_api_base(repo: &str) -> String {
572+
match repo_slug(repo) {
573+
Some(slug) => format!("{}/{}", AMPUP_API_URL, slug),
574+
None => format!("{}/repos/{}/releases", GITHUB_API_URL, repo),
575+
}
576+
}
577+
578+
fn repo_slug(repo: &str) -> Option<&'static str> {
579+
match repo {
580+
DEFAULT_REPO => Some("amp"),
581+
DEFAULT_SELF_REPO => Some("ampup"),
582+
_ => None,
583+
}
584+
}
585+
586+
#[cfg(test)]
587+
mod tests {
588+
use anyhow::Result;
589+
590+
use super::*;
591+
592+
#[test]
593+
fn release_api_base_with_amp_repo_uses_ampup_api_slug() {
594+
//* When
595+
let api_base = release_api_base(DEFAULT_REPO);
596+
597+
//* Then
598+
assert_eq!(
599+
api_base, "https://ampup.sh/api/amp",
600+
"amp releases should use the ampup API amp slug"
601+
);
602+
}
603+
604+
#[test]
605+
fn release_api_base_with_ampup_repo_uses_ampup_api_slug() {
606+
//* When
607+
let api_base = release_api_base(DEFAULT_SELF_REPO);
608+
609+
//* Then
610+
assert_eq!(
611+
api_base, "https://ampup.sh/api/ampup",
612+
"ampup releases should use the ampup API ampup slug"
613+
);
614+
}
615+
616+
#[test]
617+
fn new_with_ampup_repo_and_github_token_uses_ampup_api() -> Result<()> {
618+
//* Given
619+
let github_token = Some("test-token".to_string());
620+
621+
//* When
622+
let client = GitHubClient::new(DEFAULT_SELF_REPO.to_string(), github_token)?;
623+
624+
//* Then
625+
assert_eq!(
626+
client.api, "https://ampup.sh/api/ampup",
627+
"supported repos should use ampup.sh API even when a GitHub token is configured"
628+
);
629+
630+
Ok(())
631+
}
632+
633+
#[test]
634+
fn release_api_base_with_other_repo_uses_github_releases_api() {
635+
//* Given
636+
let repo = "some-owner/some-repo";
637+
638+
//* When
639+
let api_base = release_api_base(repo);
640+
641+
//* Then
642+
assert_eq!(
643+
api_base, "https://api.github.com/repos/some-owner/some-repo/releases",
644+
"unsupported repos should keep using the GitHub releases API"
645+
);
646+
}
647+
}

ampup/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub mod ui;
1818
/// Default GitHub repository for amp releases
1919
pub const DEFAULT_REPO: &str = "edgeandnode/amp";
2020

21+
/// Default GitHub repository for ampup releases
22+
pub const DEFAULT_SELF_REPO: &str = "edgeandnode/ampup";
23+
2124
/// Default number of concurrent downloads
2225
pub const DEFAULT_DOWNLOAD_JOBS: usize = 4;
2326

ampup/src/main.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ampup::{DEFAULT_DOWNLOAD_JOBS, DEFAULT_REPO, commands};
1+
use ampup::{DEFAULT_DOWNLOAD_JOBS, DEFAULT_REPO, DEFAULT_SELF_REPO, commands};
22
use console::style;
33

44
/// The ampd installer and version manager
@@ -165,7 +165,7 @@ enum SelfCommands {
165165
/// Update ampup itself to the latest version
166166
Update {
167167
/// GitHub repository in format "owner/repo"
168-
#[arg(long, default_value_t = DEFAULT_REPO.to_string())]
168+
#[arg(long, default_value_t = DEFAULT_SELF_REPO.to_string())]
169169
repo: String,
170170

171171
/// GitHub token for private repository access (defaults to $GITHUB_TOKEN)
@@ -283,3 +283,38 @@ async fn run() -> anyhow::Result<()> {
283283

284284
Ok(())
285285
}
286+
287+
#[cfg(test)]
288+
mod tests {
289+
use clap::Parser;
290+
291+
use super::*;
292+
293+
#[test]
294+
fn try_parse_from_with_self_update_command_uses_ampup_repo_default() {
295+
//* When
296+
let cli = Cli::try_parse_from(["ampup", "self", "update"]);
297+
298+
//* Then
299+
assert!(
300+
cli.is_ok(),
301+
"cli parsing should succeed for `ampup self update`: {:?}",
302+
cli
303+
);
304+
305+
match cli
306+
.expect("cli parsing should return parsed arguments")
307+
.command
308+
{
309+
Some(Commands::SelfCmd {
310+
command: SelfCommands::Update { repo, .. },
311+
}) => {
312+
assert_eq!(
313+
repo, DEFAULT_SELF_REPO,
314+
"self update should default to the ampup repository"
315+
);
316+
}
317+
other => panic!("expected `self update` command to parse, got {:?}", other),
318+
}
319+
}
320+
}

0 commit comments

Comments
 (0)