Merge pull request #606 from bradrydzewski/master
only generate repo token or key if not exists
This commit is contained in:
commit
063f1f4830
2 changed files with 22 additions and 18 deletions
|
@ -18,10 +18,9 @@ const (
|
||||||
DefaultURL = "https://bitbucket.org"
|
DefaultURL = "https://bitbucket.org"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// parses an email address from string format
|
||||||
// bitbucket returns commit author email only in format "John Doe <john.doe@example.com>"
|
// `John Doe <john.doe@example.com>`
|
||||||
emailRegexp = regexp.MustCompile("<(.*)>")
|
var emailRegexp = regexp.MustCompile("<(.*)>")
|
||||||
)
|
|
||||||
|
|
||||||
type Bitbucket struct {
|
type Bitbucket struct {
|
||||||
URL string
|
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.")
|
return nil, fmt.Errorf("Invalid Bitbucket post-commit Hook. Missing Repo or Commit data.")
|
||||||
}
|
}
|
||||||
|
|
||||||
rawAuthor := hook.Commits[len(hook.Commits)-1].RawAuthor
|
var author = hook.Commits[len(hook.Commits)-1].RawAuthor
|
||||||
email := rawAuthor
|
var matches = emailRegexp.FindStringSubmatch(author)
|
||||||
match := emailRegexp.FindStringSubmatch(rawAuthor)
|
if len(matches) == 2 {
|
||||||
|
author = matches[1]
|
||||||
if len(match) > 0 {
|
|
||||||
email = match[1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.Hook{
|
return &model.Hook{
|
||||||
|
@ -265,7 +262,7 @@ func (r *Bitbucket) ParseHook(req *http.Request) (*model.Hook, error) {
|
||||||
Repo: hook.Repo.Name,
|
Repo: hook.Repo.Name,
|
||||||
Sha: hook.Commits[len(hook.Commits)-1].Hash,
|
Sha: hook.Commits[len(hook.Commits)-1].Hash,
|
||||||
Branch: hook.Commits[len(hook.Commits)-1].Branch,
|
Branch: hook.Commits[len(hook.Commits)-1].Branch,
|
||||||
Author: email,
|
Author: author,
|
||||||
Timestamp: time.Now().UTC().String(),
|
Timestamp: time.Now().UTC().String(),
|
||||||
Message: hook.Commits[len(hook.Commits)-1].Message,
|
Message: hook.Commits[len(hook.Commits)-1].Message,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -56,6 +56,7 @@ func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||||
repo.Active = false
|
repo.Active = false
|
||||||
repo.PullRequest = false
|
repo.PullRequest = false
|
||||||
repo.PostCommit = false
|
repo.PostCommit = false
|
||||||
|
repo.UserID = 0
|
||||||
|
|
||||||
if err := datastore.PutRepo(ctx, repo); err != nil {
|
if err := datastore.PutRepo(ctx, repo); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
@ -80,9 +81,14 @@ func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) {
|
||||||
repo.PostCommit = true
|
repo.PostCommit = true
|
||||||
repo.UserID = user.ID
|
repo.UserID = user.ID
|
||||||
repo.Timeout = 3600 // default to 1 hour
|
repo.Timeout = 3600 // default to 1 hour
|
||||||
|
|
||||||
|
// generate a secret key for post-commit hooks
|
||||||
|
if len(repo.Token) == 0 {
|
||||||
repo.Token = model.GenerateToken()
|
repo.Token = model.GenerateToken()
|
||||||
|
}
|
||||||
|
|
||||||
// generates the rsa key
|
// generates the rsa key
|
||||||
|
if len(repo.PublicKey) == 0 || len(repo.PrivateKey) == 0 {
|
||||||
key, err := sshutil.GeneratePrivateKey()
|
key, err := sshutil.GeneratePrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
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.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey)
|
||||||
repo.PrivateKey = sshutil.MarshalPrivateKey(key)
|
repo.PrivateKey = sshutil.MarshalPrivateKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
var remote = remote.Lookup(repo.Host)
|
var remote = remote.Lookup(repo.Host)
|
||||||
if remote == nil {
|
if remote == nil {
|
||||||
|
|
Loading…
Reference in a new issue