feat: Add pr_comment webhook for harness (#280)

pull/281/head
Abhinav Singh 7 months ago committed by GitHub
parent 6982ab89b5
commit a5db7f41d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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"
},
@ -35,9 +35,12 @@
"Link": ""
},
"Sender": {
"Login": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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"
}
}

@ -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",

@ -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",
@ -25,18 +25,24 @@
"Closed": false,
"Merged": false,
"Author": {
"Login": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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": {
"Login": "",
"Name": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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"
}
}

@ -0,0 +1,63 @@
{
"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,
"author": {
"id": 8,
"uid": "0osgWsTZRsSZ8RWfjLRkEg",
"display_name": "Admin",
"email": "admin@harness.io",
"type": "user",
"created": 1675390885380,
"updated": 1675390885380
}
},
"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": {
"id": 1,
"text": "pr comment"
}
}

@ -0,0 +1,51 @@
{
"Repo": {
"ID": "18",
"Namespace": "",
"Name": "demo",
"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": {
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"Name": "Admin",
"Email": "admin@harness.io",
"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"
},
"Comment": {
"ID": 1,
"Body": "pr comment"
},
"Sender": {
"ID": "admin",
"Login": "admin",
"Name": "Administrator",
"Email": "admin@gitness.io",
"Avatar": "",
"Created": "2023-10-03T04:20:21.613-07:00",
"Updated": "2023-10-03T04:20:21.613-07:00"
}
}

@ -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",

@ -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",
@ -25,18 +25,24 @@
"Closed": false,
"Merged": false,
"Author": {
"Login": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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": {
"Login": "",
"Name": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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"
}
}

@ -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",

@ -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",
@ -25,18 +25,24 @@
"Closed": false,
"Merged": false,
"Author": {
"Login": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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": {
"Login": "",
"Name": "",
"ID": "0osgWsTZRsSZ8RWfjLRkEg",
"Login": "0osgWsTZRsSZ8RWfjLRkEg",
"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"
}
}

@ -10,6 +10,8 @@ import (
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/hmac"
@ -39,6 +41,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 +93,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 {
@ -117,6 +127,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"`
@ -156,6 +167,10 @@ type (
When string `json:"when"`
} `json:"committer"`
}
comment struct {
ID int `json:"id"`
Text string `json:"text"`
}
// harness pull request webhook payload
pullRequestHook struct {
Trigger string `json:"trigger"`
@ -178,61 +193,57 @@ 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"`
Sha string `json:"sha"`
Commit hookCommit `json:"commit"`
Comment comment `json:"comment"`
}
)
//
// 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.Commit.Committer.Identity.Name,
Email: dst.Commit.Committer.Identity.Email,
},
},
Repo: scm.Repository{
ID: dst.Repo.UID,
Branch: dst.Repo.DefaultBranch,
Link: dst.Repo.GitURL,
Clone: dst.Repo.GitURL,
},
Sender: scm.User{
Email: dst.Principal.Email,
},
Action: convertAction(src.Trigger),
PullRequest: convertPullReq(src.PullReq, src.Ref, src.Commit),
Repo: convertRepo(src.Repo),
Sender: convertUser(src.Principal),
}
}
func convertPushHook(dst *pushHook) *scm.PushHook {
func convertPushHook(src *pushHook) *scm.PushHook {
return &scm.PushHook{
Ref: dst.Sha,
Before: dst.OldSha,
After: dst.Sha,
Repo: scm.Repository{
Name: dst.Repo.UID,
},
Ref: src.Sha,
Before: src.OldSha,
After: src.Sha,
Repo: convertRepo(src.Repo),
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,
Sender: convertUser(src.Principal),
}
}
func convertPullRequestCommentHook(src *pullRequestCommentHook) *scm.PullRequestCommentHook {
return &scm.PullRequestCommentHook{
PullRequest: convertPullReq(src.PullReq, src.Ref, src.Commit),
Repo: convertRepo(src.Repo),
Comment: scm.Comment{
Body: src.Comment.Text,
ID: src.Comment.ID,
},
Sender: convertUser(src.Principal),
}
}
@ -248,3 +259,39 @@ 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: convertUser(pr.Author),
}
}
func convertRepo(repo repo) scm.Repository {
return scm.Repository{
ID: strconv.Itoa(repo.ID),
Name: repo.UID,
Branch: repo.DefaultBranch,
Link: repo.GitURL,
Clone: repo.GitURL,
}
}
func convertUser(principal principal) scm.User {
return scm.User{
Name: principal.DisplayName,
ID: principal.UID,
Login: principal.UID,
Email: principal.Email,
Created: time.Unix(0, principal.Created*int64(time.Millisecond)),
Updated: time.Unix(0, principal.Updated*int64(time.Millisecond)),
}
}

@ -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 {

Loading…
Cancel
Save