Skip to content

Commit

Permalink
feat: support more events in webhook parse in go-scm for gitness (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-harness authored Dec 20, 2023
1 parent e960969 commit e6556d1
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 1 deletion.
77 changes: 77 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_closed.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
78 changes: 78 additions & 0 deletions scm/driver/harness/testdata/webhooks/pull_request_merged.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 6 additions & 1 deletion scm/driver/harness/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions scm/driver/harness/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e6556d1

Please sign in to comment.