2016-07-24 21:07:44 +00:00
|
|
|
package bitbucketserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-08-11 20:35:47 +00:00
|
|
|
"time"
|
2016-07-24 21:07:44 +00:00
|
|
|
|
2016-09-01 16:10:35 +00:00
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/remote/bitbucketserver/internal"
|
|
|
|
"github.com/mrjones/oauth"
|
|
|
|
)
|
2016-08-14 02:06:15 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
statusPending = "INPROGRESS"
|
|
|
|
statusSuccess = "SUCCESSFUL"
|
|
|
|
statusFailure = "FAILED"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
descPending = "this build is pending"
|
|
|
|
descSuccess = "the build was successful"
|
|
|
|
descFailure = "the build failed"
|
|
|
|
descError = "oops, something went wrong"
|
|
|
|
)
|
|
|
|
|
|
|
|
// convertStatus is a helper function used to convert a Drone status to a
|
|
|
|
// Bitbucket commit status.
|
|
|
|
func convertStatus(status string) string {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending, model.StatusRunning:
|
|
|
|
return statusPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return statusSuccess
|
|
|
|
default:
|
|
|
|
return statusFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertDesc is a helper function used to convert a Drone status to a
|
|
|
|
// Bitbucket status description.
|
|
|
|
func convertDesc(status string) string {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending, model.StatusRunning:
|
|
|
|
return descPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return descSuccess
|
|
|
|
case model.StatusFailure:
|
|
|
|
return descFailure
|
|
|
|
default:
|
|
|
|
return descError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 21:07:44 +00:00
|
|
|
// convertRepo is a helper function used to convert a Bitbucket server repository
|
|
|
|
// structure to the common Drone repository structure.
|
|
|
|
func convertRepo(from *internal.Repo) *model.Repo {
|
|
|
|
|
|
|
|
repo := model.Repo{
|
|
|
|
Name: from.Slug,
|
|
|
|
Owner: from.Project.Key,
|
|
|
|
Branch: "master",
|
|
|
|
Kind: model.RepoGit,
|
|
|
|
IsPrivate: true, // Since we have to use Netrc it has to always be private :/
|
|
|
|
FullName: fmt.Sprintf("%s/%s", from.Project.Key, from.Slug),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range from.Links.Clone {
|
|
|
|
if item.Name == "http" {
|
|
|
|
uri, err := url.Parse(item.Href)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
uri.User = nil
|
|
|
|
repo.Clone = uri.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, item := range from.Links.Self {
|
|
|
|
if item.Href != "" {
|
|
|
|
repo.Link = item.Href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &repo
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertRepoLite is a helper function used to convert a Bitbucket repository
|
|
|
|
// structure to the simplified Drone repository structure.
|
|
|
|
func convertRepoLite(from *internal.Repo) *model.RepoLite {
|
|
|
|
return &model.RepoLite{
|
|
|
|
Owner: from.Project.Key,
|
|
|
|
Name: from.Slug,
|
|
|
|
FullName: from.Project.Key + "/" + from.Slug,
|
|
|
|
//TODO: find the avatar for the repo
|
|
|
|
//Avatar: might need another ws call?
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPushHook is a helper function used to convert a Bitbucket push
|
|
|
|
// hook to the Drone build struct holding commit information.
|
2016-08-11 20:35:47 +00:00
|
|
|
func convertPushHook(hook *internal.PostHook, baseURL string) *model.Build {
|
2016-09-19 14:19:12 +00:00
|
|
|
branch := strings.TrimPrefix(
|
|
|
|
strings.TrimPrefix(
|
|
|
|
hook.RefChanges[0].RefID,
|
|
|
|
"refs/heads/",
|
|
|
|
),
|
|
|
|
"refs/tags/",
|
|
|
|
)
|
2016-07-24 22:13:50 +00:00
|
|
|
|
2016-08-12 20:14:58 +00:00
|
|
|
//Ensuring the author label is not longer then 40 for the label of the commit author (default size in the db)
|
2016-08-12 21:20:26 +00:00
|
|
|
authorLabel := hook.Changesets.Values[0].ToCommit.Author.Name
|
2016-08-13 06:07:45 +00:00
|
|
|
if len(authorLabel) > 40 {
|
|
|
|
authorLabel = authorLabel[0:37] + "..."
|
2016-08-12 20:14:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 21:07:44 +00:00
|
|
|
build := &model.Build{
|
2016-08-13 06:07:45 +00:00
|
|
|
Commit: hook.RefChanges[0].ToHash, // TODO check for index value
|
2016-09-19 14:19:12 +00:00
|
|
|
Branch: branch,
|
2016-08-11 20:35:47 +00:00
|
|
|
Message: hook.Changesets.Values[0].ToCommit.Message, //TODO check for index Values
|
|
|
|
Avatar: avatarLink(hook.Changesets.Values[0].ToCommit.Author.EmailAddress),
|
2016-08-12 20:14:58 +00:00
|
|
|
Author: authorLabel,
|
2016-08-11 20:35:47 +00:00
|
|
|
Email: hook.Changesets.Values[0].ToCommit.Author.EmailAddress,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
Ref: hook.RefChanges[0].RefID, // TODO check for index Values
|
|
|
|
Link: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, hook.Repository.Project.Key, hook.Repository.Slug, hook.RefChanges[0].ToHash),
|
2016-07-24 22:13:50 +00:00
|
|
|
}
|
2016-09-19 14:19:12 +00:00
|
|
|
if strings.HasPrefix(hook.RefChanges[0].RefID, "refs/tags/") {
|
2016-07-24 22:13:50 +00:00
|
|
|
build.Event = model.EventTag
|
2016-09-19 14:19:12 +00:00
|
|
|
} else {
|
2016-07-24 22:13:50 +00:00
|
|
|
build.Event = model.EventPush
|
2016-07-24 21:07:44 +00:00
|
|
|
}
|
2016-07-24 22:13:50 +00:00
|
|
|
|
2016-07-24 21:07:44 +00:00
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertUser is a helper function used to convert a Bitbucket user account
|
|
|
|
// structure to the Drone User structure.
|
|
|
|
func convertUser(from *internal.User, token *oauth.AccessToken) *model.User {
|
|
|
|
return &model.User{
|
|
|
|
Login: from.Slug,
|
|
|
|
Token: token.Token,
|
|
|
|
Email: from.EmailAddress,
|
|
|
|
Avatar: avatarLink(from.EmailAddress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func avatarLink(email string) string {
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(strings.ToLower(email)))
|
|
|
|
emailHash := fmt.Sprintf("%v", hex.EncodeToString(hasher.Sum(nil)))
|
|
|
|
avatarURL := fmt.Sprintf("https://www.gravatar.com/avatar/%s.jpg", emailHash)
|
|
|
|
return avatarURL
|
|
|
|
}
|