Add support for Bitbucket build status
This commit is contained in:
parent
241e84608f
commit
f4e6357e96
3 changed files with 79 additions and 1 deletions
|
@ -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.
|
// Status sends the commit status to the remote system.
|
||||||
// An example would be the GitHub pull request status.
|
// 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 {
|
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
|
// 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(),
|
Timestamp: hook.PullRequest.Updated.UTC().Unix(),
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ const (
|
||||||
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
|
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
|
||||||
pathHooks = "%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"
|
pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s"
|
||||||
|
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -133,6 +134,11 @@ func (c *Client) FindSource(owner, name, revision, path string) (*Source, error)
|
||||||
return out, err
|
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 {
|
func (c *Client) do(rawurl, method string, in, out interface{}) error {
|
||||||
|
|
||||||
uri, err := url.Parse(rawurl)
|
uri, err := url.Parse(rawurl)
|
||||||
|
|
|
@ -21,6 +21,14 @@ type AccountResp struct {
|
||||||
Values []*Account `json:"values"`
|
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 {
|
type Email struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
IsConfirmed bool `json:"is_confirmed"`
|
IsConfirmed bool `json:"is_confirmed"`
|
||||||
|
|
Loading…
Add table
Reference in a new issue