Skip to content

Commit

Permalink
delete all repos when app installation is removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Oct 21, 2024
1 parent 701575e commit 6e93403
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions backend/internal/server/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ func (h WebhookHandler) handleWebhook(w http.ResponseWriter, r *http.Request) {
)

if *event.Action == "created" {
// Payload: https://github.com/octokit/webhooks/blob/main/payload-examples/api.github.com/installation/created.payload.json
repos := mapRepos(userID, event.Repositories)
err = h.repoRepo.Upsert(r.Context(), repos)
if err != nil {
Expand All @@ -292,12 +291,20 @@ func (h WebhookHandler) handleWebhook(w http.ResponseWriter, r *http.Request) {
break
}
} else if *event.Action == "deleted" {
logger.Debug("app installation deleted",
slog.Int64("installation.id", *installation.ID),
slog.String("login", login),
)
removedRepositories := event.Repositories

// TODO: Delete all repos for this user
repoIDs := make([]int64, 0, len(removedRepositories))
for _, removedRepository := range removedRepositories {
repoIDs = append(repoIDs, *removedRepository.ID)
}

err = h.repoRepo.Delete(r.Context(), repoIDs)
if err != nil {
logger.Error("error deleting repositories", slog.Any("error", err))
http.Error(w, "error deleting repositories", http.StatusInternalServerError)
break
}
_, _ = w.Write([]byte(fmt.Sprintf("removed %d repositories\n", len(removedRepositories))))
}
case *github.InstallationRepositoriesEvent:
// Payload: https://github.com/octokit/webhooks/blob/main/payload-examples/api.github.com/installation_repositories/added.payload.json
Expand Down

0 comments on commit 6e93403

Please sign in to comment.