From f4e6357e967a635723a87056e2ffa49dd7d78cd7 Mon Sep 17 00:00:00 2001 From: Aiden Nibali Date: Sat, 21 Nov 2015 16:22:28 +1100 Subject: [PATCH] Add support for Bitbucket build status --- remote/bitbucket/bitbucket.go | 66 ++++++++++++++++++++++++++++++++++- remote/bitbucket/client.go | 6 ++++ remote/bitbucket/types.go | 8 +++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/remote/bitbucket/bitbucket.go b/remote/bitbucket/bitbucket.go index 281d27bca..630c3ecad 100644 --- a/remote/bitbucket/bitbucket.go +++ b/remote/bitbucket/bitbucket.go @@ -267,7 +267,27 @@ func (bb *Bitbucket) Script(u *model.User, r *model.Repo, b *model.Build) ([]byt // Status sends the commit status to the remote system. // An example would be the GitHub pull request status. func (bb *Bitbucket) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { - return nil + client := NewClientToken( + bb.Client, + bb.Secret, + &oauth2.Token{ + AccessToken: u.Token, + RefreshToken: u.Secret, + }, + ) + + status := getStatus(b.Status) + desc := getDesc(b.Status) + + data := BuildStatus{ + State: status, + Key: "Drone", + Url: link, + Desc: desc, + } + + err := client.CreateStatus(r.Owner, r.Name, b.Commit, &data) + return err } // Netrc returns a .netrc file that can be used to clone @@ -459,3 +479,47 @@ func (bb *Bitbucket) pullHook(r *http.Request) (*model.Repo, *model.Build, error Timestamp: hook.PullRequest.Updated.UTC().Unix(), }, nil } + +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" +) + +// converts a Drone status to a BitBucket status. +func getStatus(status string) string { + switch status { + case model.StatusPending, model.StatusRunning: + return StatusPending + case model.StatusSuccess: + return StatusSuccess + case model.StatusFailure, model.StatusError, model.StatusKilled: + return StatusFailure + default: + return StatusFailure + } +} + +// generates a description for the build based on +// the Drone status +func getDesc(status string) string { + switch status { + case model.StatusPending, model.StatusRunning: + return DescPending + case model.StatusSuccess: + return DescSuccess + case model.StatusFailure: + return DescFailure + case model.StatusError, model.StatusKilled: + return DescError + default: + return DescError + } +} diff --git a/remote/bitbucket/client.go b/remote/bitbucket/client.go index f5a1cc00a..e0caed656 100644 --- a/remote/bitbucket/client.go +++ b/remote/bitbucket/client.go @@ -30,6 +30,7 @@ const ( pathHook = "%s/2.0/repositories/%s/%s/hooks/%s" pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s" pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s" + pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" ) type Client struct { @@ -133,6 +134,11 @@ func (c *Client) FindSource(owner, name, revision, path string) (*Source, error) return out, err } +func (c *Client) CreateStatus(owner, name, revision string, status *BuildStatus) error { + uri := fmt.Sprintf(pathStatus, base, owner, name, revision) + return c.do(uri, post, status, nil) +} + func (c *Client) do(rawurl, method string, in, out interface{}) error { uri, err := url.Parse(rawurl) diff --git a/remote/bitbucket/types.go b/remote/bitbucket/types.go index 8f92948e3..555b4ca4c 100644 --- a/remote/bitbucket/types.go +++ b/remote/bitbucket/types.go @@ -21,6 +21,14 @@ type AccountResp struct { Values []*Account `json:"values"` } +type BuildStatus struct { + State string `json:"state"` + Key string `json:"key"` + Name string `json:"name,omitempty"` + Url string `json:"url"` + Desc string `json:"description,omitempty"` +} + type Email struct { Email string `json:"email"` IsConfirmed bool `json:"is_confirmed"`