Skip to content

Commit

Permalink
Purge installation cache when create target
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Jan 15, 2025
1 parent f1c6eb6 commit e2bdced
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/gh/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,18 @@ func _listAppsInstalledRepo(ctx context.Context, installationID int64) ([]*githu

return repositories, nil
}

// PurgeInstallationCache purges the cache of installations
func PurgeInstallationCache(ctx context.Context) error {
installations, err := listInstallations(ctx)
if err != nil {
return fmt.Errorf("failed to get installations: %w", err)
}

for _, installation := range installations {
responseCache.Delete(getCacheInstalledRepoKey(installation.GetID()))
}

responseCache.Delete(getCacheInstallationsKey())
return nil
}
1 change: 1 addition & 0 deletions pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var (
GHIsInstalledGitHubApp = gh.IsInstalledGitHubApp
GHGenerateGitHubAppsToken = gh.GenerateGitHubAppsToken
GHNewClientApps = gh.NewClientGitHubApps
GHPurgeInstallationCache = gh.PurgeInstallationCache
)

func handleTargetList(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/web/target_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
outputErrorMsg(w, http.StatusBadRequest, err.Error())
return
}
if err := GHPurgeInstallationCache(ctx); err != nil {
logger.Logf(false, "failed to purge installation cache: %+v", err)
outputErrorMsg(w, http.StatusInternalServerError, "failed to purge installation cache")
return
}

installationID, err := GHIsInstalledGitHubApp(ctx, inputTarget.Scope)
if err != nil {
logger.Logf(false, "failed to check installed GitHub App: %+v", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/web/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func setStubFunctions() {
web.GHNewClientApps = func() (*github.Client, error) {
return &github.Client{}, nil
}

web.GHPurgeInstallationCache = func(ctx context.Context) error {
return nil
}
}

func Test_handleTargetCreate(t *testing.T) {
Expand Down

0 comments on commit e2bdced

Please sign in to comment.