woodpecker/server/forge/bitbucket/bitbucket.go

398 lines
12 KiB
Go
Raw Normal View History

// Copyright 2022 Woodpecker Authors
2018-02-19 22:24:10 +00:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 13:02:17 +00:00
//
2018-02-19 22:24:10 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitbucket
import (
"context"
"fmt"
"net/http"
"net/url"
"golang.org/x/oauth2"
shared_utils "github.com/woodpecker-ci/woodpecker/shared/utils"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/forge"
"github.com/woodpecker-ci/woodpecker/server/forge/bitbucket/internal"
"github.com/woodpecker-ci/woodpecker/server/forge/common"
2022-11-06 11:44:04 +00:00
forge_types "github.com/woodpecker-ci/woodpecker/server/forge/types"
"github.com/woodpecker-ci/woodpecker/server/model"
)
2016-05-01 06:22:30 +00:00
// Bitbucket cloud endpoints.
const (
DefaultAPI = "https://api.bitbucket.org"
DefaultURL = "https://bitbucket.org"
)
// Opts are forge options for bitbucket
type Opts struct {
Client string
Secret string
}
2016-04-29 19:39:56 +00:00
type config struct {
2016-05-01 06:22:30 +00:00
API string
2023-05-31 16:30:41 +00:00
url string
Client string
Secret string
}
// New returns a new forge Configuration for integrating with the Bitbucket
2016-04-29 19:39:56 +00:00
// repository hosting service at https://bitbucket.org
func New(opts *Opts) (forge.Forge, error) {
2016-04-29 19:39:56 +00:00
return &config{
2016-05-01 06:22:30 +00:00
API: DefaultAPI,
2023-05-31 16:30:41 +00:00
url: DefaultURL,
Client: opts.Client,
Secret: opts.Secret,
}, nil
// TODO: add checks
}
2022-06-17 18:14:01 +00:00
// Name returns the string name of this driver
func (c *config) Name() string {
return "bitbucket"
}
2023-05-31 16:30:41 +00:00
// URL returns the root url of a configured forge
func (c *config) URL() string {
return c.url
}
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(ctx context.Context, w http.ResponseWriter, req *http.Request) (*model.User, error) {
config := c.newOAuth2Config()
// get the OAuth errors
if err := req.FormValue("error"); err != "" {
2022-11-06 11:44:04 +00:00
return nil, &forge_types.AuthError{
2016-12-19 16:22:11 +00:00
Err: err,
Description: req.FormValue("error_description"),
URI: req.FormValue("error_uri"),
}
}
// get the OAuth code
code := req.FormValue("code")
if len(code) == 0 {
http.Redirect(w, req, config.AuthCodeURL("woodpecker"), http.StatusSeeOther)
2016-04-29 19:39:56 +00:00
return nil, nil
}
token, err := config.Exchange(ctx, code)
if err != nil {
2016-04-29 19:39:56 +00:00
return nil, err
}
client := internal.NewClient(ctx, c.API, config.Client(ctx, token))
curr, err := client.FindCurrent()
if err != nil {
2016-04-29 19:39:56 +00:00
return nil, err
}
2016-04-29 19:39:56 +00:00
return convertUser(curr, token), nil
}
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.
func (c *config) Auth(ctx context.Context, token, secret string) (string, error) {
client := c.newClientToken(ctx, token, secret)
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.
func (c *config) Refresh(ctx context.Context, user *model.User) (bool, error) {
config := c.newOAuth2Config()
source := config.TokenSource(
ctx, &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
user.Expiry = token.Expiry.UTC().Unix()
return true, nil
}
2016-05-01 06:22:30 +00:00
// Teams returns a list of all team membership for the Bitbucket account.
func (c *config) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
return shared_utils.Paginate(func(page int) ([]*model.Team, error) {
opts := &internal.ListWorkspacesOpts{
PageLen: 100,
Page: page,
Role: "member",
}
resp, err := c.newClient(ctx, u).ListWorkspaces(opts)
if err != nil {
return nil, err
}
return convertWorkspaceList(resp.Values), nil
})
2016-04-29 19:39:56 +00:00
}
2016-05-01 06:22:30 +00:00
// Repo returns the named Bitbucket repository.
func (c *config) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
if remoteID.IsValid() {
name = string(remoteID)
}
repos, err := c.Repos(ctx, u)
if err != nil {
return nil, err
}
if len(owner) == 0 {
for _, repo := range repos {
if string(repo.ForgeRemoteID) == name {
owner = repo.Owner
break
}
}
}
client := c.newClient(ctx, u)
repo, err := client.FindRepo(owner, name)
if err != nil {
return nil, err
}
perm, err := client.GetPermission(repo.FullName)
if err != nil {
return nil, err
}
return convertRepo(repo, perm), nil
}
2016-05-01 06:22:30 +00:00
// Repos returns a list of all repositories for Bitbucket account, including
// organization repositories.
func (c *config) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
client := c.newClient(ctx, u)
2016-04-29 19:39:56 +00:00
2017-07-14 19:58:38 +00:00
var all []*model.Repo
resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{
2016-05-01 06:22:30 +00:00
PageLen: 100,
Role: "member",
})
if err != nil {
2016-05-01 06:22:30 +00:00
return all, err
}
for _, workspace := range resp.Values {
repos, err := client.ListReposAll(workspace.Slug)
if err != nil {
2016-05-01 06:22:30 +00:00
return all, err
}
2016-05-01 06:22:30 +00:00
for _, repo := range repos {
perm, err := client.GetPermission(repo.FullName)
if err != nil {
return nil, err
}
all = append(all, convertRepo(repo, perm))
}
}
2016-05-01 06:22:30 +00:00
return all, nil
}
// File fetches the file from the Bitbucket repository and returns its contents.
func (c *config) File(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline, f string) ([]byte, error) {
config, err := c.newClient(ctx, u).FindSource(r.Owner, r.Name, p.Commit, f)
if err != nil {
return nil, err
}
return []byte(*config), err
}
2023-03-18 19:35:27 +00:00
func (c *config) Dir(_ context.Context, _ *model.User, _ *model.Repo, _ *model.Pipeline, _ string) ([]*forge_types.FileMeta, error) {
2022-11-06 11:44:04 +00:00
return nil, forge_types.ErrNotImplemented
}
// Status creates a pipeline status for the Bitbucket commit.
func (c *config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Workflow) error {
status := internal.PipelineStatus{
State: convertStatus(pipeline.Status),
Desc: common.GetPipelineStatusDescription(pipeline.Status),
Key: "Woodpecker",
URL: common.GetPipelineStatusLink(repo, pipeline, nil),
2015-11-21 05:22:28 +00:00
}
return c.newClient(ctx, user).CreateStatus(repo.Owner, repo.Name, pipeline.Commit, &status)
}
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(ctx context.Context, u *model.User, r *model.Repo, link string) error {
2016-04-29 19:39:56 +00:00
rawurl, err := url.Parse(link)
if err != nil {
return err
}
_ = c.Deactivate(ctx, u, r, link)
return c.newClient(ctx, u).CreateHook(r.Owner, r.Name, &internal.Hook{
Active: true,
2016-04-29 19:39:56 +00:00
Desc: rawurl.Host,
Events: []string{"repo:push"},
URL: link,
})
}
// Deactivate deactivates the repository be removing repository push hooks from
2016-05-01 06:22:30 +00:00
// the Bitbucket repository.
func (c *config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
client := c.newClient(ctx, u)
2016-04-29 19:39:56 +00:00
hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{})
if err != nil {
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)
}
return nil
}
2016-05-01 06:22:30 +00:00
// Netrc returns a netrc file capable of authenticating Bitbucket requests and
// cloning Bitbucket repositories.
2023-03-18 19:35:27 +00:00
func (c *config) Netrc(u *model.User, _ *model.Repo) (*model.Netrc, error) {
2016-05-01 06:22:30 +00:00
return &model.Netrc{
Machine: "bitbucket.org",
Login: "x-token-auth",
Password: u.Token,
}, nil
}
// Branches returns the names of all branches for the named repository.
func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo, _ *model.ListOptions) ([]string, error) {
bitbucketBranches, err := c.newClient(ctx, u).ListBranches(r.Owner, r.Name)
if err != nil {
return nil, err
}
branches := make([]string, 0)
for _, branch := range bitbucketBranches {
branches = append(branches, branch.Name)
}
return branches, nil
}
// BranchHead returns the sha of the head (latest commit) of the specified branch
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
return c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
}
// PullRequests returns the pull requests of the named repository.
func (c *config) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {
opts := internal.ListOpts{Page: p.Page, PageLen: p.Page}
pullRequests, err := c.newClient(ctx, u).ListPullRequests(r.Owner, r.Name, &opts)
if err != nil {
return nil, err
}
result := []*model.PullRequest{}
for _, pullRequest := range pullRequests {
result = append(result, &model.PullRequest{
Index: int64(pullRequest.ID),
Title: pullRequest.Title,
})
}
return result, nil
}
// Hook parses the incoming Bitbucket hook and returns the Repository and
// Pipeline details. If the hook is unsupported nil values are returned.
2023-03-18 19:35:27 +00:00
func (c *config) Hook(_ context.Context, req *http.Request) (*model.Repo, *model.Pipeline, error) {
return parseHook(req)
}
2016-05-01 06:22:30 +00:00
// OrgMembership returns if user is member of organization and if user
// is admin/owner in this organization.
func (c *config) OrgMembership(ctx context.Context, u *model.User, owner string) (*model.OrgPerm, error) {
perm, err := c.newClient(ctx, u).GetUserWorkspaceMembership(owner, u.Login)
if err != nil {
return nil, err
}
return &model.OrgPerm{Member: perm != "", Admin: perm == "owner"}, nil
}
func (c *config) Org(ctx context.Context, u *model.User, owner string) (*model.Org, error) {
workspace, err := c.newClient(ctx, u).GetWorkspace(owner)
if err != nil {
return nil, err
}
return &model.Org{
Name: workspace.Slug,
IsUser: false, // bitbucket uses workspaces (similar to orgs) for teams and single users so we can not distinguish between them
}, nil
}
2016-05-01 06:22:30 +00:00
// helper function to return the bitbucket oauth2 client
func (c *config) newClient(ctx context.Context, u *model.User) *internal.Client {
if u == nil {
return c.newClientToken(ctx, "", "")
}
return c.newClientToken(ctx, u.Token, u.Secret)
2016-05-01 06:22:30 +00:00
}
// helper function to return the bitbucket oauth2 client
func (c *config) newClientToken(ctx context.Context, token, secret string) *internal.Client {
2016-05-01 06:22:30 +00:00
return internal.NewClientToken(
ctx,
2016-05-01 06:22:30 +00:00
c.API,
c.Client,
c.Secret,
&oauth2.Token{
AccessToken: token,
RefreshToken: secret,
},
)
}
// helper function to return the bitbucket oauth2 config
func (c *config) newOAuth2Config() *oauth2.Config {
2016-05-01 06:22:30 +00:00
return &oauth2.Config{
ClientID: c.Client,
ClientSecret: c.Secret,
Endpoint: oauth2.Endpoint{
2023-05-31 16:30:41 +00:00
AuthURL: fmt.Sprintf("%s/site/oauth2/authorize", c.url),
TokenURL: fmt.Sprintf("%s/site/oauth2/access_token", c.url),
2016-05-01 06:22:30 +00:00
},
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
2016-05-01 06:22:30 +00:00
}
}
// 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)
2016-05-01 06:22:30 +00:00
if err == nil && hookurl.Host == link.Host {
return hook
}
}
return nil
}