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

ENH: Added issue_comment parsing for github webhook #91

Merged
merged 5 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
2 changes: 2 additions & 0 deletions scm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
// pull requests
ActionSync
ActionMerge
// issue comment
ActionEdit
)

// String returns the string representation of Action.
Expand Down
219 changes: 219 additions & 0 deletions scm/driver/github/testdata/webhooks/comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"action": "created",
"issue": {
"url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/2",
"repository_url": "https://api.github.com/repos/abc/def-ci-webhook-test",
"labels_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/2/labels{/name}",
"comments_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/2/comments",
"events_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/2/events",
"html_url": "https://github.com/abc/def-ci-webhook-test/pull/2",
"id": 735678167,
"node_id": "MDExOlB1bGxSZXF1ZXN0NTE1MDI2Njgw",
"number": 2,
"title": "Update README.md",
"user": {
"login": "lts-def",
"id": 10278482,
"node_id": "MDQ6VXNlcjEwMjc4NDgy",
"avatar_url": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/lts-def",
"html_url": "https://github.com/lts-def",
"followers_url": "https://api.github.com/users/lts-def/followers",
"following_url": "https://api.github.com/users/lts-def/following{/other_user}",
"gists_url": "https://api.github.com/users/lts-def/gists{/gist_id}",
"starred_url": "https://api.github.com/users/lts-def/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/lts-def/subscriptions",
"organizations_url": "https://api.github.com/users/lts-def/orgs",
"repos_url": "https://api.github.com/users/lts-def/repos",
"events_url": "https://api.github.com/users/lts-def/events{/privacy}",
"received_events_url": "https://api.github.com/users/lts-def/received_events",
"type": "User",
"site_admin": false
},
"labels": [

],
"state": "open",
"locked": false,
"assignee": null,
"assignees": [

],
"milestone": null,
"comments": 4,
"created_at": "2020-11-03T22:32:14Z",
"updated_at": "2020-12-29T05:49:14Z",
"closed_at": null,
"author_association": "COLLABORATOR",
"active_lock_reason": null,
"pull_request": {
"url": "https://api.github.com/repos/abc/def-ci-webhook-test/pulls/2",
"html_url": "https://github.com/abc/def-ci-webhook-test/pull/2",
"diff_url": "https://github.com/abc/def-ci-webhook-test/pull/2.diff",
"patch_url": "https://github.com/abc/def-ci-webhook-test/pull/2.patch"
},
"body": "",
"performed_via_github_app": null
},
"comment": {
"url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/comments/751955664",
"html_url": "https://github.com/abc/def-ci-webhook-test/pull/2#issuecomment-751955664",
"issue_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/2",
"id": 751955664,
"node_id": "MDEyOklzc3VlQ29tbWVudDc1MTk1NTY2NA==",
"user": {
"login": "lts-def",
"id": 10278482,
"node_id": "MDQ6VXNlcjEwMjc4NDgy",
"avatar_url": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/lts-def",
"html_url": "https://github.com/lts-def",
"followers_url": "https://api.github.com/users/lts-def/followers",
"following_url": "https://api.github.com/users/lts-def/following{/other_user}",
"gists_url": "https://api.github.com/users/lts-def/gists{/gist_id}",
"starred_url": "https://api.github.com/users/lts-def/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/lts-def/subscriptions",
"organizations_url": "https://api.github.com/users/lts-def/orgs",
"repos_url": "https://api.github.com/users/lts-def/repos",
"events_url": "https://api.github.com/users/lts-def/events{/privacy}",
"received_events_url": "https://api.github.com/users/lts-def/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2020-12-29T05:49:14Z",
"updated_at": "2020-12-29T05:49:14Z",
"author_association": "COLLABORATOR",
"body": "trigger abc",
"performed_via_github_app": null
},
"repository": {
"id": 309651765,
"node_id": "MDEwOlJlcG9zaXRvcnkzMDk2NTE3NjU=",
"name": "def-ci-webhook-test",
"full_name": "abc/def-ci-webhook-test",
"private": true,
"owner": {
"login": "abc",
"id": 18273000,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE4MjczMDAw",
"avatar_url": "https://avatars1.githubusercontent.com/u/18273000?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/abc",
"html_url": "https://github.com/abc",
"followers_url": "https://api.github.com/users/abc/followers",
"following_url": "https://api.github.com/users/abc/following{/other_user}",
"gists_url": "https://api.github.com/users/abc/gists{/gist_id}",
"starred_url": "https://api.github.com/users/abc/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/abc/subscriptions",
"organizations_url": "https://api.github.com/users/abc/orgs",
"repos_url": "https://api.github.com/users/abc/repos",
"events_url": "https://api.github.com/users/abc/events{/privacy}",
"received_events_url": "https://api.github.com/users/abc/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/abc/def-ci-webhook-test",
"description": "Webhook test",
"fork": false,
"url": "https://api.github.com/repos/abc/def-ci-webhook-test",
"forks_url": "https://api.github.com/repos/abc/def-ci-webhook-test/forks",
"keys_url": "https://api.github.com/repos/abc/def-ci-webhook-test/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/abc/def-ci-webhook-test/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/abc/def-ci-webhook-test/teams",
"hooks_url": "https://api.github.com/repos/abc/def-ci-webhook-test/hooks",
"issue_events_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/events{/number}",
"events_url": "https://api.github.com/repos/abc/def-ci-webhook-test/events",
"assignees_url": "https://api.github.com/repos/abc/def-ci-webhook-test/assignees{/user}",
"branches_url": "https://api.github.com/repos/abc/def-ci-webhook-test/branches{/branch}",
"tags_url": "https://api.github.com/repos/abc/def-ci-webhook-test/tags",
"blobs_url": "https://api.github.com/repos/abc/def-ci-webhook-test/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/abc/def-ci-webhook-test/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/abc/def-ci-webhook-test/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/abc/def-ci-webhook-test/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/abc/def-ci-webhook-test/statuses/{sha}",
"languages_url": "https://api.github.com/repos/abc/def-ci-webhook-test/languages",
"stargazers_url": "https://api.github.com/repos/abc/def-ci-webhook-test/stargazers",
"contributors_url": "https://api.github.com/repos/abc/def-ci-webhook-test/contributors",
"subscribers_url": "https://api.github.com/repos/abc/def-ci-webhook-test/subscribers",
"subscription_url": "https://api.github.com/repos/abc/def-ci-webhook-test/subscription",
"commits_url": "https://api.github.com/repos/abc/def-ci-webhook-test/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/abc/def-ci-webhook-test/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/abc/def-ci-webhook-test/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/abc/def-ci-webhook-test/contents/{+path}",
"compare_url": "https://api.github.com/repos/abc/def-ci-webhook-test/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/abc/def-ci-webhook-test/merges",
"archive_url": "https://api.github.com/repos/abc/def-ci-webhook-test/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/abc/def-ci-webhook-test/downloads",
"issues_url": "https://api.github.com/repos/abc/def-ci-webhook-test/issues{/number}",
"pulls_url": "https://api.github.com/repos/abc/def-ci-webhook-test/pulls{/number}",
"milestones_url": "https://api.github.com/repos/abc/def-ci-webhook-test/milestones{/number}",
"notifications_url": "https://api.github.com/repos/abc/def-ci-webhook-test/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/abc/def-ci-webhook-test/labels{/name}",
"releases_url": "https://api.github.com/repos/abc/def-ci-webhook-test/releases{/id}",
"deployments_url": "https://api.github.com/repos/abc/def-ci-webhook-test/deployments",
"created_at": "2020-11-03T10:36:21Z",
"updated_at": "2020-12-29T05:06:28Z",
"pushed_at": "2020-12-29T05:06:25Z",
"git_url": "git:https://github.com/abc/def-ci-webhook-test.git",
"ssh_url": "[email protected]:abc/def-ci-webhook-test.git",
"clone_url": "https://github.com/abc/def-ci-webhook-test.git",
"svn_url": "https://github.com/abc/def-ci-webhook-test",
"homepage": null,
"size": 338096,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 2,
"license": null,
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "master"
},
"organization": {
"login": "abc",
"id": 18273000,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE4MjczMDAw",
"url": "https://api.github.com/orgs/abc",
"repos_url": "https://api.github.com/orgs/abc/repos",
"events_url": "https://api.github.com/orgs/abc/events",
"hooks_url": "https://api.github.com/orgs/abc/hooks",
"issues_url": "https://api.github.com/orgs/abc/issues",
"members_url": "https://api.github.com/orgs/abc/members{/member}",
"public_members_url": "https://api.github.com/orgs/abc/public_members{/member}",
"avatar_url": "https://avatars1.githubusercontent.com/u/18273000?v=4",
"description": ""
},
"sender": {
"login": "lts-def",
"id": 10278482,
"node_id": "MDQ6VXNlcjEwMjc4NDgy",
"avatar_url": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/lts-def",
"html_url": "https://github.com/lts-def",
"followers_url": "https://api.github.com/users/lts-def/followers",
"following_url": "https://api.github.com/users/lts-def/following{/other_user}",
"gists_url": "https://api.github.com/users/lts-def/gists{/gist_id}",
"starred_url": "https://api.github.com/users/lts-def/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/lts-def/subscriptions",
"organizations_url": "https://api.github.com/users/lts-def/orgs",
"repos_url": "https://api.github.com/users/lts-def/repos",
"events_url": "https://api.github.com/users/lts-def/events{/privacy}",
"received_events_url": "https://api.github.com/users/lts-def/received_events",
"type": "User",
"site_admin": false
}
}
58 changes: 58 additions & 0 deletions scm/driver/github/testdata/webhooks/comment.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"Action": "created",
"Repo": {
"ID": "309651765",
"Namespace": "abc",
"Name": "def-ci-webhook-test",
"Perm": null,
"Branch": "master",
"Private": true,
"Visibility": 0,
"Clone": "https://github.com/abc/def-ci-webhook-test.git",
"CloneSSH": "[email protected]:abc/def-ci-webhook-test.git",
"Link": "https://github.com/abc/def-ci-webhook-test",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Issue": {
"Number": 2,
"Title": "Update README.md",
"Body": "",
"Link": "https://github.com/abc/def-ci-webhook-test/pull/2",
"Labels": null,
"Closed": false,
"Locked": false,
"Author": {
"Login": "lts-def",
"Name": "",
"Email": "",
"Avatar": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Created": "2020-11-03T22:32:14Z",
"Updated": "2020-12-29T05:49:14Z"
},
"Comment": {
"ID": 751955664,
"Body": "trigger abc",
"Author": {
"Login": "lts-def",
"Name": "",
"Email": "",
"Avatar": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Created": "2020-12-29T05:49:14Z",
"Updated": "2020-12-29T05:49:14Z"
},
"Sender": {
"Login": "lts-def",
"Name": "",
"Email": "",
"Avatar": "https://avatars0.githubusercontent.com/u/10278482?v=4",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
}
}
67 changes: 66 additions & 1 deletion scm/driver/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"net/http"
"regexp"
"time"

"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/hmac"
Expand Down Expand Up @@ -43,7 +44,8 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
hook, err = s.parseDeploymentHook(data)
// case "pull_request_review_comment":
// case "issues":
// case "issue_comment":
case "issue_comment":
hook, err = s.parseIssueCommentHook(data)
default:
return nil, scm.ErrUnknownEvent
}
Expand Down Expand Up @@ -117,6 +119,26 @@ func (s *webhookService) parseDeploymentHook(data []byte) (scm.Webhook, error) {
return dst, nil
}

func (s *webhookService) parseIssueCommentHook(data []byte) (scm.Webhook, error) {
src := new(issueCommentHook)
err := json.Unmarshal(data, src)
if err != nil {
return nil, err
}
dst := convertIssueCommentHook(src)
switch src.Action {
case "created":
dst.Action = scm.ActionCreate
case "edited":
dst.Action = scm.ActionEdit
case "deleted":
dst.Action = scm.ActionDelete
default:
dst.Action = scm.ActionUnknown
}
return dst, nil
}

func (s *webhookService) parsePullRequestHook(data []byte) (scm.Webhook, error) {
src := new(pullRequestHook)
err := json.Unmarshal(data, src)
Expand Down Expand Up @@ -260,6 +282,22 @@ type (
Repository repository `json:"repository"`
Sender user `json:"sender"`
}

// github issue_comment webhook payload
issueCommentHook struct {
Action string `json:"action"`
Issue issue `json:"issue"`
Repository repository `json:"repository"`
Sender user `json:"sender"`
Organization user `json:"organization"`
Comment struct {
ID int `json:"id"`
Body string `json:"body"`
Author user `json:"user"`
shubham149 marked this conversation as resolved.
Show resolved Hide resolved
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
} `json:"comment"`
}
)

//
Expand Down Expand Up @@ -433,6 +471,33 @@ func convertDeploymentHook(src *deploymentHook) *scm.DeployHook {
return dst
}

func convertIssueCommentHook(src *issueCommentHook) *scm.IssueCommentHook {
dst := &scm.IssueCommentHook{
// Action: src.Action,
shubham149 marked this conversation as resolved.
Show resolved Hide resolved
Repo: scm.Repository{
ID: fmt.Sprint(src.Repository.ID),
Namespace: src.Repository.Owner.Login,
Name: src.Repository.Name,
Branch: src.Repository.DefaultBranch,
Private: src.Repository.Private,
Visibility: convertVisibility(src.Repository.Visibility),
Clone: src.Repository.CloneURL,
CloneSSH: src.Repository.SSHURL,
Link: src.Repository.HTMLURL,
},
Issue: *convertIssue(&src.Issue),
Comment: scm.Comment{
ID: src.Comment.ID,
Body: src.Comment.Body,
Author: *convertUser(&src.Comment.Author),
Created: src.Comment.Updated,
shubham149 marked this conversation as resolved.
Show resolved Hide resolved
Updated: src.Comment.Updated,
},
Sender: *convertUser(&src.Sender),
}
return dst
}

// regexp help determine if the named git object is a tag.
// this is not meant to be 100% accurate.
var tagRE = regexp.MustCompile("^v?(\\d+).(.+)")
Loading