diff --git a/api/webhook/post.go b/api/webhook/post.go index 4564b97f1..7c695f820 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -367,12 +367,26 @@ func PostWebhook(c *gin.Context) { b.SetHeadRef(headref) } + // if the event is delete, + // call SCM for more data not provided in webhook payload + if strings.EqualFold(b.GetEvent(), constants.EventDelete) { + // send API call to capture the commit sha for the branch + _, commit, err := scm.FromContext(c).GetBranch(ctx, u, r, r.GetBranch()) + if err != nil { + retErr := fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err) + util.HandleError(c, http.StatusInternalServerError, retErr) + } + + b.SetCommit(commit) + } + // variable to store changeset files var files []string // check if the build event is not issue_comment or pull_request if !strings.EqualFold(b.GetEvent(), constants.EventComment) && - !strings.EqualFold(b.GetEvent(), constants.EventPull) { + !strings.EqualFold(b.GetEvent(), constants.EventPull) && + !strings.EqualFold(b.GetEvent(), constants.EventDelete) { // send API call to capture list of files changed for the commit files, err = scm.FromContext(c).Changeset(ctx, u, repo, b.GetCommit()) if err != nil { diff --git a/go.mod b/go.mod index acc5aef80..a14d20c6b 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.20240111192144-0d0e22367796 + github.com/go-vela/types v0.22.1-0.20240115212525-483bff0c8693 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 c8c8f47fc..6ae1e765c 100644 --- a/go.sum +++ b/go.sum @@ -145,6 +145,8 @@ github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc h1:S59SXYfqFTJeuI 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/go-vela/types v0.22.1-0.20240115212525-483bff0c8693 h1:hF7S/clsM2rdruxgkRceQSttjl0iMur1ZhYHxGbNXtY= +github.com/go-vela/types v0.22.1-0.20240115212525-483bff0c8693/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/__debug_bin273415630 b/scm/github/__debug_bin273415630 deleted file mode 100755 index baa0ee7ca..000000000 Binary files a/scm/github/__debug_bin273415630 and /dev/null differ diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 5b2972e4e..d6c42e742 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -528,16 +528,15 @@ func (c *client) processDeleteEvent(h *library.Hook, payload *github.DeleteEvent b := new(library.Build) b.SetEvent(constants.EventDelete) b.SetClone(repo.GetCloneURL()) - //b.SetSource(payload.GetHeadCommit().GetURL()) + b.SetEventAction(payload.GetRefType()) b.SetTitle(fmt.Sprintf("%s received from %s", constants.EventDelete, repo.GetHTMLURL())) - //b.SetMessage(payload.GetHeadCommit().GetMessage()) - //b.SetCommit(payload.GetHeadCommit().GetID()) + b.SetMessage(fmt.Sprintf("%s %s deleted", payload.GetRef(), payload.GetRefType())) b.SetSender(payload.GetSender().GetLogin()) - //b.SetAuthor(payload.GetDelete()) - //b.SetEmail(payload.GetHeadCommit().GetAuthor().GetEmail()) + b.SetAuthor(payload.GetSender().GetLogin()) + b.SetEmail(payload.GetSender().GetEmail()) b.SetBranch(strings.TrimPrefix(payload.GetRef(), "refs/heads/")) - b.SetRef(payload.GetRef()) - //b.SetBaseRef(payload.GetBaseRef()) + b.SetRef(repo.GetDefaultBranch()) + b.SetSource(payload.GetRepo().GetOwner().GetHTMLURL()) // update the hook object h.SetBranch(b.GetBranch()) @@ -546,34 +545,6 @@ func (c *client) processDeleteEvent(h *library.Hook, payload *github.DeleteEvent 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,