fix: wrong installation id returned from cache (cherry-pick #26969 for 3.3) (#27027)

Signed-off-by: Zach Aller <zach_aller@intuit.com>
Co-authored-by: Zach Aller <zachaller@users.noreply.github.com>
This commit is contained in:
argo-cd-cherry-pick-bot[bot] 2026-03-27 09:24:06 -04:00 committed by GitHub
parent 4a823fecf0
commit c5d7748f2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View file

@ -659,10 +659,11 @@ func DiscoverGitHubAppInstallationID(ctx context.Context, appId int64, privateKe
opts.Page = resp.NextPage
}
// Cache all installation IDs
// Cache each installation under its account's key so multiple orgs do not overwrite each other.
for _, installation := range allInstallations {
if installation.Account != nil && installation.Account.Login != nil && installation.ID != nil {
githubInstallationIdCache.Set(cacheKey, *installation.ID, gocache.DefaultExpiration)
instKey := fmt.Sprintf("%s:%s:%d", strings.ToLower(*installation.Account.Login), domain, appId)
githubInstallationIdCache.Set(instKey, *installation.ID, gocache.DefaultExpiration)
}
}

View file

@ -610,6 +610,35 @@ func TestDiscoverGitHubAppInstallationID(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, int64(98765), actualId)
})
t.Run("returns correct installation ID when app is installed on multiple orgs", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/app/installations") {
w.WriteHeader(http.StatusOK)
//nolint:errcheck
json.NewEncoder(w).Encode([]map[string]any{
{"id": 11111, "account": map[string]any{"login": "org-alpha"}},
{"id": 22222, "account": map[string]any{"login": "target-org"}},
{"id": 33333, "account": map[string]any{"login": "org-gamma"}},
})
return
}
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
t.Cleanup(func() {
domain, _ := domainFromBaseURL(server.URL)
for _, org := range []string{"org-alpha", "target-org", "org-gamma"} {
githubInstallationIdCache.Delete(fmt.Sprintf("%s:%s:%d", org, domain, 12345))
}
})
ctx := context.Background()
actualId, err := DiscoverGitHubAppInstallationID(ctx, 12345, fakeGitHubAppPrivateKey, server.URL, "target-org")
require.NoError(t, err)
assert.Equal(t, int64(22222), actualId, "should return the installation ID for the requested org, not the last one in the list")
})
}
func TestExtractOrgFromRepoURL(t *testing.T) {