2015-10-04 04:50:11 +00:00
|
|
|
package bitbucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2016-04-29 19:39:56 +00:00
|
|
|
"github.com/drone/drone/remote"
|
|
|
|
"github.com/drone/drone/remote/bitbucket/internal"
|
2015-10-04 04:50:11 +00:00
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Bitbucket cloud endpoints.
|
|
|
|
const (
|
|
|
|
DefaultAPI = "https://api.bitbucket.org"
|
|
|
|
DefaultURL = "https://bitbucket.org"
|
|
|
|
)
|
2016-04-30 08:00:39 +00:00
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
type config struct {
|
2016-05-01 06:22:30 +00:00
|
|
|
API string
|
2016-04-30 08:00:39 +00:00
|
|
|
URL string
|
2015-10-04 04:50:11 +00:00
|
|
|
Client string
|
|
|
|
Secret string
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// New returns a new remote Configuration for integrating with the Bitbucket
|
|
|
|
// repository hosting service at https://bitbucket.org
|
|
|
|
func New(client, secret string) remote.Remote {
|
|
|
|
return &config{
|
2016-05-01 06:22:30 +00:00
|
|
|
API: DefaultAPI,
|
|
|
|
URL: DefaultURL,
|
2016-04-29 19:39:56 +00:00
|
|
|
Client: client,
|
|
|
|
Secret: secret,
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Login authenticates an account with Bitbucket using the oauth2 protocol. The
|
|
|
|
// Bitbucket account details are returned when the user is successfully authenticated.
|
|
|
|
func (c *config) Login(w http.ResponseWriter, r *http.Request) (*model.User, error) {
|
|
|
|
redirect := httputil.GetURL(r)
|
|
|
|
config := c.newConfig(redirect)
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
code := r.FormValue("code")
|
2015-10-04 04:50:11 +00:00
|
|
|
if len(code) == 0 {
|
2016-05-01 06:22:30 +00:00
|
|
|
http.Redirect(w, r, config.AuthCodeURL("drone"), http.StatusSeeOther)
|
2016-04-29 19:39:56 +00:00
|
|
|
return nil, nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
token, err := config.Exchange(oauth2.NoContext, code)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
2016-04-29 19:39:56 +00:00
|
|
|
return nil, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
client := internal.NewClient(c.API, config.Client(oauth2.NoContext, token))
|
2015-10-04 04:50:11 +00:00
|
|
|
curr, err := client.FindCurrent()
|
|
|
|
if err != nil {
|
2016-04-29 19:39:56 +00:00
|
|
|
return nil, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
2016-04-29 19:39:56 +00:00
|
|
|
return convertUser(curr, token), nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Auth uses the Bitbucket oauth2 access token and refresh token to authenticate
|
|
|
|
// a session and return the Bitbucket account login.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Auth(token, secret string) (string, error) {
|
2016-05-01 06:22:30 +00:00
|
|
|
client := c.newClientToken(token, secret)
|
2015-10-04 04:50:11 +00:00
|
|
|
user, err := client.FindCurrent()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return user.Login, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Refresh refreshes the Bitbucket oauth2 access token. If the token is
|
|
|
|
// refreshed the user is updated and a true value is returned.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Refresh(user *model.User) (bool, error) {
|
2016-05-01 06:22:30 +00:00
|
|
|
config := c.newConfig("")
|
2015-10-04 04:50:11 +00:00
|
|
|
source := config.TokenSource(
|
|
|
|
oauth2.NoContext, &oauth2.Token{RefreshToken: user.Secret})
|
|
|
|
|
|
|
|
token, err := source.Token()
|
|
|
|
if err != nil || len(token.AccessToken) == 0 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Token = token.AccessToken
|
|
|
|
user.Secret = token.RefreshToken
|
2015-10-05 00:40:27 +00:00
|
|
|
user.Expiry = token.Expiry.UTC().Unix()
|
2015-10-04 04:50:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Teams returns a list of all team membership for the Bitbucket account.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Teams(u *model.User) ([]*model.Team, error) {
|
|
|
|
opts := &internal.ListTeamOpts{
|
|
|
|
PageLen: 100,
|
|
|
|
Role: "member",
|
|
|
|
}
|
|
|
|
resp, err := c.newClient(u).ListTeams(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return convertTeamList(resp.Values), nil
|
|
|
|
}
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2016-11-11 18:56:48 +00:00
|
|
|
// TeamPerm is not supported by the Bitbucket driver.
|
|
|
|
func (c *config) TeamPerm(u *model.User, org string) (*model.Perm, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Repo returns the named Bitbucket repository.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Repo(u *model.User, owner, name string) (*model.Repo, error) {
|
|
|
|
repo, err := c.newClient(u).FindRepo(owner, name)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return convertRepo(repo), nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Repos returns a list of all repositories for Bitbucket account, including
|
|
|
|
// organization repositories.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Repos(u *model.User) ([]*model.RepoLite, error) {
|
|
|
|
client := c.newClient(u)
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
var all []*model.RepoLite
|
2015-10-04 05:23:37 +00:00
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
accounts := []string{u.Login}
|
|
|
|
resp, err := client.ListTeams(&internal.ListTeamOpts{
|
|
|
|
PageLen: 100,
|
|
|
|
Role: "member",
|
|
|
|
})
|
2015-10-05 00:40:27 +00:00
|
|
|
if err != nil {
|
2016-05-01 06:22:30 +00:00
|
|
|
return all, err
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
|
|
|
for _, team := range resp.Values {
|
2016-05-01 06:22:30 +00:00
|
|
|
accounts = append(accounts, team.Login)
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
for _, account := range accounts {
|
|
|
|
repos, err := client.ListReposAll(account)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
2016-05-01 06:22:30 +00:00
|
|
|
return all, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
for _, repo := range repos {
|
|
|
|
all = append(all, convertRepoLite(repo))
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
return all, nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Perm returns the user permissions for the named repository. Because Bitbucket
|
|
|
|
// does not have an endpoint to access user permissions, we attempt to fetch
|
|
|
|
// the repository hook list, which is restricted to administrators to calculate
|
|
|
|
// administrative access to a repository.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Perm(u *model.User, owner, name string) (*model.Perm, error) {
|
|
|
|
client := c.newClient(u)
|
2015-10-04 04:50:11 +00:00
|
|
|
|
|
|
|
perms := new(model.Perm)
|
|
|
|
_, err := client.FindRepo(owner, name)
|
|
|
|
if err != nil {
|
|
|
|
return perms, err
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
_, err = client.ListHooks(owner, name, &internal.ListOpts{})
|
2015-10-04 04:50:11 +00:00
|
|
|
if err == nil {
|
|
|
|
perms.Push = true
|
|
|
|
perms.Admin = true
|
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
perms.Pull = true
|
2015-10-04 04:50:11 +00:00
|
|
|
return perms, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// File fetches the file from the Bitbucket repository and returns its contents.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
|
|
|
|
config, err := c.newClient(u).FindSource(r.Owner, r.Name, b.Commit, f)
|
2015-10-04 05:23:37 +00:00
|
|
|
if err != nil {
|
2016-03-22 10:34:33 +00:00
|
|
|
return nil, err
|
2015-10-04 05:23:37 +00:00
|
|
|
}
|
2016-03-22 10:34:33 +00:00
|
|
|
return []byte(config.Data), err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Status creates a build status for the Bitbucket commit.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Status(u *model.User, r *model.Repo, b *model.Build, link string) error {
|
|
|
|
status := internal.BuildStatus{
|
2016-04-30 08:00:39 +00:00
|
|
|
State: convertStatus(b.Status),
|
|
|
|
Desc: convertDesc(b.Status),
|
2016-03-22 10:34:33 +00:00
|
|
|
Key: "Drone",
|
|
|
|
Url: link,
|
2015-11-21 05:22:28 +00:00
|
|
|
}
|
2016-04-29 19:39:56 +00:00
|
|
|
return c.newClient(u).CreateStatus(r.Owner, r.Name, b.Commit, &status)
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Activate activates the repository by registering repository push hooks with
|
|
|
|
// the Bitbucket repository. Prior to registering hook, previously created hooks
|
|
|
|
// are deleted.
|
|
|
|
func (c *config) Activate(u *model.User, r *model.Repo, link string) error {
|
2016-04-29 19:39:56 +00:00
|
|
|
rawurl, err := url.Parse(link)
|
2015-10-05 00:40:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
c.Deactivate(u, r, link)
|
2015-10-05 00:40:27 +00:00
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
return c.newClient(u).CreateHook(r.Owner, r.Name, &internal.Hook{
|
2015-10-05 00:40:27 +00:00
|
|
|
Active: true,
|
2016-04-29 19:39:56 +00:00
|
|
|
Desc: rawurl.Host,
|
2015-10-05 00:40:27 +00:00
|
|
|
Events: []string{"repo:push"},
|
|
|
|
Url: link,
|
|
|
|
})
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Deactivate deactives the repository be removing repository push hooks from
|
|
|
|
// the Bitbucket repository.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Deactivate(u *model.User, r *model.Repo, link string) error {
|
|
|
|
client := c.newClient(u)
|
2015-10-05 00:40:27 +00:00
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{})
|
|
|
|
if err != nil {
|
2016-04-30 08:00:39 +00:00
|
|
|
return err
|
2016-04-29 19:39:56 +00:00
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
hook := matchingHooks(hooks.Values, link)
|
|
|
|
if hook != nil {
|
|
|
|
return client.DeleteHook(r.Owner, r.Name, hook.Uuid)
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
2015-10-04 04:50:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Netrc returns a netrc file capable of authenticating Bitbucket requests and
|
|
|
|
// cloning Bitbucket repositories.
|
|
|
|
func (c *config) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
|
|
|
return &model.Netrc{
|
|
|
|
Machine: "bitbucket.org",
|
|
|
|
Login: "x-token-auth",
|
|
|
|
Password: u.Token,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
// Hook parses the incoming Bitbucket hook and returns the Repository and
|
2016-05-01 06:22:30 +00:00
|
|
|
// Build details. If the hook is unsupported nil values are returned.
|
2016-04-29 19:39:56 +00:00
|
|
|
func (c *config) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
2016-04-30 08:00:39 +00:00
|
|
|
return parseHook(r)
|
2015-10-06 06:17:59 +00:00
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
|
|
|
|
// helper function to return the bitbucket oauth2 client
|
|
|
|
func (c *config) newClient(u *model.User) *internal.Client {
|
|
|
|
return c.newClientToken(u.Token, u.Secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return the bitbucket oauth2 client
|
|
|
|
func (c *config) newClientToken(token, secret string) *internal.Client {
|
|
|
|
return internal.NewClientToken(
|
|
|
|
c.API,
|
|
|
|
c.Client,
|
|
|
|
c.Secret,
|
|
|
|
&oauth2.Token{
|
|
|
|
AccessToken: token,
|
|
|
|
RefreshToken: secret,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return the bitbucket oauth2 config
|
|
|
|
func (c *config) newConfig(redirect string) *oauth2.Config {
|
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.Client,
|
|
|
|
ClientSecret: c.Secret,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: fmt.Sprintf("%s/site/oauth2/authorize", c.URL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/site/oauth2/access_token", c.URL),
|
|
|
|
},
|
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", redirect),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to return matching hooks.
|
|
|
|
func matchingHooks(hooks []*internal.Hook, rawurl string) *internal.Hook {
|
|
|
|
link, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
|
|
|
hookurl, err := url.Parse(hook.Url)
|
|
|
|
if err == nil && hookurl.Host == link.Host {
|
|
|
|
return hook
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|