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..277249e11 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: + deletion := events.GetPush() + deletion.SetDeleteBranch(true) + deletion.SetDeleteTag(true) + + events.SetPush(deletion) } } diff --git a/api/webhook/post.go b/api/webhook/post.go index 1a321aef2..fb2618ebe 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -372,7 +372,8 @@ func PostWebhook(c *gin.Context) { // 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/database/repo/repo_test.go b/database/repo/repo_test.go index 5e70956d7..597ef1623 100644 --- a/database/repo/repo_test.go +++ b/database/repo/repo_test.go @@ -206,8 +206,10 @@ func testRepo() *library.Repo { func testEvents() *library.Events { return &library.Events{ Push: &actions.Push{ - Branch: new(bool), - Tag: new(bool), + Branch: new(bool), + Tag: new(bool), + DeleteBranch: new(bool), + DeleteTag: new(bool), }, PullRequest: &actions.Pull{ Opened: new(bool), diff --git a/database/secret/secret_test.go b/database/secret/secret_test.go index 3c6dfc0bd..31156ebca 100644 --- a/database/secret/secret_test.go +++ b/database/secret/secret_test.go @@ -231,8 +231,10 @@ func testSecret() *library.Secret { func testEvents() *library.Events { return &library.Events{ Push: &actions.Push{ - Branch: new(bool), - Tag: new(bool), + Branch: new(bool), + Tag: new(bool), + DeleteBranch: new(bool), + DeleteTag: new(bool), }, PullRequest: &actions.Pull{ Opened: new(bool), diff --git a/go.mod b/go.mod index a1fe54c69..8c88f696a 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.20240122175704-d058de26b401 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..6122592ad 100644 --- a/go.sum +++ b/go.sum @@ -141,8 +141,8 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= 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.20240122175704-d058de26b401 h1:LdqaHKseq6EUXIJU0h01p14J2rpXF2oFwKbJtsrdE2Y= +github.com/go-vela/types v0.22.1-0.20240122175704-d058de26b401/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/testdata/hooks/push_delete_branch.json b/scm/github/testdata/hooks/push_delete_branch.json new file mode 100644 index 000000000..afb6cbf24 --- /dev/null +++ b/scm/github/testdata/hooks/push_delete_branch.json @@ -0,0 +1,139 @@ +{ + "ref": "refs/heads/main", + "before": "d3d9188fc87a6977343e922c128f162a86018d76", + "after": "0000000000000000000000000000000000000000", + "created": false, + "deleted": true, + "forced": false, + "base_ref": null, + "compare": "https://github.com/Codertocat/Hello-World/compare/d3d9188fc87a...9c93babf5891", + "commits": [ + + ], + "head_commit": null, + "repository": { + "id": 135493233, + "node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=", + "name": "Hello-World", + "full_name": "Codertocat/Hello-World", + "owner": { + "name": "Codertocat", + "email": "21031067+Codertocat@users.noreply.github.com", + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/Codertocat/Hello-World", + "description": null, + "fork": false, + "url": "https://github.com/Codertocat/Hello-World", + "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", + "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", + "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", + "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", + "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", + "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", + "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", + "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", + "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", + "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", + "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", + "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", + "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", + "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", + "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", + "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", + "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", + "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", + "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", + "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", + "created_at": 1527711484, + "updated_at": "2018-05-30T20:18:35Z", + "pushed_at": 1527711528, + "git_url": "git://github.com/Codertocat/Hello-World.git", + "ssh_url": "git@github.com:Codertocat/Hello-World.git", + "clone_url": "https://github.com/Codertocat/Hello-World.git", + "svn_url": "https://github.com/Codertocat/Hello-World", + "homepage": null, + "size": 0, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": true, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "open_issues_count": 2, + "license": null, + "forks": 0, + "open_issues": 2, + "watchers": 0, + "default_branch": "main", + "stargazers": 0, + "master_branch": "main", + "topics": [ + "go", + "vela" + ] + }, + "pusher": { + "name": "Codertocat", + "email": "21031067+Codertocat@users.noreply.github.com" + }, + "sender": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/scm/github/testdata/hooks/push_delete_tag.json b/scm/github/testdata/hooks/push_delete_tag.json new file mode 100644 index 000000000..8d8e6a2b1 --- /dev/null +++ b/scm/github/testdata/hooks/push_delete_tag.json @@ -0,0 +1,140 @@ +{ + "ref": "refs/tags/v0.1", + "before": "d3d9188fc87a6977343e922c128f162a86018d76", + "after": "0000000000000000000000000000000000000000", + "created": false, + "deleted": true, + "forced": false, + "base_ref": null, + "compare": "https://github.com/Codertocat/Hello-World/compare/d3d9188fc87a...9c93babf5891", + "commits": [ + + ], + "head_commit": null, + "repository": { + "id": 135493233, + "node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=", + "name": "Hello-World", + "full_name": "Codertocat/Hello-World", + "owner": { + "name": "Codertocat", + "email": "21031067+Codertocat@users.noreply.github.com", + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/Codertocat/Hello-World", + "description": null, + "fork": false, + "url": "https://github.com/Codertocat/Hello-World", + "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", + "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", + "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", + "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", + "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", + "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", + "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", + "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", + "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", + "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", + "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", + "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", + "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", + "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", + "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", + "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", + "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", + "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", + "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", + "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", + "created_at": 1527711484, + "updated_at": "2018-05-30T20:18:35Z", + "pushed_at": 1527711528, + "git_url": "git://github.com/Codertocat/Hello-World.git", + "ssh_url": "git@github.com:Codertocat/Hello-World.git", + "clone_url": "https://github.com/Codertocat/Hello-World.git", + "svn_url": "https://github.com/Codertocat/Hello-World", + "homepage": null, + "size": 0, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": true, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "open_issues_count": 2, + "license": null, + "forks": 0, + "open_issues": 2, + "watchers": 0, + "default_branch": "main", + "stargazers": 0, + "master_branch": "main", + "topics": [ + "go", + "vela" + ] + }, + "pusher": { + "name": "Codertocat", + "email": "21031067+Codertocat@users.noreply.github.com" + }, + "sender": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } + } + \ No newline at end of file diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 8dcda751d..14612793c 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -196,6 +196,34 @@ func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (* } } + // handle when push event is a delete + if strings.EqualFold(b.GetCommit(), "") { + b.SetCommit(payload.GetBefore()) + b.SetRef(payload.GetBefore()) + b.SetTitle(fmt.Sprintf("%s received from %s", constants.EventDelete, repo.GetHTMLURL())) + b.SetAuthor(payload.GetSender().GetLogin()) + b.SetSource(fmt.Sprintf("%s/commit/%s", payload.GetRepo().GetHTMLURL(), payload.GetBefore())) + b.SetEmail(payload.GetPusher().GetEmail()) + + // set the proper event for the hook + h.SetEvent(constants.EventDelete) + // set the proper event for the build + b.SetEvent(constants.EventDelete) + + if strings.HasPrefix(payload.GetRef(), "refs/tags/") { + b.SetBranch(strings.TrimPrefix(payload.GetRef(), "refs/tags/")) + // set the proper action for the build + b.SetEventAction(constants.ActionTag) + // set the proper message for the build + b.SetMessage(fmt.Sprintf("%s %s deleted", strings.TrimPrefix(payload.GetRef(), "refs/tags/"), constants.ActionTag)) + } else { + // set the proper action for the build + b.SetEventAction(constants.ActionBranch) + // set the proper message for the build + b.SetMessage(fmt.Sprintf("%s %s deleted", strings.TrimPrefix(payload.GetRef(), "refs/heads/"), constants.ActionBranch)) + } + } + return &types.Webhook{ Hook: h, Repo: r, diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index fb0aef9c2..10fe49b43 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -175,6 +175,160 @@ func TestGithub_ProcessWebhook_Push_NoSender(t *testing.T) { } } +func TestGithub_ProcessWebhook_Push_Branch_Delete(t *testing.T) { + // setup router + s := httptest.NewServer(http.NotFoundHandler()) + defer s.Close() + + // setup request + body, err := os.Open("testdata/hooks/push_delete_branch.json") + if err != nil { + t.Errorf("unable to open file: %v", err) + } + + defer body.Close() + + request, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/test", body) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") + request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") + request.Header.Set("X-GitHub-Event", "push") + + // setup client + client, _ := NewTest(s.URL) + + // run test + wantHook := new(library.Hook) + wantHook.SetNumber(1) + wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) + wantHook.SetCreated(time.Now().UTC().Unix()) + wantHook.SetHost("github.com") + wantHook.SetEvent("delete") + wantHook.SetBranch("main") + wantHook.SetStatus(constants.StatusSuccess) + wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") + + wantRepo := new(library.Repo) + wantRepo.SetOrg("Codertocat") + wantRepo.SetName("Hello-World") + wantRepo.SetFullName("Codertocat/Hello-World") + wantRepo.SetLink("https://github.com/Codertocat/Hello-World") + wantRepo.SetClone("https://github.com/Codertocat/Hello-World.git") + wantRepo.SetBranch("main") + wantRepo.SetPrivate(false) + wantRepo.SetTopics([]string{"go", "vela"}) + + wantBuild := new(library.Build) + wantBuild.SetEvent("delete") + wantBuild.SetEventAction("branch") + wantBuild.SetClone("https://github.com/Codertocat/Hello-World.git") + wantBuild.SetSource("https://github.com/Codertocat/Hello-World/commit/d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetTitle("delete received from https://github.com/Codertocat/Hello-World") + wantBuild.SetMessage("main branch deleted") + wantBuild.SetCommit("d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetSender("Codertocat") + wantBuild.SetAuthor("Codertocat") + wantBuild.SetEmail("21031067+Codertocat@users.noreply.github.com") + wantBuild.SetBranch("main") + wantBuild.SetRef("d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetBaseRef("") + + want := &types.Webhook{ + Hook: wantHook, + Repo: wantRepo, + Build: wantBuild, + } + + got, err := client.ProcessWebhook(context.TODO(), request) + + if err != nil { + t.Errorf("ProcessWebhook returned err: %v", err) + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("ProcessWebhook is %v, want %v", got, want) + } +} + +func TestGithub_ProcessWebhook_Push_Tag_Delete(t *testing.T) { + // setup router + s := httptest.NewServer(http.NotFoundHandler()) + defer s.Close() + + // setup request + body, err := os.Open("testdata/hooks/push_delete_tag.json") + if err != nil { + t.Errorf("unable to open file: %v", err) + } + + defer body.Close() + + request, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/test", body) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("User-Agent", "GitHub-Hookshot/a22606a") + request.Header.Set("X-GitHub-Delivery", "7bd477e4-4415-11e9-9359-0d41fdf9567e") + request.Header.Set("X-GitHub-Hook-ID", "123456") + request.Header.Set("X-GitHub-Event", "push") + + // setup client + client, _ := NewTest(s.URL) + + // run test + wantHook := new(library.Hook) + wantHook.SetNumber(1) + wantHook.SetSourceID("7bd477e4-4415-11e9-9359-0d41fdf9567e") + wantHook.SetWebhookID(123456) + wantHook.SetCreated(time.Now().UTC().Unix()) + wantHook.SetHost("github.com") + wantHook.SetEvent("delete") + wantHook.SetBranch("refs/tags/v0.1") + wantHook.SetStatus(constants.StatusSuccess) + wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") + + wantRepo := new(library.Repo) + wantRepo.SetOrg("Codertocat") + wantRepo.SetName("Hello-World") + wantRepo.SetFullName("Codertocat/Hello-World") + wantRepo.SetLink("https://github.com/Codertocat/Hello-World") + wantRepo.SetClone("https://github.com/Codertocat/Hello-World.git") + wantRepo.SetBranch("main") + wantRepo.SetPrivate(false) + wantRepo.SetTopics([]string{"go", "vela"}) + + wantBuild := new(library.Build) + wantBuild.SetEvent("delete") + wantBuild.SetEventAction("tag") + wantBuild.SetClone("https://github.com/Codertocat/Hello-World.git") + wantBuild.SetSource("https://github.com/Codertocat/Hello-World/commit/d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetTitle("delete received from https://github.com/Codertocat/Hello-World") + wantBuild.SetMessage("v0.1 tag deleted") + wantBuild.SetCommit("d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetSender("Codertocat") + wantBuild.SetAuthor("Codertocat") + wantBuild.SetEmail("21031067+Codertocat@users.noreply.github.com") + wantBuild.SetBranch("v0.1") + wantBuild.SetRef("d3d9188fc87a6977343e922c128f162a86018d76") + wantBuild.SetBaseRef("") + + want := &types.Webhook{ + Hook: wantHook, + Repo: wantRepo, + Build: wantBuild, + } + + got, err := client.ProcessWebhook(context.TODO(), request) + + if err != nil { + t.Errorf("ProcessWebhook returned err: %v", err) + } + + if !reflect.DeepEqual(got, want) { + t.Errorf("ProcessWebhook is %v, want %v", got, want) + } +} + func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { // setup router s := httptest.NewServer(http.NotFoundHandler())