Skip to content

Commit

Permalink
enable deploy hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Feb 15, 2019
1 parent a5005d5 commit f5fde5b
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
152 changes: 152 additions & 0 deletions scm/driver/github/testdata/webhooks/deployment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"deployment": {
"url": "https://api.github.com/repos/Codertocat/Hello-World/deployments/87972451",
"id": 87972451,
"node_id": "MDEwOkRlcGxveW1lbnQ4Nzk3MjQ1MQ==",
"sha": "a10867b14bb761a232cd80139fbd4c0d33264240",
"ref": "master",
"task": "deploy",
"payload": {
"foo": "bar"
},
"environment": "production",
"description": "this is a description",
"creator": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2018-05-30T20:18:45Z",
"updated_at": "2018-05-30T20:18:45Z",
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments/87972451/statuses",
"repository_url": "https://api.github.com/repos/Codertocat/Hello-World"
},
"repository": {
"id": 135493233,
"node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=",
"name": "Hello-World",
"full_name": "Codertocat/Hello-World",
"owner": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/Codertocat/Hello-World",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/Codertocat/Hello-World",
"forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks",
"keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/events",
"assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges",
"archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments",
"created_at": "2018-05-30T20:18:04Z",
"updated_at": "2018-05-30T20:18:35Z",
"pushed_at": "2018-05-30T20:18:44Z",
"git_url": "git://github.com/Codertocat/Hello-World.git",
"ssh_url": "[email protected]:Codertocat/Hello-World.git",
"clone_url": "https://github.com/Codertocat/Hello-World.git",
"svn_url": "https://github.com/Codertocat/Hello-World",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"open_issues_count": 2,
"license": null,
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
}
}
35 changes: 35 additions & 0 deletions scm/driver/github/testdata/webhooks/deployment.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Data": {
"foo": "bar"
},
"Desc": "this is a description",
"Ref": {
"Name": "master",
"Path": "refs/heads/master",
"Sha": "a10867b14bb761a232cd80139fbd4c0d33264240"
},
"Repo": {
"ID": "135493233",
"Namespace": "Codertocat",
"Name": "Hello-World",
"Perm": null,
"Branch": "master",
"Private": false,
"Clone": "https://github.com/Codertocat/Hello-World.git",
"CloneSSH": "[email protected]:Codertocat/Hello-World.git",
"Link": "https://github.com/Codertocat/Hello-World",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Sender": {
"Login": "Codertocat",
"Name": "",
"Email": "",
"Avatar": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Target": "production",
"TargetURL": "",
"Task": "deploy"
}
66 changes: 66 additions & 0 deletions scm/driver/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"io"
"io/ioutil"
"net/http"
"regexp"

"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/driver/internal/hmac"
"github.com/drone/go-scm/scm/driver/internal/null"
)

type webhookService struct {
Expand All @@ -37,6 +39,8 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
hook, err = s.parseDeleteHook(data)
case "pull_request":
hook, err = s.parsePullRequestHook(data)
case "deployment":
hook, err = s.parseDeploymentHook(data)
// case "pull_request_review_comment":
// case "issues":
// case "issue_comment":
Expand Down Expand Up @@ -103,6 +107,16 @@ func (s *webhookService) parseDeleteHook(data []byte) (scm.Webhook, error) {
return dst, nil
}

func (s *webhookService) parseDeploymentHook(data []byte) (scm.Webhook, error) {
src := new(deploymentHook)
err := json.Unmarshal(data, src)
if err != nil {
return nil, err
}
dst := convertDeploymentHook(src)
return dst, nil
}

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

// github deployment webhook payload
deploymentHook struct {
Deployment struct {
Creator user `json:"creator"`
Description null.String `json:"description"`
Environment null.String `json:"environment"`
EnvironmentURL null.String `json:"environment_url"`
Sha null.String `json:"sha"`
Ref null.String `json:"ref"`
Task null.String `json:"task"`
Payload interface{} `json:"payload"`
} `json:"deployment"`
Repository repository `json:"repository"`
Sender user `json:"sender"`
}
)

//
Expand Down Expand Up @@ -325,3 +355,39 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook {
Sender: *convertUser(&src.Sender),
}
}

func convertDeploymentHook(src *deploymentHook) *scm.DeployHook {
dst := &scm.DeployHook{
Data: src.Deployment.Payload,
Desc: src.Deployment.Description.String,
Ref: scm.Reference{
Name: src.Deployment.Ref.String,
Path: src.Deployment.Ref.String,
Sha: src.Deployment.Sha.String,
},
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,
Clone: src.Repository.CloneURL,
CloneSSH: src.Repository.SSHURL,
Link: src.Repository.HTMLURL,
},
Sender: *convertUser(&src.Sender),
Task: src.Deployment.Task.String,
Target: src.Deployment.Environment.String,
TargetURL: src.Deployment.EnvironmentURL.String,
}
if tagRE.MatchString(dst.Ref.Name) {
dst.Ref.Path = scm.ExpandRef(dst.Ref.Path, "refs/tags/")
} else {
dst.Ref.Path = scm.ExpandRef(dst.Ref.Path, "refs/heads/")
}
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+).(.+)")
7 changes: 7 additions & 0 deletions scm/driver/github/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ func TestWebhooks(t *testing.T) {
after: "testdata/webhooks/pr_unlabeled.json.golden",
obj: new(scm.PullRequestHook),
},
// deployment
{
event: "deployment",
before: "testdata/webhooks/deployment.json",
after: "testdata/webhooks/deployment.json.golden",
obj: new(scm.DeployHook),
},
}

for _, test := range tests {
Expand Down
14 changes: 14 additions & 0 deletions scm/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ type (
Review Review
}

// DeployHook represents a deployment event. This is
// currently a GitHub-specific event type.
DeployHook struct {
Data interface{}
Desc string
Ref Reference
Repo Repository
Sender User
Target string
TargetURL string
Task string
}

// SecretFunc provides the Webhook parser with the
// secret key used to validate webhook authenticity.
SecretFunc func(webhook Webhook) (string, error)
Expand All @@ -118,6 +131,7 @@ type (

func (h *PushHook) Repository() Repository { return h.Repo }
func (h *BranchHook) Repository() Repository { return h.Repo }
func (h *DeployHook) Repository() Repository { return h.Repo }
func (h *TagHook) Repository() Repository { return h.Repo }
func (h *IssueHook) Repository() Repository { return h.Repo }
func (h *IssueCommentHook) Repository() Repository { return h.Repo }
Expand Down

0 comments on commit f5fde5b

Please sign in to comment.