From 17753a478d19f3f1eeea94035d9dc3de489423c5 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 00:53:28 -0700 Subject: [PATCH 1/9] feat: Add pr_comment webhook for harness --- .../pull_request_comment_created.json | 53 ++++++++++++++++++ .../pull_request_comment_created.json.golden | 44 +++++++++++++++ scm/driver/harness/webhook.go | 54 +++++++++++++++++-- scm/driver/harness/webhook_test.go | 7 +++ 4 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_comment_created.json create mode 100644 scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json new file mode 100644 index 000000000..7b56f4fdd --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json @@ -0,0 +1,53 @@ +{ + "trigger": "pullreq_comment_created", + "repo": { + "id": 18, + "path": "asd/demo", + "uid": "demo", + "default_branch": "main", + "git_url": "http://localhost:3000/git/asd/demo.git" + }, + "principal": { + "id": 3, + "uid": "admin", + "display_name": "Administrator", + "email": "admin@gitness.io", + "type": "user", + "created": 1696332021613, + "updated": 1696332021613 + }, + "pull_req": { + "number": 2, + "state": "open", + "is_draft": false, + "title": "Update test.txt", + "source_repo_id": 18, + "source_branch": "pr2", + "target_repo_id": 18, + "target_branch": "main", + "merge_strategy": null + }, + "target_ref": { + "name": "refs/heads/main", + "repo": { + "id": 18, + "path": "asd/demo", + "uid": "demo", + "default_branch": "main", + "git_url": "http://localhost:3000/git/asd/demo.git" + } + }, + "ref": { + "name": "refs/heads/pr2", + "repo": { + "id": 18, + "path": "asd/demo", + "uid": "demo", + "default_branch": "main", + "git_url": "http://localhost:3000/git/asd/demo.git" + } + }, + "comment": { + "text": "pr comment" + } +} \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden new file mode 100644 index 000000000..e7321792e --- /dev/null +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden @@ -0,0 +1,44 @@ +{ + "Repo": { + "ID": "demo", + "Namespace": "", + "Name": "", + "Branch": "main", + "Private": false, + "Clone": "http://localhost:3000/git/asd/demo.git", + "CloneSSH": "", + "Link": "http://localhost:3000/git/asd/demo.git", + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "PullRequest": { + "Number": 2, + "Title": "Update test.txt", + "Body": "", + "Sha": "", + "Ref": "refs/heads/pr2", + "Source": "pr2", + "Target": "main", + "Fork": "fork", + "Link": "http://localhost:3000/git/asd/demo.git", + "Closed": false, + "Merged": false, + "Author": { + "Login": "", + "Name": "", + "Email": "", + "Avatar": "" + }, + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" + }, + "Comment": { + "Body": "pr comment" + }, + "Sender": { + "Login": "", + "Name": "", + "Email": "admin@gitness.io", + "Avatar": "" + } +} \ No newline at end of file diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index cc077a6ca..afc05a257 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -7,12 +7,11 @@ package harness import ( "crypto/sha256" "encoding/json" + "github.com/drone/go-scm/scm" + "github.com/drone/go-scm/scm/driver/internal/hmac" "io" "io/ioutil" "net/http" - - "github.com/drone/go-scm/scm" - "github.com/drone/go-scm/scm/driver/internal/hmac" ) type webhookService struct { @@ -39,6 +38,8 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo hook, err = s.parsePushHook(data) case "pullreq_created", "pullreq_reopened", "pullreq_branch_updated": hook, err = s.parsePullRequestHook(data) + case "pullreq_comment_created": + hook, err = s.parsePullRequestCommentHook(data) default: return nil, scm.ErrUnknownEvent } @@ -89,6 +90,12 @@ func (s *webhookService) parsePushHook(data []byte) (scm.Webhook, error) { return convertPushHook(dst), err } +func (s *webhookService) parsePullRequestCommentHook(data []byte) (scm.Webhook, error) { + dst := new(pullRequestCommentHook) + err := json.Unmarshal(data, dst) + return convertPullRequestCommentHook(dst), err +} + // native data structures type ( repo struct { @@ -156,6 +163,9 @@ type ( When string `json:"when"` } `json:"committer"` } + comment struct { + Text string `json:"text"` + } // harness pull request webhook payload pullRequestHook struct { Trigger string `json:"trigger"` @@ -178,6 +188,16 @@ type ( OldSha string `json:"old_sha"` Forced bool `json:"forced"` } + // harness pull request comment webhook payload + pullRequestCommentHook struct { + Trigger string `json:"trigger"` + Repo repo `json:"repo"` + Principal principal `json:"principal"` + PullReq pullReq `json:"pull_req"` + TargetRef targetRef `json:"target_ref"` + Ref ref `json:"ref"` + Comment comment `json:"comment"` + } ) // @@ -236,6 +256,34 @@ func convertPushHook(dst *pushHook) *scm.PushHook { } } +func convertPullRequestCommentHook(dst *pullRequestCommentHook) *scm.PullRequestCommentHook { + return &scm.PullRequestCommentHook{ + Repo: scm.Repository{ + ID: dst.Repo.UID, + Branch: dst.Repo.DefaultBranch, + Link: dst.Repo.GitURL, + Clone: dst.Repo.GitURL, + }, + PullRequest: scm.PullRequest{ + Number: dst.PullReq.Number, + Title: dst.PullReq.Title, + Closed: dst.PullReq.State != "open", + Source: dst.PullReq.SourceBranch, + Target: dst.PullReq.TargetBranch, + Fork: "fork", + Link: dst.Ref.Repo.GitURL, + Ref: dst.Ref.Name, + }, + Comment: scm.Comment{ + Body: dst.Comment.Text, + }, + + Sender: scm.User{ + Email: dst.Principal.Email, + }, + } +} + func convertAction(src string) (action scm.Action) { switch src { case "pullreq_created": diff --git a/scm/driver/harness/webhook_test.go b/scm/driver/harness/webhook_test.go index 4e38687be..175115e61 100644 --- a/scm/driver/harness/webhook_test.go +++ b/scm/driver/harness/webhook_test.go @@ -57,6 +57,13 @@ func TestWebhooks(t *testing.T) { after: "testdata/webhooks/pull_request_branch_updated.json.golden", obj: new(scm.PullRequestHook), }, + // pull request comment created + { + event: "pullreq_comment_created", + before: "testdata/webhooks/pull_request_comment_created.json", + after: "testdata/webhooks/pull_request_comment_created.json.golden", + obj: new(scm.PullRequestCommentHook), + }, } for _, test := range tests { From c4f15d5f1fe1c5c2c483a6f3943c8c907bdb8521 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 00:57:42 -0700 Subject: [PATCH 2/9] style --- scm/driver/harness/webhook.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index afc05a257..c09bc2571 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -7,11 +7,12 @@ package harness import ( "crypto/sha256" "encoding/json" - "github.com/drone/go-scm/scm" - "github.com/drone/go-scm/scm/driver/internal/hmac" "io" "io/ioutil" "net/http" + + "github.com/drone/go-scm/scm" + "github.com/drone/go-scm/scm/driver/internal/hmac" ) type webhookService struct { From a90bd32f33bcc8c7564dac30db9b3528b94b62f7 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 18:04:10 -0700 Subject: [PATCH 3/9] feat: Add pr_comment webhook for harness --- .../webhooks/pull_request_branch_updated.json | 11 ++++- .../pull_request_comment_created.json | 11 ++++- .../pull_request_comment_created.json.golden | 4 +- .../webhooks/pull_request_opened.json | 11 ++++- .../webhooks/pull_request_reopened.json | 11 ++++- scm/driver/harness/webhook.go | 41 +++++++++++-------- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json index 7509798e9..2709b46dc 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json +++ b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json @@ -25,7 +25,16 @@ "source_branch": "b", "target_repo_id": 13, "target_branch": "main", - "merge_strategy": null + "merge_strategy": null, + "author": { + "id": 8, + "uid": "0osgWsTZRsSZ8RWfjLRkEg", + "display_name": "Admin", + "email": "admin@harness.io", + "type": "user", + "created": 1675390885380, + "updated": 1675390885380 + } }, "target_ref": { "name": "refs/heads/main", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json index 7b56f4fdd..408f690fc 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json @@ -25,7 +25,16 @@ "source_branch": "pr2", "target_repo_id": 18, "target_branch": "main", - "merge_strategy": null + "merge_strategy": null, + "author": { + "id": 8, + "uid": "0osgWsTZRsSZ8RWfjLRkEg", + "display_name": "Admin", + "email": "admin@harness.io", + "type": "user", + "created": 1675390885380, + "updated": 1675390885380 + } }, "target_ref": { "name": "refs/heads/main", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden index e7321792e..eab82d033 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden @@ -25,8 +25,8 @@ "Merged": false, "Author": { "Login": "", - "Name": "", - "Email": "", + "Name": "Admin", + "Email": "admin@harness.io", "Avatar": "" }, "Created": "0001-01-01T00:00:00Z", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_opened.json b/scm/driver/harness/testdata/webhooks/pull_request_opened.json index a7fd7666b..45b76600c 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_opened.json +++ b/scm/driver/harness/testdata/webhooks/pull_request_opened.json @@ -25,7 +25,16 @@ "source_branch": "b", "target_repo_id": 13, "target_branch": "main", - "merge_strategy": null + "merge_strategy": null, + "author": { + "id": 8, + "uid": "0osgWsTZRsSZ8RWfjLRkEg", + "display_name": "Admin", + "email": "admin@harness.io", + "type": "user", + "created": 1675390885380, + "updated": 1675390885380 + } }, "target_ref": { "name": "refs/heads/main", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json index c6549d374..d8a8b2d86 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json +++ b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json @@ -25,7 +25,16 @@ "source_branch": "b", "target_repo_id": 13, "target_branch": "main", - "merge_strategy": null + "merge_strategy": null, + "author": { + "id": 8, + "uid": "0osgWsTZRsSZ8RWfjLRkEg", + "display_name": "Admin", + "email": "admin@harness.io", + "type": "user", + "created": 1675390885380, + "updated": 1675390885380 + } }, "target_ref": { "name": "refs/heads/main", diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index c09bc2571..9367b8dd3 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -125,6 +125,7 @@ type ( TargetRepoID int `json:"target_repo_id"` TargetBranch string `json:"target_branch"` MergeStrategy interface{} `json:"merge_strategy"` + Author principal `json:"author"` } targetRef struct { Name string `json:"name"` @@ -166,6 +167,7 @@ type ( } comment struct { Text string `json:"text"` + ID int `json:"id"` } // harness pull request webhook payload pullRequestHook struct { @@ -191,13 +193,15 @@ type ( } // harness pull request comment webhook payload pullRequestCommentHook struct { - Trigger string `json:"trigger"` - Repo repo `json:"repo"` - Principal principal `json:"principal"` - PullReq pullReq `json:"pull_req"` - TargetRef targetRef `json:"target_ref"` - Ref ref `json:"ref"` - Comment comment `json:"comment"` + Trigger string `json:"trigger"` + Repo repo `json:"repo"` + Principal principal `json:"principal"` + PullReq pullReq `json:"pull_req"` + TargetRef targetRef `json:"target_ref"` + Ref ref `json:"ref"` + Sha string `json:"sha"` + Commit hookCommit `json:"commit"` + Comment comment `json:"comment"` } ) @@ -219,8 +223,8 @@ func convertPullRequestHook(dst *pullRequestHook) *scm.PullRequestHook { Sha: dst.Commit.Sha, Ref: dst.Ref.Name, Author: scm.User{ - Name: dst.Commit.Committer.Identity.Name, - Email: dst.Commit.Committer.Identity.Email, + Name: dst.PullReq.Author.DisplayName, + Email: dst.PullReq.Author.Email, }, }, Repo: scm.Repository{ @@ -259,12 +263,6 @@ func convertPushHook(dst *pushHook) *scm.PushHook { func convertPullRequestCommentHook(dst *pullRequestCommentHook) *scm.PullRequestCommentHook { return &scm.PullRequestCommentHook{ - Repo: scm.Repository{ - ID: dst.Repo.UID, - Branch: dst.Repo.DefaultBranch, - Link: dst.Repo.GitURL, - Clone: dst.Repo.GitURL, - }, PullRequest: scm.PullRequest{ Number: dst.PullReq.Number, Title: dst.PullReq.Title, @@ -273,12 +271,23 @@ func convertPullRequestCommentHook(dst *pullRequestCommentHook) *scm.PullRequest Target: dst.PullReq.TargetBranch, Fork: "fork", Link: dst.Ref.Repo.GitURL, + Sha: dst.Commit.Sha, Ref: dst.Ref.Name, + Author: scm.User{ + Name: dst.PullReq.Author.DisplayName, + Email: dst.PullReq.Author.Email, + }, + }, + Repo: scm.Repository{ + ID: dst.Repo.UID, + Branch: dst.Repo.DefaultBranch, + Link: dst.Repo.GitURL, + Clone: dst.Repo.GitURL, }, Comment: scm.Comment{ Body: dst.Comment.Text, + ID: dst.Comment.ID, }, - Sender: scm.User{ Email: dst.Principal.Email, }, From 1662e44c6d3bb6b9a26c7e77cd572c807dcb7379 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 18:13:01 -0700 Subject: [PATCH 4/9] feat: Add pr_comment webhook for harness --- scm/driver/harness/webhook.go | 99 ++++++++++++++++------------------- 1 file changed, 44 insertions(+), 55 deletions(-) diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index 9367b8dd3..c6758b509 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -209,81 +209,43 @@ type ( // native data structure conversion // -func convertPullRequestHook(dst *pullRequestHook) *scm.PullRequestHook { +func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook { return &scm.PullRequestHook{ - Action: convertAction(dst.Trigger), - PullRequest: scm.PullRequest{ - Number: dst.PullReq.Number, - Title: dst.PullReq.Title, - Closed: dst.PullReq.State != "open", - Source: dst.PullReq.SourceBranch, - Target: dst.PullReq.TargetBranch, - Fork: "fork", - Link: dst.Ref.Repo.GitURL, - Sha: dst.Commit.Sha, - Ref: dst.Ref.Name, - Author: scm.User{ - Name: dst.PullReq.Author.DisplayName, - Email: dst.PullReq.Author.Email, - }, - }, - Repo: scm.Repository{ - ID: dst.Repo.UID, - Branch: dst.Repo.DefaultBranch, - Link: dst.Repo.GitURL, - Clone: dst.Repo.GitURL, - }, + Action: convertAction(src.Trigger), + PullRequest: convertPullReq(src.PullReq, src.Ref, src.Commit), + Repo: convertRepo(src.Repo), Sender: scm.User{ - Email: dst.Principal.Email, + Email: src.Principal.Email, }, } } -func convertPushHook(dst *pushHook) *scm.PushHook { +func convertPushHook(src *pushHook) *scm.PushHook { return &scm.PushHook{ - Ref: dst.Sha, - Before: dst.OldSha, - After: dst.Sha, + Ref: src.Sha, + Before: src.OldSha, + After: src.Sha, Repo: scm.Repository{ - Name: dst.Repo.UID, + Name: src.Repo.UID, }, Commit: scm.Commit{ - Sha: dst.Commit.Sha, - Message: dst.Commit.Message, + Sha: src.Commit.Sha, + Message: src.Commit.Message, Author: scm.Signature{ - Name: dst.Commit.Author.Identity.Name, - Email: dst.Commit.Author.Identity.Email, + Name: src.Commit.Author.Identity.Name, + Email: src.Commit.Author.Identity.Email, }, }, Sender: scm.User{ - Name: dst.Principal.DisplayName, + Name: src.Principal.DisplayName, }, } } func convertPullRequestCommentHook(dst *pullRequestCommentHook) *scm.PullRequestCommentHook { return &scm.PullRequestCommentHook{ - PullRequest: scm.PullRequest{ - Number: dst.PullReq.Number, - Title: dst.PullReq.Title, - Closed: dst.PullReq.State != "open", - Source: dst.PullReq.SourceBranch, - Target: dst.PullReq.TargetBranch, - Fork: "fork", - Link: dst.Ref.Repo.GitURL, - Sha: dst.Commit.Sha, - Ref: dst.Ref.Name, - Author: scm.User{ - Name: dst.PullReq.Author.DisplayName, - Email: dst.PullReq.Author.Email, - }, - }, - Repo: scm.Repository{ - ID: dst.Repo.UID, - Branch: dst.Repo.DefaultBranch, - Link: dst.Repo.GitURL, - Clone: dst.Repo.GitURL, - }, + PullRequest: convertPullReq(dst.PullReq, dst.Ref, dst.Commit), + Repo: convertRepo(dst.Repo), Comment: scm.Comment{ Body: dst.Comment.Text, ID: dst.Comment.ID, @@ -306,3 +268,30 @@ func convertAction(src string) (action scm.Action) { return } } + +func convertPullReq(pr pullReq, ref ref, commit hookCommit) scm.PullRequest { + return scm.PullRequest{ + Number: pr.Number, + Title: pr.Title, + Closed: pr.State != "open", + Source: pr.SourceBranch, + Target: pr.TargetBranch, + Fork: "fork", + Link: ref.Repo.GitURL, + Sha: commit.Sha, + Ref: ref.Name, + Author: scm.User{ + Name: pr.Author.DisplayName, + Email: pr.Author.Email, + }, + } +} + +func convertRepo(repo repo) scm.Repository { + return scm.Repository{ + ID: repo.UID, + Branch: repo.DefaultBranch, + Link: repo.GitURL, + Clone: repo.GitURL, + } +} From bec92684cdde0c0f9ab0d123fd040ee3d688fcce Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 19:35:33 -0700 Subject: [PATCH 5/9] add common methods --- .../webhooks/branch_create.json.golden | 7 +++- .../pull_request_branch_updated.json.golden | 12 ++++-- .../pull_request_comment_created.json.golden | 12 ++++-- .../webhooks/pull_request_opened.json.golden | 12 ++++-- .../pull_request_reopened.json.golden | 12 ++++-- scm/driver/harness/webhook.go | 38 ++++++++++--------- 6 files changed, 61 insertions(+), 32 deletions(-) diff --git a/scm/driver/harness/testdata/webhooks/branch_create.json.golden b/scm/driver/harness/testdata/webhooks/branch_create.json.golden index 00e4ce11b..e3c4954ab 100644 --- a/scm/driver/harness/testdata/webhooks/branch_create.json.golden +++ b/scm/driver/harness/testdata/webhooks/branch_create.json.golden @@ -35,9 +35,12 @@ "Link": "" }, "Sender": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", "Name": "default", - "Email": "", - "Avatar": "" + "Email": "default@harness.io", + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" } } \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden index 6be31bb70..d0fcc8676 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden @@ -25,18 +25,24 @@ "Closed": false, "Merged": false, "Author": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", "Name": "Admin", "Email": "admin@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" }, "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" }, "Sender": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", - "Name": "", + "Name": "default", "Email": "default@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" } } \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden index eab82d033..7ca477eca 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden @@ -24,10 +24,13 @@ "Closed": false, "Merged": false, "Author": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", "Name": "Admin", "Email": "admin@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" }, "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" @@ -36,9 +39,12 @@ "Body": "pr comment" }, "Sender": { + "ID": "admin", "Login": "", - "Name": "", + "Name": "Administrator", "Email": "admin@gitness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-10-03T04:20:21.613-07:00", + "Updated": "2023-10-03T04:20:21.613-07:00" } } \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden index 2928bf6a1..8066564c2 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden @@ -25,18 +25,24 @@ "Closed": false, "Merged": false, "Author": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", "Name": "Admin", "Email": "admin@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" }, "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" }, "Sender": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", - "Name": "", + "Name": "default", "Email": "default@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" } } \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden index 03f8b54df..6eb93b97d 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden @@ -25,18 +25,24 @@ "Closed": false, "Merged": false, "Author": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", "Name": "Admin", "Email": "admin@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" }, "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" }, "Sender": { + "ID": "0osgWsTZRsSZ8RWfjLRkEg", "Login": "", - "Name": "", + "Name": "default", "Email": "default@harness.io", - "Avatar": "" + "Avatar": "", + "Created": "2023-02-02T18:21:25.38-08:00", + "Updated": "2023-02-02T18:21:25.38-08:00" } } \ No newline at end of file diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index c6758b509..472390b72 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -10,6 +10,7 @@ import ( "io" "io/ioutil" "net/http" + "time" "github.com/drone/go-scm/scm" "github.com/drone/go-scm/scm/driver/internal/hmac" @@ -214,9 +215,7 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook { Action: convertAction(src.Trigger), PullRequest: convertPullReq(src.PullReq, src.Ref, src.Commit), Repo: convertRepo(src.Repo), - Sender: scm.User{ - Email: src.Principal.Email, - }, + Sender: convertUser(src.Principal), } } @@ -236,23 +235,19 @@ func convertPushHook(src *pushHook) *scm.PushHook { Email: src.Commit.Author.Identity.Email, }, }, - Sender: scm.User{ - Name: src.Principal.DisplayName, - }, + Sender: convertUser(src.Principal), } } -func convertPullRequestCommentHook(dst *pullRequestCommentHook) *scm.PullRequestCommentHook { +func convertPullRequestCommentHook(src *pullRequestCommentHook) *scm.PullRequestCommentHook { return &scm.PullRequestCommentHook{ - PullRequest: convertPullReq(dst.PullReq, dst.Ref, dst.Commit), - Repo: convertRepo(dst.Repo), + PullRequest: convertPullReq(src.PullReq, src.Ref, src.Commit), + Repo: convertRepo(src.Repo), Comment: scm.Comment{ - Body: dst.Comment.Text, - ID: dst.Comment.ID, - }, - Sender: scm.User{ - Email: dst.Principal.Email, + Body: src.Comment.Text, + ID: src.Comment.ID, }, + Sender: convertUser(src.Principal), } } @@ -280,10 +275,7 @@ func convertPullReq(pr pullReq, ref ref, commit hookCommit) scm.PullRequest { Link: ref.Repo.GitURL, Sha: commit.Sha, Ref: ref.Name, - Author: scm.User{ - Name: pr.Author.DisplayName, - Email: pr.Author.Email, - }, + Author: convertUser(pr.Author), } } @@ -295,3 +287,13 @@ func convertRepo(repo repo) scm.Repository { Clone: repo.GitURL, } } + +func convertUser(principal principal) scm.User { + return scm.User{ + Name: principal.DisplayName, + ID: principal.UID, + Email: principal.Email, + Created: time.UnixMilli(principal.Created), + Updated: time.UnixMilli(principal.Updated), + } +} From 7fba76abf083e8125e4db92d59baae96b13c2bd9 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 19:45:25 -0700 Subject: [PATCH 6/9] add common methods --- .../testdata/webhooks/branch_create.json.golden | 8 ++++---- .../webhooks/pull_request_branch_updated.json.golden | 4 ++-- .../webhooks/pull_request_comment_created.json | 1 + .../webhooks/pull_request_comment_created.json.golden | 5 +++-- .../testdata/webhooks/pull_request_opened.json.golden | 4 ++-- .../webhooks/pull_request_reopened.json.golden | 4 ++-- scm/driver/harness/webhook.go | 10 +++++----- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/scm/driver/harness/testdata/webhooks/branch_create.json.golden b/scm/driver/harness/testdata/webhooks/branch_create.json.golden index e3c4954ab..a96f2ae8e 100644 --- a/scm/driver/harness/testdata/webhooks/branch_create.json.golden +++ b/scm/driver/harness/testdata/webhooks/branch_create.json.golden @@ -3,15 +3,15 @@ "Before": "0000000000000000000000000000000000000000", "After": "aeafa0e2e4ec6909ad75cb8fad57c0b1eb5986e6", "Repo": { - "ID": "", + "ID": "13", "Namespace": "", "Name": "aba", "Perm": null, - "Branch": "", + "Branch": "main", "Private": false, - "Clone": "", + "Clone": "http://localhost:3000/git/kmpySmUISimoRrJL6NL73w/myOrg/myProject/aba.git", "CloneSSH": "", - "Link": "", + "Link": "http://localhost:3000/git/kmpySmUISimoRrJL6NL73w/myOrg/myProject/aba.git", "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" }, diff --git a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden index d0fcc8676..360df39eb 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden @@ -1,9 +1,9 @@ { "Action": "updated", "Repo": { - "ID": "aba", + "ID": "13", "Namespace": "", - "Name": "", + "Name": "aba", "Branch": "main", "Private": false, "Clone": "http://localhost:3000/git/kmpySmUISimoRrJL6NL73w/myOrg/myProject/aba.git", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json index 408f690fc..3b39a54fd 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json @@ -57,6 +57,7 @@ } }, "comment": { + "id": 1, "text": "pr comment" } } \ No newline at end of file diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden index 7ca477eca..feec71788 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden @@ -1,8 +1,8 @@ { "Repo": { - "ID": "demo", + "ID": "18", "Namespace": "", - "Name": "", + "Name": "demo", "Branch": "main", "Private": false, "Clone": "http://localhost:3000/git/asd/demo.git", @@ -36,6 +36,7 @@ "Updated": "0001-01-01T00:00:00Z" }, "Comment": { + "ID": 1, "Body": "pr comment" }, "Sender": { diff --git a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden index 8066564c2..5cd122d3b 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden @@ -1,9 +1,9 @@ { "Action": "created", "Repo": { - "ID": "aba", + "ID": "13", "Namespace": "", - "Name": "", + "Name": "aba", "Branch": "main", "Private": false, "Clone": "http://localhost:3000/git/kmpySmUISimoRrJL6NL73w/myOrg/myProject/aba.git", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden index 6eb93b97d..1d2aca43d 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden @@ -1,9 +1,9 @@ { "Action": "reopened", "Repo": { - "ID": "aba", + "ID": "13", "Namespace": "", - "Name": "", + "Name": "aba", "Branch": "main", "Private": false, "Clone": "http://localhost:3000/git/kmpySmUISimoRrJL6NL73w/myOrg/myProject/aba.git", diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index 472390b72..de085b9ea 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -10,6 +10,7 @@ import ( "io" "io/ioutil" "net/http" + "strconv" "time" "github.com/drone/go-scm/scm" @@ -167,8 +168,8 @@ type ( } `json:"committer"` } comment struct { - Text string `json:"text"` ID int `json:"id"` + Text string `json:"text"` } // harness pull request webhook payload pullRequestHook struct { @@ -224,9 +225,7 @@ func convertPushHook(src *pushHook) *scm.PushHook { Ref: src.Sha, Before: src.OldSha, After: src.Sha, - Repo: scm.Repository{ - Name: src.Repo.UID, - }, + Repo: convertRepo(src.Repo), Commit: scm.Commit{ Sha: src.Commit.Sha, Message: src.Commit.Message, @@ -281,7 +280,8 @@ func convertPullReq(pr pullReq, ref ref, commit hookCommit) scm.PullRequest { func convertRepo(repo repo) scm.Repository { return scm.Repository{ - ID: repo.UID, + ID: strconv.Itoa(repo.ID), + Name: repo.UID, Branch: repo.DefaultBranch, Link: repo.GitURL, Clone: repo.GitURL, From bdb608c6d46de88dccc3e0e5cc3fd64d81bb389a Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 20:18:02 -0700 Subject: [PATCH 7/9] add common methods --- .../harness/testdata/webhooks/branch_create.json.golden | 2 +- .../testdata/webhooks/pull_request_branch_updated.json.golden | 4 ++-- .../webhooks/pull_request_comment_created.json.golden | 4 ++-- .../harness/testdata/webhooks/pull_request_opened.json.golden | 4 ++-- .../testdata/webhooks/pull_request_reopened.json.golden | 4 ++-- scm/driver/harness/webhook.go | 1 + 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/scm/driver/harness/testdata/webhooks/branch_create.json.golden b/scm/driver/harness/testdata/webhooks/branch_create.json.golden index a96f2ae8e..0b0dad736 100644 --- a/scm/driver/harness/testdata/webhooks/branch_create.json.golden +++ b/scm/driver/harness/testdata/webhooks/branch_create.json.golden @@ -36,7 +36,7 @@ }, "Sender": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "default", "Email": "default@harness.io", "Avatar": "", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden index 360df39eb..05c42eb0f 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_branch_updated.json.golden @@ -26,7 +26,7 @@ "Merged": false, "Author": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "Admin", "Email": "admin@harness.io", "Avatar": "", @@ -38,7 +38,7 @@ }, "Sender": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "default", "Email": "default@harness.io", "Avatar": "", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden index feec71788..ed14715f7 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_comment_created.json.golden @@ -25,7 +25,7 @@ "Merged": false, "Author": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "Admin", "Email": "admin@harness.io", "Avatar": "", @@ -41,7 +41,7 @@ }, "Sender": { "ID": "admin", - "Login": "", + "Login": "admin", "Name": "Administrator", "Email": "admin@gitness.io", "Avatar": "", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden index 5cd122d3b..94b221212 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_opened.json.golden @@ -26,7 +26,7 @@ "Merged": false, "Author": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "Admin", "Email": "admin@harness.io", "Avatar": "", @@ -38,7 +38,7 @@ }, "Sender": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "default", "Email": "default@harness.io", "Avatar": "", diff --git a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden index 1d2aca43d..56968a689 100644 --- a/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden +++ b/scm/driver/harness/testdata/webhooks/pull_request_reopened.json.golden @@ -26,7 +26,7 @@ "Merged": false, "Author": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "Admin", "Email": "admin@harness.io", "Avatar": "", @@ -38,7 +38,7 @@ }, "Sender": { "ID": "0osgWsTZRsSZ8RWfjLRkEg", - "Login": "", + "Login": "0osgWsTZRsSZ8RWfjLRkEg", "Name": "default", "Email": "default@harness.io", "Avatar": "", diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index de085b9ea..94b3e11e5 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -292,6 +292,7 @@ func convertUser(principal principal) scm.User { return scm.User{ Name: principal.DisplayName, ID: principal.UID, + Login: principal.UID, Email: principal.Email, Created: time.UnixMilli(principal.Created), Updated: time.UnixMilli(principal.Updated), From 7f8323f910c83214596bb1500ae2434cf7210e3f Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 20:33:03 -0700 Subject: [PATCH 8/9] vet --- scm/driver/harness/webhook.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index 94b3e11e5..8fbbcf2be 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -294,7 +294,7 @@ func convertUser(principal principal) scm.User { ID: principal.UID, Login: principal.UID, Email: principal.Email, - Created: time.UnixMilli(principal.Created), - Updated: time.UnixMilli(principal.Updated), + Created: time.Unix(0, principal.Created*int64(time.Millisecond)), + Updated: time.Unix(0, principal.Updated*int64(time.Millisecond)), } } From c943526fbc0c86b6ae7091b2c96e024ee089515d Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Tue, 24 Oct 2023 20:41:28 -0700 Subject: [PATCH 9/9] rerun --- scm/driver/harness/webhook.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/scm/driver/harness/webhook.go b/scm/driver/harness/webhook.go index 8fbbcf2be..1c63738aa 100644 --- a/scm/driver/harness/webhook.go +++ b/scm/driver/harness/webhook.go @@ -207,10 +207,7 @@ type ( } ) -// // native data structure conversion -// - func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook { return &scm.PullRequestHook{ Action: convertAction(src.Trigger),