2015-10-05 00:40:27 +00:00
|
|
|
package bitbucket
|
|
|
|
|
|
|
|
import (
|
2016-04-30 08:00:39 +00:00
|
|
|
"fmt"
|
2015-10-05 01:34:06 +00:00
|
|
|
"net/url"
|
2017-03-18 08:49:27 +00:00
|
|
|
"regexp"
|
2015-10-05 00:40:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2016-04-29 19:39:56 +00:00
|
|
|
"github.com/drone/drone/remote/bitbucket/internal"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2015-10-05 00:40:27 +00:00
|
|
|
)
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
const (
|
|
|
|
statusPending = "INPROGRESS"
|
|
|
|
statusSuccess = "SUCCESSFUL"
|
|
|
|
statusFailure = "FAILED"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-18 08:49:27 +00:00
|
|
|
descPending = "this build is pending"
|
|
|
|
descSuccess = "the build was successful"
|
|
|
|
descFailure = "the build failed"
|
|
|
|
descBlocked = "the build requires approval"
|
|
|
|
descDeclined = "the build was rejected"
|
|
|
|
descError = "oops, something went wrong"
|
2016-04-30 08:00:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// convertStatus is a helper function used to convert a Drone status to a
|
|
|
|
// Bitbucket commit status.
|
|
|
|
func convertStatus(status string) string {
|
|
|
|
switch status {
|
2017-03-18 08:49:27 +00:00
|
|
|
case model.StatusPending, model.StatusRunning, model.StatusBlocked:
|
2016-04-30 08:00:39 +00:00
|
|
|
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
|
2017-03-18 08:49:27 +00:00
|
|
|
case model.StatusBlocked:
|
|
|
|
return descBlocked
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return descDeclined
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
|
|
|
return descError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// convertRepo is a helper function used to convert a Bitbucket repository
|
|
|
|
// structure to the common Drone repository structure.
|
|
|
|
func convertRepo(from *internal.Repo) *model.Repo {
|
2015-10-05 00:40:27 +00:00
|
|
|
repo := model.Repo{
|
2016-04-29 19:39:56 +00:00
|
|
|
Clone: cloneLink(from),
|
2015-11-26 01:09:57 +00:00
|
|
|
Owner: strings.Split(from.FullName, "/")[0],
|
|
|
|
Name: strings.Split(from.FullName, "/")[1],
|
2015-10-05 00:40:27 +00:00
|
|
|
FullName: from.FullName,
|
|
|
|
Link: from.Links.Html.Href,
|
|
|
|
IsPrivate: from.IsPrivate,
|
|
|
|
Avatar: from.Owner.Links.Avatar.Href,
|
2015-10-27 20:03:37 +00:00
|
|
|
Kind: from.Scm,
|
2015-10-05 00:40:27 +00:00
|
|
|
Branch: "master",
|
|
|
|
}
|
2015-10-27 21:53:55 +00:00
|
|
|
if repo.Kind == model.RepoHg {
|
|
|
|
repo.Branch = "default"
|
|
|
|
}
|
2015-10-05 00:40:27 +00:00
|
|
|
return &repo
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// cloneLink is a helper function that tries to extract the clone url from the
|
|
|
|
// repository object.
|
|
|
|
func cloneLink(repo *internal.Repo) string {
|
2015-10-06 06:17:59 +00:00
|
|
|
var clone string
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// above we manually constructed the repository clone url. below we will
|
|
|
|
// iterate through the list of clone links and attempt to instead use the
|
|
|
|
// clone url provided by bitbucket.
|
2015-10-06 06:17:59 +00:00
|
|
|
for _, link := range repo.Links.Clone {
|
|
|
|
if link.Name == "https" {
|
|
|
|
clone = link.Href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// if no repository name is provided, we use the Html link. this excludes the
|
|
|
|
// .git suffix, but will still clone the repo.
|
2015-10-06 06:17:59 +00:00
|
|
|
if len(clone) == 0 {
|
|
|
|
clone = repo.Links.Html.Href
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// if bitbucket tries to automatically populate the user in the url we must
|
|
|
|
// strip it out.
|
2015-10-06 06:17:59 +00:00
|
|
|
cloneurl, err := url.Parse(clone)
|
|
|
|
if err == nil {
|
|
|
|
cloneurl.User = nil
|
|
|
|
clone = cloneurl.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// 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 {
|
2015-10-05 00:40:27 +00:00
|
|
|
return &model.RepoLite{
|
2015-11-26 01:09:57 +00:00
|
|
|
Owner: strings.Split(from.FullName, "/")[0],
|
|
|
|
Name: strings.Split(from.FullName, "/")[1],
|
2015-10-05 00:40:27 +00:00
|
|
|
FullName: from.FullName,
|
|
|
|
Avatar: from.Owner.Links.Avatar.Href,
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 19:39:56 +00:00
|
|
|
|
|
|
|
// convertUser is a helper function used to convert a Bitbucket user account
|
|
|
|
// structure to the Drone User structure.
|
|
|
|
func convertUser(from *internal.Account, token *oauth2.Token) *model.User {
|
|
|
|
return &model.User{
|
|
|
|
Login: from.Login,
|
|
|
|
Token: token.AccessToken,
|
|
|
|
Secret: token.RefreshToken,
|
|
|
|
Expiry: token.Expiry.UTC().Unix(),
|
|
|
|
Avatar: from.Links.Avatar.Href,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeamList is a helper function used to convert a Bitbucket team list
|
|
|
|
// structure to the Drone Team structure.
|
|
|
|
func convertTeamList(from []*internal.Account) []*model.Team {
|
|
|
|
var teams []*model.Team
|
|
|
|
for _, team := range from {
|
|
|
|
teams = append(teams, convertTeam(team))
|
|
|
|
}
|
|
|
|
return teams
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeam is a helper function used to convert a Bitbucket team account
|
|
|
|
// structure to the Drone Team structure.
|
|
|
|
func convertTeam(from *internal.Account) *model.Team {
|
|
|
|
return &model.Team{
|
|
|
|
Login: from.Login,
|
|
|
|
Avatar: from.Links.Avatar.Href,
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
|
|
|
|
// convertPullHook is a helper function used to convert a Bitbucket pull request
|
|
|
|
// hook to the Drone build struct holding commit information.
|
|
|
|
func convertPullHook(from *internal.PullRequestHook) *model.Build {
|
|
|
|
return &model.Build{
|
2016-11-19 22:44:22 +00:00
|
|
|
Event: model.EventPull,
|
|
|
|
Commit: from.PullRequest.Dest.Commit.Hash,
|
|
|
|
Ref: fmt.Sprintf("refs/heads/%s", from.PullRequest.Dest.Branch.Name),
|
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
|
|
|
from.PullRequest.Source.Branch.Name,
|
|
|
|
from.PullRequest.Dest.Branch.Name,
|
|
|
|
),
|
|
|
|
Remote: fmt.Sprintf("https://bitbucket.org/%s", from.PullRequest.Source.Repo.FullName),
|
2016-04-30 08:00:39 +00:00
|
|
|
Link: from.PullRequest.Links.Html.Href,
|
|
|
|
Branch: from.PullRequest.Dest.Branch.Name,
|
|
|
|
Message: from.PullRequest.Desc,
|
|
|
|
Avatar: from.Actor.Links.Avatar.Href,
|
|
|
|
Author: from.Actor.Login,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: from.Actor.Login,
|
2016-04-30 08:00:39 +00:00
|
|
|
Timestamp: from.PullRequest.Updated.UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPushHook is a helper function used to convert a Bitbucket push
|
|
|
|
// hook to the Drone build struct holding commit information.
|
|
|
|
func convertPushHook(hook *internal.PushHook, change *internal.Change) *model.Build {
|
|
|
|
build := &model.Build{
|
|
|
|
Commit: change.New.Target.Hash,
|
|
|
|
Link: change.New.Target.Links.Html.Href,
|
|
|
|
Branch: change.New.Name,
|
|
|
|
Message: change.New.Target.Message,
|
|
|
|
Avatar: hook.Actor.Links.Avatar.Href,
|
|
|
|
Author: hook.Actor.Login,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: hook.Actor.Login,
|
2016-04-30 08:00:39 +00:00
|
|
|
Timestamp: change.New.Target.Date.UTC().Unix(),
|
|
|
|
}
|
|
|
|
switch change.New.Type {
|
|
|
|
case "tag", "annotated_tag", "bookmark":
|
|
|
|
build.Event = model.EventTag
|
|
|
|
build.Ref = fmt.Sprintf("refs/tags/%s", change.New.Name)
|
|
|
|
default:
|
|
|
|
build.Event = model.EventPush
|
|
|
|
build.Ref = fmt.Sprintf("refs/heads/%s", change.New.Name)
|
|
|
|
}
|
2016-11-25 21:37:58 +00:00
|
|
|
if len(change.New.Target.Author.Raw) != 0 {
|
|
|
|
build.Email = extractEmail(change.New.Target.Author.Raw)
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
return build
|
|
|
|
}
|
2016-11-25 21:37:58 +00:00
|
|
|
|
2017-01-20 09:52:17 +00:00
|
|
|
// regex for git author fields ("name <name@mail.tld>")
|
|
|
|
var reGitMail = regexp.MustCompile("<(.*)>")
|
|
|
|
|
2016-11-25 21:37:58 +00:00
|
|
|
// extracts the email from a git commit author string
|
|
|
|
func extractEmail(gitauthor string) (author string) {
|
2017-03-18 08:49:27 +00:00
|
|
|
matches := reGitMail.FindAllStringSubmatch(gitauthor, -1)
|
|
|
|
if len(matches) == 1 {
|
|
|
|
author = matches[0][1]
|
|
|
|
}
|
|
|
|
return
|
2016-11-25 21:37:58 +00:00
|
|
|
}
|