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
22 changes: 14 additions & 8 deletions internal/cli/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,16 @@ var githubAPIBaseURL = "https://api.github.com"

var githubHTTPClient = &http.Client{Timeout: 30 * time.Second}

// lookupTokenFn resolves a GitHub token for app-ID lookups using the
// standard resolution chain (GH_TOKEN → GITHUB_TOKEN → gh auth token).
// Defaults to resolveToken; overridden in tests.
var lookupTokenFn = resolveToken

// lookupAppID fetches the numeric app ID for a public GitHub App by slug.
// When GH_TOKEN or GITHUB_TOKEN is set in the environment, the request is
// authenticated (5,000 requests/hour). Otherwise it falls back to an
// unauthenticated request (60 requests/hour, shared by source IP).
// When a GitHub token is available (via GH_TOKEN, GITHUB_TOKEN, or
// gh auth token), the request is authenticated (5,000 requests/hour).
// Otherwise it falls back to an unauthenticated request (60 requests/hour,
// shared by source IP).
func lookupAppID(ctx context.Context, slug string) (int, error) {
url := githubAPIBaseURL + "/apps/" + url.PathEscape(slug)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
Expand All @@ -175,10 +181,10 @@ func lookupAppID(ctx context.Context, slug string) (int, error) {

// Authenticate if a token is available, lifting the rate limit from
// 60/hour (unauthenticated, shared by IP) to 5,000/hour.
token := os.Getenv("GH_TOKEN")
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
// Uses the standard token resolution chain: GH_TOKEN, GITHUB_TOKEN,
// then gh auth token. The /apps/{slug} endpoint is public, so a
// resolution error is not fatal — proceed unauthenticated.
token, _ := lookupTokenFn()
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
Expand All @@ -199,7 +205,7 @@ func lookupAppID(ctx context.Context, slug string) (int, error) {
if token != "" {
return 0, fmt.Errorf("GitHub API rate limit exceeded for app %s — try again later", slug)
}
return 0, fmt.Errorf("GitHub API rate limit exceeded for app %s — unauthenticated requests are limited to 60/hour; set GH_TOKEN or GITHUB_TOKEN and try again", slug)
return 0, fmt.Errorf("GitHub API rate limit exceeded for app %s — unauthenticated requests are limited to 60/hour; set GH_TOKEN or GITHUB_TOKEN, or run 'gh auth login', and try again", slug)
}
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("GitHub API returned %d for app %s", resp.StatusCode, slug)
Expand Down
42 changes: 38 additions & 4 deletions internal/cli/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2320,9 +2320,17 @@ func TestMintDeployCmd_NoWarningForCorrectPlatformFlags(t *testing.T) {

// --- lookupAppID tests ---

// withNoToken overrides lookupTokenFn to return no token, simulating an
// environment with no GitHub credentials. Restores the original on cleanup.
func withNoToken(t *testing.T) {
t.Helper()
orig := lookupTokenFn
lookupTokenFn = func() (string, error) { return "", fmt.Errorf("no token") }
t.Cleanup(func() { lookupTokenFn = orig })
}

func TestLookupAppID_Success(t *testing.T) {
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
withNoToken(t)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/apps/fullsend-ai-coder", r.URL.Path)
Expand Down Expand Up @@ -2397,8 +2405,7 @@ func TestLookupAppID_RateLimit(t *testing.T) {
{"TooManyRequests", http.StatusTooManyRequests},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("GH_TOKEN", "")
t.Setenv("GITHUB_TOKEN", "")
withNoToken(t)

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tc.code)
Expand All @@ -2413,6 +2420,8 @@ func TestLookupAppID_RateLimit(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "rate limit")
assert.Contains(t, err.Error(), "set GH_TOKEN or GITHUB_TOKEN")
assert.Contains(t, err.Error(), "gh auth login",
"unauthenticated rate limit error should suggest gh auth login")
})
}
}
Expand Down Expand Up @@ -2498,6 +2507,31 @@ func TestLookupAppID_RateLimitAuthenticated(t *testing.T) {
"authenticated rate limit error should not suggest setting a token")
}

func TestLookupAppID_UsesResolveTokenFallback(t *testing.T) {
// Simulate the gh auth token fallback: both env vars are unset, but
// the token resolver returns a token (as resolveToken would when
// gh auth login has been run).
orig := lookupTokenFn
lookupTokenFn = func() (string, error) { return "ghp_from_gh_auth", nil }
t.Cleanup(func() { lookupTokenFn = orig })

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "Bearer ghp_from_gh_auth", r.Header.Get("Authorization"),
"lookupAppID should authenticate using the token from the resolution chain")
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"id": 88}`)
}))
defer srv.Close()

origBase := githubAPIBaseURL
githubAPIBaseURL = srv.URL
defer func() { githubAPIBaseURL = origBase }()

appID, err := lookupAppID(context.Background(), "test-app")
require.NoError(t, err)
assert.Equal(t, 88, appID)
}

// --- verifyPEMMatchesApp tests ---

func TestVerifyPEMMatchesApp_Success(t *testing.T) {
Expand Down
Loading