From e6556d14011a4ae33c38f77d211f4f54458e278c Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Wed, 20 Dec 2023 14:58:17 +0530 Subject: [PATCH] feat: support more events in webhook parse in go-scm for gitness (#289) --- .../webhooks/pull_request_closed.json | 77 ++++++++++++++++++ .../webhooks/pull_request_closed.json.golden | 48 ++++++++++++ .../webhooks/pull_request_merged.json | 78 +++++++++++++++++++ .../webhooks/pull_request_merged.json.golden | 48 ++++++++++++ scm/driver/harness/webhook.go | 7 +- scm/driver/harness/webhook_test.go | 14 ++++ 6 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_closed.json create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_closed.json.golden create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_merged.json create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_merged.json.golden diff --git a/scm/driver/harness/testdata/webhooks/pull_request_closed.json b/scm/driver/harness/testdata/webhooks/pull_request_closed.json new file mode 100644 index 00000000..f4f038ce --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_closed.json @@ -0,0 +1,77 @@ +{ + "trigger": "pullreq_closed", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + }, + "principal": { + "id": 3, + "uid": "admin", + "display_name": "Administrator", + "email": "admin@gitness.io", + "type": "user", + "created": 1696332021613, + "updated": 1696332021613 + }, + "pull_req": { + "number": 6, + "state": "closed", + "is_draft": false, + "title": "Create sad", + "source_repo_id": 22, + "source_branch": "asdxsa", + "target_repo_id": 22, + "target_branch": "main", + "author": { + "id": 3, + "uid": "admin", + "display_name": "Administrator", + "email": "admin@gitness.io", + "type": "user", + "created": 1696332021613, + "updated": 1696332021613 + } + }, + "target_ref": { + "name": "refs/heads/main", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + } + }, + "ref": { + "name": "refs/heads/asdxsa", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + } + }, + "sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574", + "commit": { + "sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574", + "message": "", + "author": { + "identity": { + "name": "Administrator", + "email": "admin@gitness.io" + }, + "when": "2023-12-20T13:10:47+05:30" + }, + "committer": { + "identity": { + "name": "Gitness", + "email": "system@gitness.io" + }, + "when": "2023-12-20T13:10:47+05:30" + } + } +} \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_closed.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_closed.json.golden new file mode 100644 index 00000000..fad057ac --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_closed.json.golden @@ -0,0 +1,48 @@ +{ + "Action": "closed", + "Repo": { + "ID": "22", + "Namespace": "", + "Name": "asdsad", + "Branch": "main", + "Private": false, + "Clone": "http://localhost:3000/git/codeowners/asdsad.git", + "CloneSSH": "", + "Link": "http://localhost:3000/git/codeowners/asdsad.git", + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "PullRequest": { + "Number": 6, + "Title": "Create sad", + "Body": "", + "Sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574", + "Ref": "refs/heads/asdxsa", + "Source": "asdxsa", + "Target": "main", + "Fork": "fork", + "Link": "http://localhost:3000/git/codeowners/asdsad.git", + "Closed": true, + "Merged": false, + "Author": { + "ID": "admin", + "Login": "admin", + "Name": "Administrator", + "Email": "admin@gitness.io", + "Avatar": "", + "Created": "2023-10-03T16:50:21.613+05:30", + "Updated": "2023-10-03T16:50:21.613+05:30" + }, + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "Sender": { + "ID": "admin", + "Login": "admin", + "Name": "Administrator", + "Email": "admin@gitness.io", + "Avatar": "", + "Created": "2023-10-03T16:50:21.613+05:30", + "Updated": "2023-10-03T16:50:21.613+05:30" + } +} \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_merged.json b/scm/driver/harness/testdata/webhooks/pull_request_merged.json new file mode 100644 index 00000000..32f2dcc8 --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_merged.json @@ -0,0 +1,78 @@ +{ + "trigger": "pullreq_merged", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + }, + "principal": { + "id": 3, + "uid": "admin", + "display_name": "Administrator", + "email": "admin@gitness.io", + "type": "user", + "created": 1696332021613, + "updated": 1696332021613 + }, + "pull_req": { + "number": 10, + "state": "merged", + "is_draft": false, + "title": "Create xxasc", + "source_repo_id": 22, + "source_branch": "xas", + "target_repo_id": 22, + "target_branch": "main", + "merge_strategy": "squash", + "author": { + "id": 3, + "uid": "admin", + "display_name": "Administrator", + "email": "admin@gitness.io", + "type": "user", + "created": 1696332021613, + "updated": 1696332021613 + } + }, + "target_ref": { + "name": "refs/heads/main", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + } + }, + "ref": { + "name": "refs/heads/xas", + "repo": { + "id": 22, + "path": "codeowners/asdsad", + "uid": "asdsad", + "default_branch": "main", + "git_url": "http://localhost:3000/git/codeowners/asdsad.git" + } + }, + "sha": "4ec41187008f77222a60dfa21cdbd980f6490443", + "commit": { + "sha": "4ec41187008f77222a60dfa21cdbd980f6490443", + "message": "", + "author": { + "identity": { + "name": "Administrator", + "email": "admin@gitness.io" + }, + "when": "2023-12-20T13:40:52+05:30" + }, + "committer": { + "identity": { + "name": "Gitness", + "email": "system@gitness.io" + }, + "when": "2023-12-20T13:40:52+05:30" + } + } +} \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_merged.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_merged.json.golden new file mode 100644 index 00000000..fe707ce6 --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_merged.json.golden @@ -0,0 +1,48 @@ +{ + "Action": "merged", + "Repo": { + "ID": "22", + "Namespace": "", + "Name": "asdsad", + "Branch": "main", + "Private": false, + "Clone": "http://localhost:3000/git/codeowners/asdsad.git", + "CloneSSH": "", + "Link": "http://localhost:3000/git/codeowners/asdsad.git", + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "PullRequest": { + "Number": 10, + "Title": "Create xxasc", + "Body": "", + "Sha": "4ec41187008f77222a60dfa21cdbd980f6490443", + "Ref": "refs/heads/xas", + "Source": "xas", + "Target": "main", + "Fork": "fork", + "Link": "http://localhost:3000/git/codeowners/asdsad.git", + "Closed": true, + "Merged": true, + "Author": { + "ID": "admin", + "Login": "admin", + "Name": "Administrator", + "Email": "admin@gitness.io", + "Avatar": "", + "Created": "2023-10-03T16:50:21.613+05:30", + "Updated": "2023-10-03T16:50:21.613+05:30" + }, + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "Sender": { + "ID": "admin", + "Login": "admin", + "Name": "Administrator", + "Email": "admin@gitness.io", + "Avatar": "", + "Created": "2023-10-03T16:50:21.613+05:30", + "Updated": "2023-10-03T16:50:21.613+05:30" + } +} \ No newline at end of file diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index d1ce064f..10f808e9 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -39,7 +39,7 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo // hook, err = s.parseIssueHook(data) case "branch_created", "branch_updated": hook, err = s.parsePushHook(data) - case "pullreq_created", "pullreq_reopened", "pullreq_branch_updated": + case "pullreq_created", "pullreq_reopened", "pullreq_branch_updated", "pullreq_closed", "pullreq_merged": hook, err = s.parsePullRequestHook(data) case "pullreq_comment_created": hook, err = s.parsePullRequestCommentHook(data) @@ -259,6 +259,10 @@ func convertAction(src string) (action scm.Action) { return scm.ActionUpdate case "pullreq_reopened": return scm.ActionReopen + case "pullreq_closed": + return scm.ActionClose + case "pullreq_merged": + return scm.ActionMerge default: return } @@ -271,6 +275,7 @@ func convertPullReq(pr pullReq, ref ref, commit hookCommit) scm.PullRequest { Closed: pr.State != "open", Source: pr.SourceBranch, Target: pr.TargetBranch, + Merged: pr.State == "merged", Fork: "fork", Link: ref.Repo.GitURL, Sha: commit.Sha, diff --git a/scm/driver/harness/webhook_test.go b/scm/driver/harness/webhook_test.go index 5e8d7cdf..26966db2 100644 --- a/scm/driver/harness/webhook_test.go +++ b/scm/driver/harness/webhook_test.go @@ -71,6 +71,20 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/pull_request_comment_created.json.golden", obj: new(scm.PullRequestCommentHook), }, + // pull request closed + { + event: "pullreq_reopened", + before: "testdata/webhooks/pull_request_closed.json", + after: "testdata/webhooks/pull_request_closed.json.golden", + obj: new(scm.PullRequestHook), + }, + // pull request merged + { + event: "pullreq_reopened", + before: "testdata/webhooks/pull_request_merged.json", + after: "testdata/webhooks/pull_request_merged.json.golden", + obj: new(scm.PullRequestHook), + }, } for _, test := range tests {