From f9e7d774aa0351fb93d13215689e38a6962b3272 Mon Sep 17 00:00:00 2001 From: davidvader Date: Tue, 22 Oct 2024 11:01:27 -0500 Subject: [PATCH] fix: remove install_id when app is deleted --- scm/github/installation.go | 12 +++++++++++- scm/github/webhook.go | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/scm/github/installation.go b/scm/github/installation.go index 839e8dfff..3fe0f4185 100644 --- a/scm/github/installation.go +++ b/scm/github/installation.go @@ -22,6 +22,9 @@ func (c *client) ProcessInstallation(ctx context.Context, request *http.Request, errs := []error{} + // if action is "deleted" then the RepositoriesAdded field will indicate the repositories that + // need to have install_id set to zero + // set install_id for repos added to the installation for _, repo := range webhook.Installation.RepositoriesAdded { r, err := db.GetRepoForOrg(ctx, webhook.Installation.Org, repo) @@ -34,7 +37,14 @@ func (c *client) ProcessInstallation(ctx context.Context, request *http.Request, continue } - err = updateRepoInstallationID(ctx, webhook, r, db, webhook.Installation.ID) + installID := webhook.Installation.ID + + // clear install_id if the installation is deleted + if webhook.Installation.Action == "deleted" { + installID = 0 + } + + err = updateRepoInstallationID(ctx, webhook, r, db, installID) if err != nil { errs = append(errs, err) } diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 9d3e8323d..dea62ac52 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -543,6 +543,7 @@ func (c *client) processRepositoryEvent(h *api.Hook, payload *github.RepositoryE }, nil } +// processInstallationEvent is a helper function to process the installation event. func (c *client) processInstallationEvent(ctx context.Context, h *api.Hook, payload *github.InstallationEvent) (*internal.Webhook, error) { h.SetEvent(constants.EventRepository) h.SetEventAction(payload.GetAction()) @@ -563,6 +564,7 @@ func (c *client) processInstallationEvent(ctx context.Context, h *api.Hook, payl }, nil } +// processInstallationRepositoriesEvent is a helper function to process the installation repositories event. func (c *client) processInstallationRepositoriesEvent(ctx context.Context, h *api.Hook, payload *github.InstallationRepositoriesEvent) (*internal.Webhook, error) { install := new(internal.Installation)