From 86624091dbcb6a4a6dd7936c06105b851886a38e Mon Sep 17 00:00:00 2001 From: "Claire.Nicholas" Date: Mon, 15 Jan 2024 15:15:10 -0600 Subject: [PATCH] fix: starting stuff --- api/build/list_org.go | 3 +- api/build/list_repo.go | 3 +- api/repo/create.go | 6 +++ database/repo/repo_test.go | 4 ++ database/secret/secret_test.go | 4 ++ go.mod | 2 +- go.sum | 2 + scm/github/webhook.go | 80 ++++++++++++++++++++++++++++++++++ 8 files changed, 101 insertions(+), 3 deletions(-) diff --git a/api/build/list_org.go b/api/build/list_org.go index aa495087e..673879bc4 100644 --- a/api/build/list_org.go +++ b/api/build/list_org.go @@ -135,7 +135,8 @@ func ListBuildsForOrg(c *gin.Context) { // verify the event provided is a valid event type if event != constants.EventComment && event != constants.EventDeploy && event != constants.EventPush && event != constants.EventPull && - event != constants.EventTag && event != constants.EventSchedule { + event != constants.EventTag && event != constants.EventSchedule && + event != constants.EventDelete { retErr := fmt.Errorf("unable to process event %s: invalid event type provided", event) util.HandleError(c, http.StatusBadRequest, retErr) diff --git a/api/build/list_repo.go b/api/build/list_repo.go index e6b043afd..a1a51d023 100644 --- a/api/build/list_repo.go +++ b/api/build/list_repo.go @@ -159,7 +159,8 @@ func ListBuildsForRepo(c *gin.Context) { // verify the event provided is a valid event type if event != constants.EventComment && event != constants.EventDeploy && event != constants.EventPush && event != constants.EventPull && - event != constants.EventTag && event != constants.EventSchedule { + event != constants.EventTag && event != constants.EventSchedule && + event != constants.EventDelete { retErr := fmt.Errorf("unable to process event %s: invalid event type provided", event) util.HandleError(c, http.StatusBadRequest, retErr) diff --git a/api/repo/create.go b/api/repo/create.go index f43adf5bd..d0ef7fee8 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -378,6 +378,12 @@ func defaultAllowedEvents(sliceDefaults []string, maskDefaults int64) *library.E comment.SetEdited(true) events.SetComment(comment) + case constants.EventDelete: + delete := new(actions.Delete) + delete.SetBranch(true) + delete.SetTag(true) + + events.SetDelete(delete) } } diff --git a/database/repo/repo_test.go b/database/repo/repo_test.go index 5e70956d7..e2b38d85b 100644 --- a/database/repo/repo_test.go +++ b/database/repo/repo_test.go @@ -225,6 +225,10 @@ func testEvents() *library.Events { Schedule: &actions.Schedule{ Run: new(bool), }, + Delete: &actions.Delete{ + Branch: new(bool), + Tag: new(bool), + }, } } diff --git a/database/secret/secret_test.go b/database/secret/secret_test.go index 3c6dfc0bd..db35dc874 100644 --- a/database/secret/secret_test.go +++ b/database/secret/secret_test.go @@ -250,6 +250,10 @@ func testEvents() *library.Events { Schedule: &actions.Schedule{ Run: new(bool), }, + Delete: &actions.Delete{ + Branch: new(bool), + Tag: new(bool), + }, } } diff --git a/go.mod b/go.mod index a1fe54c69..acc5aef80 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/drone/envsubst v1.0.3 github.com/gin-gonic/gin v1.9.1 github.com/go-playground/assert/v2 v2.2.0 - github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc + github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796 github.com/golang-jwt/jwt/v5 v5.1.0 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v56 v56.0.0 diff --git a/go.sum b/go.sum index e583a0a5c..c8c8f47fc 100644 --- a/go.sum +++ b/go.sum @@ -143,6 +143,8 @@ github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc h1:S59SXYfqFTJeuIBdwoKQE/oFRPAFU/LuzHCq3mRXe3w= github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc/go.mod h1:cax3mW1kVz/ioI8qltZE+wl9rOLgOPdwBIvCooL09e4= +github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796 h1:fjma5QAoncoknvTOp2AL/qYDOt4liWkZS5+sUaCQKJw= +github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796/go.mod h1:cax3mW1kVz/ioI8qltZE+wl9rOLgOPdwBIvCooL09e4= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 15ce12be1..5b2972e4e 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -79,6 +79,9 @@ func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*ty case *github.IssueCommentEvent: c.Logger.Tracef("issue comment") return c.processIssueCommentEvent(h, event) + case *github.DeleteEvent: + c.Logger.Tracef("delete") + return c.processDeleteEvent(h, event) case *github.RepositoryEvent: c.Logger.Tracef("repository") return c.processRepositoryEvent(h, event) @@ -501,6 +504,83 @@ func (c *client) processRepositoryEvent(h *library.Hook, payload *github.Reposit }, nil } +// processDeleteEvent is a helper function to process the delete event. +func (c *client) processDeleteEvent(h *library.Hook, payload *github.DeleteEvent) (*types.Webhook, error) { + c.Logger.WithFields(logrus.Fields{ + "org": payload.GetRepo().GetOwner().GetLogin(), + "repo": payload.GetRepo().GetName(), + }).Tracef("processing delete GitHub webhook for %s", payload.GetRepo().GetFullName()) + + repo := payload.GetRepo() + + // convert payload to library repo + r := new(library.Repo) + r.SetOrg(repo.GetOwner().GetLogin()) + r.SetName(repo.GetName()) + r.SetFullName(repo.GetFullName()) + r.SetLink(repo.GetHTMLURL()) + r.SetClone(repo.GetCloneURL()) + r.SetBranch(repo.GetDefaultBranch()) + r.SetPrivate(repo.GetPrivate()) + r.SetTopics(repo.Topics) + + // convert payload to library build + b := new(library.Build) + b.SetEvent(constants.EventDelete) + b.SetClone(repo.GetCloneURL()) + //b.SetSource(payload.GetHeadCommit().GetURL()) + b.SetTitle(fmt.Sprintf("%s received from %s", constants.EventDelete, repo.GetHTMLURL())) + //b.SetMessage(payload.GetHeadCommit().GetMessage()) + //b.SetCommit(payload.GetHeadCommit().GetID()) + b.SetSender(payload.GetSender().GetLogin()) + //b.SetAuthor(payload.GetDelete()) + //b.SetEmail(payload.GetHeadCommit().GetAuthor().GetEmail()) + b.SetBranch(strings.TrimPrefix(payload.GetRef(), "refs/heads/")) + b.SetRef(payload.GetRef()) + //b.SetBaseRef(payload.GetBaseRef()) + + // update the hook object + h.SetBranch(b.GetBranch()) + h.SetEvent(constants.EventDelete) + h.SetLink( + fmt.Sprintf("https://%s/%s/settings/hooks", h.GetHost(), r.GetFullName()), + ) + + // ensure the build author is set + //if len(b.GetAuthor()) == 0 { + // b.SetAuthor(payload.GetHeadCommit().GetCommitter().GetName()) + //} + + // ensure the build sender is set + //if len(b.GetSender()) == 0 { + // b.SetSender(payload.GetPusher().GetName()) + //} + + // ensure the build email is set + //if len(b.GetEmail()) == 0 { + // b.SetEmail(payload.GetHeadCommit().GetCommitter().GetEmail()) + //} + + // handle when push event is a tag + //if strings.HasPrefix(b.GetRef(), "refs/tags/") { + // set the proper event for the hook + // h.SetEvent(constants.EventTag) + // set the proper event for the build + // b.SetEvent(constants.EventTag) + + // set the proper branch from the base ref + // if strings.HasPrefix(payload.GetBaseRef(), "refs/heads/") { + // b.SetBranch(strings.TrimPrefix(payload.GetBaseRef(), "refs/heads/")) + // } + //} + + return &types.Webhook{ + Hook: h, + Repo: r, + Build: b, + }, nil +} + // getDeliveryID gets the last 100 webhook deliveries for a repo and // finds the matching delivery id with the source id in the hook. func (c *client) getDeliveryID(ctx context.Context, ghClient *github.Client, r *library.Repo, h *library.Hook) (int64, error) {