diff --git a/plugin/remote/bitbucket/bitbucket.go b/plugin/remote/bitbucket/bitbucket.go index 534c22f1a..d035889f0 100644 --- a/plugin/remote/bitbucket/bitbucket.go +++ b/plugin/remote/bitbucket/bitbucket.go @@ -18,10 +18,9 @@ const ( DefaultURL = "https://bitbucket.org" ) -var ( - // bitbucket returns commit author email only in format "John Doe " - emailRegexp = regexp.MustCompile("<(.*)>") -) +// parses an email address from string format +// `John Doe ` +var emailRegexp = regexp.MustCompile("<(.*)>") type Bitbucket struct { URL string @@ -252,12 +251,10 @@ func (r *Bitbucket) ParseHook(req *http.Request) (*model.Hook, error) { return nil, fmt.Errorf("Invalid Bitbucket post-commit Hook. Missing Repo or Commit data.") } - rawAuthor := hook.Commits[len(hook.Commits)-1].RawAuthor - email := rawAuthor - match := emailRegexp.FindStringSubmatch(rawAuthor) - - if len(match) > 0 { - email = match[1] + var author = hook.Commits[len(hook.Commits)-1].RawAuthor + var matches = emailRegexp.FindStringSubmatch(author) + if len(matches) == 2 { + author = matches[1] } return &model.Hook{ @@ -265,7 +262,7 @@ func (r *Bitbucket) ParseHook(req *http.Request) (*model.Hook, error) { Repo: hook.Repo.Name, Sha: hook.Commits[len(hook.Commits)-1].Hash, Branch: hook.Commits[len(hook.Commits)-1].Branch, - Author: email, + Author: author, Timestamp: time.Now().UTC().String(), Message: hook.Commits[len(hook.Commits)-1].Message, }, nil diff --git a/server/handler/repo.go b/server/handler/repo.go index be7662437..7e74f02f5 100644 --- a/server/handler/repo.go +++ b/server/handler/repo.go @@ -56,6 +56,7 @@ func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) { repo.Active = false repo.PullRequest = false repo.PostCommit = false + repo.UserID = 0 if err := datastore.PutRepo(ctx, repo); err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -80,16 +81,22 @@ func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) { repo.PostCommit = true repo.UserID = user.ID repo.Timeout = 3600 // default to 1 hour - repo.Token = model.GenerateToken() + + // generate a secret key for post-commit hooks + if len(repo.Token) == 0 { + repo.Token = model.GenerateToken() + } // generates the rsa key - key, err := sshutil.GeneratePrivateKey() - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return + if len(repo.PublicKey) == 0 || len(repo.PrivateKey) == 0 { + key, err := sshutil.GeneratePrivateKey() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + repo.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey) + repo.PrivateKey = sshutil.MarshalPrivateKey(key) } - repo.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey) - repo.PrivateKey = sshutil.MarshalPrivateKey(key) var remote = remote.Lookup(repo.Host) if remote == nil {