Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support more events in webhook parse in go-scm for gitness #289

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "https://localhost:3000/git/codeowners/asdsad.git"
},
"principal": {
"id": 3,
"uid": "admin",
"display_name": "Administrator",
"email": "[email protected]",
"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": "[email protected]",
"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": "https://localhost:3000/git/codeowners/asdsad.git"
}
},
"ref": {
"name": "refs/heads/asdxsa",
"repo": {
"id": 22,
"path": "codeowners/asdsad",
"uid": "asdsad",
"default_branch": "main",
"git_url": "https://localhost:3000/git/codeowners/asdsad.git"
}
},
"sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
"commit": {
"sha": "27822dd2ec788f924c97b0b194c5bfd906f2e574",
"message": "",
"author": {
"identity": {
"name": "Administrator",
"email": "[email protected]"
},
"when": "2023-12-20T13:10:47+05:30"
},
"committer": {
"identity": {
"name": "Gitness",
"email": "[email protected]"
},
"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": "https://localhost:3000/git/codeowners/asdsad.git",
"CloneSSH": "",
"Link": "https://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": "https://localhost:3000/git/codeowners/asdsad.git",
"Closed": true,
"Merged": false,
"Author": {
"ID": "admin",
"Login": "admin",
"Name": "Administrator",
"Email": "[email protected]",
"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": "[email protected]",
"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": "https://localhost:3000/git/codeowners/asdsad.git"
},
"principal": {
"id": 3,
"uid": "admin",
"display_name": "Administrator",
"email": "[email protected]",
"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": "[email protected]",
"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": "https://localhost:3000/git/codeowners/asdsad.git"
}
},
"ref": {
"name": "refs/heads/xas",
"repo": {
"id": 22,
"path": "codeowners/asdsad",
"uid": "asdsad",
"default_branch": "main",
"git_url": "https://localhost:3000/git/codeowners/asdsad.git"
}
},
"sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
"commit": {
"sha": "4ec41187008f77222a60dfa21cdbd980f6490443",
"message": "",
"author": {
"identity": {
"name": "Administrator",
"email": "[email protected]"
},
"when": "2023-12-20T13:40:52+05:30"
},
"committer": {
"identity": {
"name": "Gitness",
"email": "[email protected]"
},
"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": "https://localhost:3000/git/codeowners/asdsad.git",
"CloneSSH": "",
"Link": "https://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": "https://localhost:3000/git/codeowners/asdsad.git",
"Closed": true,
"Merged": true,
"Author": {
"ID": "admin",
"Login": "admin",
"Name": "Administrator",
"Email": "[email protected]",
"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": "[email protected]",
"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",
johannesHarness marked this conversation as resolved.
Show resolved Hide resolved
Source: pr.SourceBranch,
Target: pr.TargetBranch,
Merged: pr.State == "merged",
Fork: "fork",
johannesHarness marked this conversation as resolved.
Show resolved Hide resolved
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