Skip to content

Commit

Permalink
use push hooks for branch and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Nov 4, 2018
1 parent 3622865 commit ef91064
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
25 changes: 20 additions & 5 deletions scm/driver/gitlab/testdata/webhooks/tag_create.json.golden
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"Ref": {
"Name": "v1.0.0",
"Sha": "2adc9465c4edfc33834e173fe89436a7cb899a1d"
},
"Ref": "refs/tags/v1.0.0",
"Repo": {
"ID": "4861503",
"Namespace": "sytses",
Expand All @@ -16,7 +13,25 @@
"Created": "0001-01-01T00:00:00Z",
"Updated": "0001-01-01T00:00:00Z"
},
"Action": "created",
"Commit": {
"Sha": "2adc9465c4edfc33834e173fe89436a7cb899a1d",
"Message": "added readme\n",
"Author": {
"Name": "Sid Sijbrandij",
"Email": "[email protected]",
"Date": "0001-01-01T00:00:00Z",
"Login": "sytses",
"Avatar": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87?s=80\u0026d=identicon"
},
"Committer": {
"Name": "Sid Sijbrandij",
"Email": "[email protected]",
"Date": "0001-01-01T00:00:00Z",
"Login": "sytses",
"Avatar": "https://secure.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87?s=80\u0026d=identicon"
},
"Link": "https://gitlab.com/gitlab-org/hello-world/commit/2adc9465c4edfc33834e173fe89436a7cb899a1d"
},
"Sender": {
"Login": "sytses",
"Name": "Sid Sijbrandij",
Expand Down
22 changes: 15 additions & 7 deletions scm/driver/gitlab/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo

var hook scm.Webhook
switch req.Header.Get("X-Gitlab-Event") {
case "Push Hook":
case "Push Hook", "Tag Push Hook":
hook, err = parsePushHook(data)
case "Issue Hook":
return nil, scm.ErrUnknownEvent
case "Merge Request Hook":
hook, err = parsePullRequestHook(data)
case "Tag Push Hook":
return nil, scm.ErrUnknownEvent
default:
return nil, scm.ErrUnknownEvent
}
Expand Down Expand Up @@ -75,7 +73,12 @@ func parsePushHook(data []byte) (scm.Webhook, error) {
return convertPushHook(src), nil
case src.ObjectKind == "push" && src.After == "0000000000000000000000000000000000000000":
return converBranchHook(src), nil
case src.ObjectKind == "tag_push":
case src.ObjectKind == "tag_push" && src.Before == "0000000000000000000000000000000000000000":
// TODO we previously considered returning a
// tag creation hook, however, the push hook
// returns more metadata (commit details).
return convertPushHook(src), nil
case src.ObjectKind == "tag_push" && src.After == "0000000000000000000000000000000000000000":
return convertTagHook(src), nil
default:
return convertPushHook(src), nil
Expand All @@ -101,7 +104,7 @@ func parsePullRequestHook(data []byte) (scm.Webhook, error) {
}

func convertPushHook(src *pushHook) *scm.PushHook {
return &scm.PushHook{
dst := &scm.PushHook{
Ref: scm.ExpandRef(src.Ref, "refs/heads/"),
Repo: scm.Repository{
ID: strconv.Itoa(src.Project.ID),
Expand All @@ -115,7 +118,7 @@ func convertPushHook(src *pushHook) *scm.PushHook {
},
Commit: scm.Commit{
Sha: src.CheckoutSha,
Message: src.Commits[0].Message, // TODO need safe option
Message: "", // NOTE this is set below
Author: scm.Signature{
Login: src.UserUsername,
Name: src.UserName,
Expand All @@ -128,7 +131,7 @@ func convertPushHook(src *pushHook) *scm.PushHook {
Email: src.UserEmail,
Avatar: src.UserAvatar,
},
Link: src.Commits[0].URL, // TODO need safe option
Link: "", // NOTE this is set below
},
Sender: scm.User{
Login: src.UserUsername,
Expand All @@ -137,6 +140,11 @@ func convertPushHook(src *pushHook) *scm.PushHook {
Avatar: src.UserAvatar,
},
}
if len(src.Commits) > 0 {
dst.Commit.Message = src.Commits[0].Message
dst.Commit.Link = src.Commits[0].URL
}
return dst
}

func converBranchHook(src *pushHook) *scm.BranchHook {
Expand Down
4 changes: 2 additions & 2 deletions scm/driver/gitlab/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func TestWebhooks(t *testing.T) {
},
// tag hooks
{
event: "Push Hook",
event: "Tag Push Hook",
before: "testdata/webhooks/tag_create.json",
after: "testdata/webhooks/tag_create.json.golden",
obj: new(scm.TagHook),
obj: new(scm.PushHook),
},
{
event: "Push Hook",
Expand Down
2 changes: 1 addition & 1 deletion scm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TrimRef(ref string) string {
// reference path (e.g refs/heads/master).
func ExpandRef(name, prefix string) string {
prefix = strings.TrimSuffix(prefix, "/")
if strings.HasPrefix(name, prefix) {
if strings.HasPrefix(name, "refs/") {
return name
}
return prefix + "/" + name
Expand Down
6 changes: 6 additions & 0 deletions scm/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func TestExpandRef(t *testing.T) {
name: "master",
prefix: "refs/heads/",
},
// is already a ref
{
after: "refs/tags/v1.0.0",
name: "refs/tags/v1.0.0",
prefix: "refs/heads/",
},
}
for _, test := range tests {
if got, want := ExpandRef(test.name, test.prefix), test.after; got != want {
Expand Down

0 comments on commit ef91064

Please sign in to comment.