Skip to content

Commit

Permalink
Add support for Bitbucket build status
Browse files Browse the repository at this point in the history
  • Loading branch information
anibali committed Nov 21, 2015
1 parent 241e846 commit f4e6357
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
66 changes: 65 additions & 1 deletion remote/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
6 changes: 6 additions & 0 deletions remote/bitbucket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions remote/bitbucket/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit f4e6357

Please sign in to comment.