From e919be521083ad1aec2f06598a6aec89146ea72b Mon Sep 17 00:00:00 2001 From: Patrick Jahns Date: Fri, 25 Nov 2016 22:37:58 +0100 Subject: [PATCH 1/2] extract email for bitbucket push requests from raw git commit --- remote/bitbucket/convert.go | 14 ++++++++++++++ remote/bitbucket/convert_test.go | 2 ++ remote/bitbucket/fixtures/hooks.go | 1 + 3 files changed, 17 insertions(+) diff --git a/remote/bitbucket/convert.go b/remote/bitbucket/convert.go index 6963a3e21..dc4e0437e 100644 --- a/remote/bitbucket/convert.go +++ b/remote/bitbucket/convert.go @@ -2,6 +2,7 @@ package bitbucket import ( "fmt" + "regexp" "net/url" "strings" @@ -186,5 +187,18 @@ func convertPushHook(hook *internal.PushHook, change *internal.Change) *model.Bu build.Event = model.EventPush build.Ref = fmt.Sprintf("refs/heads/%s", change.New.Name) } + if len(change.New.Target.Author.Raw) != 0 { + build.Email = extractEmail(change.New.Target.Author.Raw) + } return build } + +// extracts the email from a git commit author string +func extractEmail(gitauthor string) (author string) { + re := regexp.MustCompile("<(.*)>") + matches := re.FindAllStringSubmatch(gitauthor,-1) + if len(matches) == 1 { + author = matches[0][1] + } + return +} diff --git a/remote/bitbucket/convert_test.go b/remote/bitbucket/convert_test.go index 2a91041ba..50b552374 100644 --- a/remote/bitbucket/convert_test.go +++ b/remote/bitbucket/convert_test.go @@ -166,6 +166,7 @@ func Test_helper(t *testing.T) { change.New.Target.Links.Html.Href = "https://bitbucket.org/foo/bar/commits/73f9c44d" change.New.Target.Message = "updated README" change.New.Target.Date = time.Now() + change.New.Target.Author.Raw = "Test " hook := internal.PushHook{} hook.Actor.Login = "octocat" @@ -173,6 +174,7 @@ func Test_helper(t *testing.T) { build := convertPushHook(&hook, &change) g.Assert(build.Event).Equal(model.EventPush) + g.Assert(build.Email).Equal("test@domain.tld") g.Assert(build.Author).Equal(hook.Actor.Login) g.Assert(build.Avatar).Equal(hook.Actor.Links.Avatar.Href) g.Assert(build.Commit).Equal(change.New.Target.Hash) diff --git a/remote/bitbucket/fixtures/hooks.go b/remote/bitbucket/fixtures/hooks.go index 2b68e24e9..2ee0823ad 100644 --- a/remote/bitbucket/fixtures/hooks.go +++ b/remote/bitbucket/fixtures/hooks.go @@ -33,6 +33,7 @@ const HookPush = ` "type": "commit", "hash": "709d658dc5b6d6afcd46049c2f332ee3f515a67d", "author": { + "raw": "emmap1 ", "username": "emmap1", "links": { "avatar": { From 63a0659aa43be8b3e3e86d6e3fe45d2662b1f1ce Mon Sep 17 00:00:00 2001 From: Patrick Jahns Date: Fri, 20 Jan 2017 10:52:17 +0100 Subject: [PATCH 2/2] change regex to global regex --- remote/bitbucket/convert.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/remote/bitbucket/convert.go b/remote/bitbucket/convert.go index dc4e0437e..bc6d9171e 100644 --- a/remote/bitbucket/convert.go +++ b/remote/bitbucket/convert.go @@ -193,10 +193,12 @@ func convertPushHook(hook *internal.PushHook, change *internal.Change) *model.Bu return build } +// regex for git author fields ("name ") +var reGitMail = regexp.MustCompile("<(.*)>") + // extracts the email from a git commit author string func extractEmail(gitauthor string) (author string) { - re := regexp.MustCompile("<(.*)>") - matches := re.FindAllStringSubmatch(gitauthor,-1) + matches := reGitMail.FindAllStringSubmatch(gitauthor,-1) if len(matches) == 1 { author = matches[0][1] }