@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
44use futures:: StreamExt ;
55use serde:: Deserialize ;
66
7- use crate :: rate_limiter:: GitHubRateLimiter ;
7+ use crate :: { DEFAULT_REPO , DEFAULT_SELF_REPO , rate_limiter:: GitHubRateLimiter } ;
88
99const AMPUP_API_URL : & str = "https://ampup.sh/api" ;
1010const 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+ }
0 commit comments