only generate repo token or key if not exists

This commit is contained in:
Brad Rydzewski 2014-10-22 01:02:14 -07:00
parent 273755d2a0
commit 4f30e1388c
2 changed files with 22 additions and 18 deletions

View file

@ -18,10 +18,9 @@ const (
DefaultURL = "https://bitbucket.org"
)
var (
// bitbucket returns commit author email only in format "John Doe <john.doe@example.com>"
emailRegexp = regexp.MustCompile("<(.*)>")
)
// parses an email address from string format
// `John Doe <john.doe@example.com>`
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

View file

@ -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,9 +81,14 @@ 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
// generate a secret key for post-commit hooks
if len(repo.Token) == 0 {
repo.Token = model.GenerateToken()
}
// generates the rsa key
if len(repo.PublicKey) == 0 || len(repo.PrivateKey) == 0 {
key, err := sshutil.GeneratePrivateKey()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@ -90,6 +96,7 @@ func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) {
}
repo.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey)
repo.PrivateKey = sshutil.MarshalPrivateKey(key)
}
var remote = remote.Lookup(repo.Host)
if remote == nil {