Skip to content

Commit e6556d1

Browse files
feat: support more events in webhook parse in go-scm for gitness (#289)
1 parent e960969 commit e6556d1

File tree

6 files changed

+271
-1
lines changed

6 files changed

+271
-1
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"trigger": "pullreq_closed",
3+
"repo": {
4+
"id": 22,
5+
"path": "codeowners/asdsad",
6+
"uid": "asdsad",
7+
"default_branch": "main",
8+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
9+
},
10+
"principal": {
11+
"id": 3,
12+
"uid": "admin",
13+
"display_name": "Administrator",
14+
"email": "[email protected]",
15+
"type": "user",
16+
"created": 1696332021613,
17+
"updated": 1696332021613
18+
},
19+
"pull_req": {
20+
"number": 6,
21+
"state": "closed",
22+
"is_draft": false,
23+
"title": "Create sad",
24+
"source_repo_id": 22,
25+
"source_branch": "asdxsa",
26+
"target_repo_id": 22,
27+
"target_branch": "main",
28+
"author": {
29+
"id": 3,
30+
"uid": "admin",
31+
"display_name": "Administrator",
32+
"email": "[email protected]",
33+
"type": "user",
34+
"created": 1696332021613,
35+
"updated": 1696332021613
36+
}
37+
},
38+
"target_ref": {
39+
"name": "refs/heads/main",
40+
"repo": {
41+
"id": 22,
42+
"path": "codeowners/asdsad",
43+
"uid": "asdsad",
44+
"default_branch": "main",
45+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
46+
}
47+
},
48+
"ref": {
49+
"name": "refs/heads/asdxsa",
50+
"repo": {
51+
"id": 22,
52+
"path": "codeowners/asdsad",
53+
"uid": "asdsad",
54+
"default_branch": "main",
55+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
56+
}
57+
},
58+
"sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
59+
"commit": {
60+
"sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
61+
"message": "",
62+
"author": {
63+
"identity": {
64+
"name": "Administrator",
65+
"email": "[email protected]"
66+
},
67+
"when": "2023-12-20T13:10:47+05:30"
68+
},
69+
"committer": {
70+
"identity": {
71+
"name": "Gitness",
72+
"email": "[email protected]"
73+
},
74+
"when": "2023-12-20T13:10:47+05:30"
75+
}
76+
}
77+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"Action": "closed",
3+
"Repo": {
4+
"ID": "22",
5+
"Namespace": "",
6+
"Name": "asdsad",
7+
"Branch": "main",
8+
"Private": false,
9+
"Clone": "http://localhost:3000/git/codeowners/asdsad.git",
10+
"CloneSSH": "",
11+
"Link": "http://localhost:3000/git/codeowners/asdsad.git",
12+
"Created": "0001-01-01T00:00:00Z",
13+
"Updated": "0001-01-01T00:00:00Z"
14+
},
15+
"PullRequest": {
16+
"Number": 6,
17+
"Title": "Create sad",
18+
"Body": "",
19+
"Sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
20+
"Ref": "refs/heads/asdxsa",
21+
"Source": "asdxsa",
22+
"Target": "main",
23+
"Fork": "fork",
24+
"Link": "http://localhost:3000/git/codeowners/asdsad.git",
25+
"Closed": true,
26+
"Merged": false,
27+
"Author": {
28+
"ID": "admin",
29+
"Login": "admin",
30+
"Name": "Administrator",
31+
"Email": "[email protected]",
32+
"Avatar": "",
33+
"Created": "2023-10-03T16:50:21.613+05:30",
34+
"Updated": "2023-10-03T16:50:21.613+05:30"
35+
},
36+
"Created": "0001-01-01T00:00:00Z",
37+
"Updated": "0001-01-01T00:00:00Z"
38+
},
39+
"Sender": {
40+
"ID": "admin",
41+
"Login": "admin",
42+
"Name": "Administrator",
43+
"Email": "[email protected]",
44+
"Avatar": "",
45+
"Created": "2023-10-03T16:50:21.613+05:30",
46+
"Updated": "2023-10-03T16:50:21.613+05:30"
47+
}
48+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"trigger": "pullreq_merged",
3+
"repo": {
4+
"id": 22,
5+
"path": "codeowners/asdsad",
6+
"uid": "asdsad",
7+
"default_branch": "main",
8+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
9+
},
10+
"principal": {
11+
"id": 3,
12+
"uid": "admin",
13+
"display_name": "Administrator",
14+
"email": "[email protected]",
15+
"type": "user",
16+
"created": 1696332021613,
17+
"updated": 1696332021613
18+
},
19+
"pull_req": {
20+
"number": 10,
21+
"state": "merged",
22+
"is_draft": false,
23+
"title": "Create xxasc",
24+
"source_repo_id": 22,
25+
"source_branch": "xas",
26+
"target_repo_id": 22,
27+
"target_branch": "main",
28+
"merge_strategy": "squash",
29+
"author": {
30+
"id": 3,
31+
"uid": "admin",
32+
"display_name": "Administrator",
33+
"email": "[email protected]",
34+
"type": "user",
35+
"created": 1696332021613,
36+
"updated": 1696332021613
37+
}
38+
},
39+
"target_ref": {
40+
"name": "refs/heads/main",
41+
"repo": {
42+
"id": 22,
43+
"path": "codeowners/asdsad",
44+
"uid": "asdsad",
45+
"default_branch": "main",
46+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
47+
}
48+
},
49+
"ref": {
50+
"name": "refs/heads/xas",
51+
"repo": {
52+
"id": 22,
53+
"path": "codeowners/asdsad",
54+
"uid": "asdsad",
55+
"default_branch": "main",
56+
"git_url": "http://localhost:3000/git/codeowners/asdsad.git"
57+
}
58+
},
59+
"sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
60+
"commit": {
61+
"sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
62+
"message": "",
63+
"author": {
64+
"identity": {
65+
"name": "Administrator",
66+
"email": "[email protected]"
67+
},
68+
"when": "2023-12-20T13:40:52+05:30"
69+
},
70+
"committer": {
71+
"identity": {
72+
"name": "Gitness",
73+
"email": "[email protected]"
74+
},
75+
"when": "2023-12-20T13:40:52+05:30"
76+
}
77+
}
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"Action": "merged",
3+
"Repo": {
4+
"ID": "22",
5+
"Namespace": "",
6+
"Name": "asdsad",
7+
"Branch": "main",
8+
"Private": false,
9+
"Clone": "http://localhost:3000/git/codeowners/asdsad.git",
10+
"CloneSSH": "",
11+
"Link": "http://localhost:3000/git/codeowners/asdsad.git",
12+
"Created": "0001-01-01T00:00:00Z",
13+
"Updated": "0001-01-01T00:00:00Z"
14+
},
15+
"PullRequest": {
16+
"Number": 10,
17+
"Title": "Create xxasc",
18+
"Body": "",
19+
"Sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
20+
"Ref": "refs/heads/xas",
21+
"Source": "xas",
22+
"Target": "main",
23+
"Fork": "fork",
24+
"Link": "http://localhost:3000/git/codeowners/asdsad.git",
25+
"Closed": true,
26+
"Merged": true,
27+
"Author": {
28+
"ID": "admin",
29+
"Login": "admin",
30+
"Name": "Administrator",
31+
"Email": "[email protected]",
32+
"Avatar": "",
33+
"Created": "2023-10-03T16:50:21.613+05:30",
34+
"Updated": "2023-10-03T16:50:21.613+05:30"
35+
},
36+
"Created": "0001-01-01T00:00:00Z",
37+
"Updated": "0001-01-01T00:00:00Z"
38+
},
39+
"Sender": {
40+
"ID": "admin",
41+
"Login": "admin",
42+
"Name": "Administrator",
43+
"Email": "[email protected]",
44+
"Avatar": "",
45+
"Created": "2023-10-03T16:50:21.613+05:30",
46+
"Updated": "2023-10-03T16:50:21.613+05:30"
47+
}
48+
}

