move regexp to package level variable, add check for email array

This commit is contained in:
Grzegorz Graczyk 2014-10-22 09:37:04 +02:00
parent 25b887c059
commit 79e0c7a84f

View file

@ -18,6 +18,11 @@ const (
DefaultURL = "https://bitbucket.org" DefaultURL = "https://bitbucket.org"
) )
var (
// bitbucket returns commit author email only in format "John Doe <john.doe@example.com>"
emailRegexp = regexp.MustCompile("<(.*)>")
)
type Bitbucket struct { type Bitbucket struct {
URL string URL string
API string API string
@ -247,9 +252,13 @@ func (r *Bitbucket) ParseHook(req *http.Request) (*model.Hook, error) {
return nil, fmt.Errorf("Invalid Bitbucket post-commit Hook. Missing Repo or Commit data.") return nil, fmt.Errorf("Invalid Bitbucket post-commit Hook. Missing Repo or Commit data.")
} }
// bitbucket returns commit author email only in format "John Doe <john.doe@example.com>" rawAuthor := hook.Commits[len(hook.Commits)-1].RawAuthor
regexp, _ := regexp.Compile("<(.*)>") email := rawAuthor
email := regexp.FindStringSubmatch(hook.Commits[len(hook.Commits)-1].RawAuthor)[1] match := emailRegexp.FindStringSubmatch(rawAuthor)
if len(match) > 0 {
email = match[1]
}
return &model.Hook{ return &model.Hook{
Owner: hook.Repo.Owner, Owner: hook.Repo.Owner,