scm/driver/harness/webhook.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
3939
// hook, err = s.parseIssueHook(data)
4040
case "branch_created", "branch_updated":
4141
hook, err = s.parsePushHook(data)
42-
case "pullreq_created", "pullreq_reopened", "pullreq_branch_updated":
42+
case "pullreq_created", "pullreq_reopened", "pullreq_branch_updated", "pullreq_closed", "pullreq_merged":
4343
hook, err = s.parsePullRequestHook(data)
4444
case "pullreq_comment_created":
4545
hook, err = s.parsePullRequestCommentHook(data)
@@ -259,6 +259,10 @@ func convertAction(src string) (action scm.Action) {
259259
return scm.ActionUpdate
260260
case "pullreq_reopened":
261261
return scm.ActionReopen
262+
case "pullreq_closed":
263+
return scm.ActionClose
264+
case "pullreq_merged":
265+
return scm.ActionMerge
262266
default:
263267
return
264268
}
@@ -271,6 +275,7 @@ func convertPullReq(pr pullReq, ref ref, commit hookCommit) scm.PullRequest {
271275
Closed: pr.State != "open",
272276
Source: pr.SourceBranch,
273277
Target: pr.TargetBranch,
278+
Merged: pr.State == "merged",
274279
Fork: "fork",
275280
Link: ref.Repo.GitURL,
276281
Sha: commit.Sha,

scm/driver/harness/webhook_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ func TestWebhooks(t *testing.T) {
7171
after: "testdata/webhooks/pull_request_comment_created.json.golden",
7272
obj: new(scm.PullRequestCommentHook),
7373
},
74+
// pull request closed
75+
{
76+
event: "pullreq_reopened",
77+
before: "testdata/webhooks/pull_request_closed.json",
78+
after: "testdata/webhooks/pull_request_closed.json.golden",
79+
obj: new(scm.PullRequestHook),
80+
},
81+
// pull request merged
82+
{
83+
event: "pullreq_reopened",
84+
before: "testdata/webhooks/pull_request_merged.json",
85+
after: "testdata/webhooks/pull_request_merged.json.golden",
86+
obj: new(scm.PullRequestHook),
87+
},
7488
}
7589

7690
for _, test := range tests {

0 commit comments

Comments
 (0)