2022-10-18 01:24:12 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-10-04 04:50:11 +00:00
|
|
|
package bitbucket
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2024-04-09 09:30:04 +00:00
|
|
|
"errors"
|
2015-10-04 04:50:11 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-08-02 11:15:57 +00:00
|
|
|
"path/filepath"
|
2023-11-25 23:52:52 +00:00
|
|
|
"strconv"
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/bitbucket/internal"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/common"
|
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
shared_utils "go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2015-10-04 04:50:11 +00:00
|
|
|
)
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Bitbucket cloud endpoints.
|
|
|
|
const (
|
|
|
|
DefaultAPI = "https://api.bitbucket.org"
|
|
|
|
DefaultURL = "https://bitbucket.org"
|
2024-03-15 17:00:25 +00:00
|
|
|
pageSize = 100
|
2016-05-01 06:22:30 +00:00
|
|
|
)
|
2016-04-30 08:00:39 +00:00
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Opts are forge options for bitbucket.
|
2021-10-19 09:44:49 +00:00
|
|
|
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
|
2015-10-04 04:50:11 +00:00
|
|
|
Client string
|
|
|
|
Secret string
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// 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
|
2022-11-04 23:35:06 +00:00
|
|
|
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,
|
2021-10-19 09:44:49 +00:00
|
|
|
Client: opts.Client,
|
|
|
|
Secret: opts.Secret,
|
|
|
|
}, nil
|
|
|
|
// TODO: add checks
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Name returns the string name of this driver.
|
2022-06-17 18:14:01 +00:00
|
|
|
func (c *config) Name() string {
|
|
|
|
return "bitbucket"
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// URL returns the root url of a configured forge.
|
2023-05-31 16:30:41 +00:00
|
|
|
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.
|
2024-02-13 15:19:02 +00:00
|
|
|
func (c *config) Login(ctx context.Context, req *forge_types.OAuthRequest) (*model.User, string, error) {
|
2023-07-30 16:28:52 +00:00
|
|
|
config := c.newOAuth2Config()
|
2024-06-21 07:55:30 +00:00
|
|
|
redirectURL := config.AuthCodeURL(req.State)
|
2016-12-19 05:42:56 +00:00
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
// check the OAuth code
|
|
|
|
if len(req.Code) == 0 {
|
|
|
|
return nil, redirectURL, nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 15:19:02 +00:00
|
|
|
token, err := config.Exchange(ctx, req.Code)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client := internal.NewClient(ctx, c.API, config.Client(ctx, token))
|
2015-10-04 04:50:11 +00:00
|
|
|
curr, err := client.FindCurrent()
|
|
|
|
if err != nil {
|
2024-02-13 15:19:02 +00:00
|
|
|
return nil, redirectURL, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
2024-02-13 15:19:02 +00:00
|
|
|
return convertUser(curr, token), redirectURL, 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.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) Auth(ctx context.Context, token, secret string) (string, error) {
|
|
|
|
client := c.newClientToken(ctx, 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.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) Refresh(ctx context.Context, user *model.User) (bool, error) {
|
2023-07-30 16:28:52 +00:00
|
|
|
config := c.newOAuth2Config()
|
2015-10-04 04:50:11 +00:00
|
|
|
source := config.TokenSource(
|
2021-09-28 10:56:59 +00:00
|
|
|
ctx, &oauth2.Token{RefreshToken: user.Secret})
|
2015-10-04 04:50:11 +00:00
|
|
|
|
|
|
|
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.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
2023-04-30 01:40:13 +00:00
|
|
|
return shared_utils.Paginate(func(page int) ([]*model.Team, error) {
|
|
|
|
opts := &internal.ListWorkspacesOpts{
|
2024-03-15 17:00:25 +00:00
|
|
|
PageLen: pageSize,
|
2023-04-30 01:40:13 +00:00
|
|
|
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
|
|
|
}
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Repo returns the named Bitbucket repository.
|
2022-11-15 14:01:23 +00:00
|
|
|
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)
|
2022-09-05 15:08:51 +00:00
|
|
|
}
|
2023-10-23 16:44:25 +00:00
|
|
|
if owner == "" {
|
|
|
|
repos, err := c.Repos(ctx, u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-26 21:55:21 +00:00
|
|
|
for _, repo := range repos {
|
|
|
|
if string(repo.ForgeRemoteID) == name {
|
|
|
|
owner = repo.Owner
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 22:01:59 +00:00
|
|
|
client := c.newClient(ctx, u)
|
|
|
|
repo, err := client.FindRepo(owner, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
perm, err := client.GetPermission(repo.FullName)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-21 22:01:59 +00:00
|
|
|
return convertRepo(repo, perm), nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 06:22:30 +00:00
|
|
|
// Repos returns a list of all repositories for Bitbucket account, including
|
|
|
|
// organization repositories.
|
2021-09-28 10:56:59 +00:00
|
|
|
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
|
|
|
|
2023-10-23 16:44:25 +00:00
|
|
|
workspaces, err := shared_utils.Paginate(func(page int) ([]*internal.Workspace, error) {
|
|
|
|
resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{
|
|
|
|
Page: page,
|
2024-03-15 17:00:25 +00:00
|
|
|
PageLen: pageSize,
|
2023-10-23 16:44:25 +00:00
|
|
|
Role: "member",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Values, nil
|
2016-05-01 06:22:30 +00:00
|
|
|
})
|
2015-10-05 00:40:27 +00:00
|
|
|
if err != nil {
|
2023-10-23 16:44:25 +00:00
|
|
|
return nil, err
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
2015-10-04 04:50:11 +00:00
|
|
|
|
2024-06-04 06:30:54 +00:00
|
|
|
userPermissions, err := client.ListPermissionsAll()
|
2024-01-19 03:15:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userPermissionsByRepo := make(map[string]*internal.RepoPerm)
|
2024-06-04 06:30:54 +00:00
|
|
|
for _, permission := range userPermissions {
|
2024-01-19 03:15:47 +00:00
|
|
|
userPermissionsByRepo[permission.Repo.FullName] = permission
|
|
|
|
}
|
|
|
|
|
2023-10-23 16:44:25 +00:00
|
|
|
var all []*model.Repo
|
|
|
|
for _, workspace := range workspaces {
|
2022-09-18 07:26:13 +00:00
|
|
|
repos, err := client.ListReposAll(workspace.Slug)
|
2015-10-04 04:50:11 +00:00
|
|
|
if err != nil {
|
2023-10-23 16:44:25 +00:00
|
|
|
return nil, err
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
for _, repo := range repos {
|
2024-01-19 03:15:47 +00:00
|
|
|
if perm, ok := userPermissionsByRepo[repo.FullName]; ok {
|
|
|
|
all = append(all, convertRepo(repo, perm))
|
2023-03-21 22:01:59 +00:00
|
|
|
}
|
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 23:30:00 +00:00
|
|
|
// File fetches the file from the Bitbucket repository and returns its contents.
|
2022-10-18 01:24:12 +00:00
|
|
|
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)
|
2015-10-04 05:23:37 +00:00
|
|
|
if err != nil {
|
2024-04-09 09:30:04 +00:00
|
|
|
var rspErr internal.Error
|
|
|
|
if ok := errors.As(err, &rspErr); ok && rspErr.Status == http.StatusNotFound {
|
|
|
|
return nil, &forge_types.ErrConfigNotFound{
|
|
|
|
Configs: []string{f},
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 10:34:33 +00:00
|
|
|
return nil, err
|
2015-10-04 05:23:37 +00:00
|
|
|
}
|
2024-04-09 09:30:04 +00:00
|
|
|
return []byte(*config), nil
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Dir fetches a folder from the bitbucket repository.
|
2023-08-02 11:15:57 +00:00
|
|
|
func (c *config) Dir(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline, f string) ([]*forge_types.FileMeta, error) {
|
|
|
|
var page *string
|
|
|
|
repoPathFiles := []*forge_types.FileMeta{}
|
|
|
|
client := c.newClient(ctx, u)
|
|
|
|
for {
|
|
|
|
filesResp, err := client.GetRepoFiles(r.Owner, r.Name, p.Commit, f, page)
|
|
|
|
if err != nil {
|
2024-04-09 09:30:04 +00:00
|
|
|
var rspErr internal.Error
|
|
|
|
if ok := errors.As(err, &rspErr); ok && rspErr.Status == http.StatusNotFound {
|
|
|
|
return nil, &forge_types.ErrConfigNotFound{
|
|
|
|
Configs: []string{f},
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 11:15:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, file := range filesResp.Values {
|
|
|
|
_, filename := filepath.Split(file.Path)
|
|
|
|
repoFile := forge_types.FileMeta{
|
|
|
|
Name: filename,
|
|
|
|
}
|
|
|
|
if file.Type == "commit_file" {
|
|
|
|
fileData, err := c.newClient(ctx, u).FindSource(r.Owner, r.Name, p.Commit, file.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fileData != nil {
|
|
|
|
repoFile.Data = []byte(*fileData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repoPathFiles = append(repoPathFiles, &repoFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for more results page
|
|
|
|
if filesResp.Next == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
nextPageURL, err := url.Parse(*filesResp.Next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
params, err := url.ParseQuery(nextPageURL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nextPage := params.Get("page")
|
|
|
|
if len(nextPage) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page = &nextPage
|
|
|
|
}
|
|
|
|
return repoPathFiles, nil
|
2019-06-03 07:16:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Status creates a pipeline status for the Bitbucket commit.
|
2023-06-27 16:01:18 +00:00
|
|
|
func (c *config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Workflow) error {
|
2022-10-18 01:24:12 +00:00
|
|
|
status := internal.PipelineStatus{
|
|
|
|
State: convertStatus(pipeline.Status),
|
|
|
|
Desc: common.GetPipelineStatusDescription(pipeline.Status),
|
2021-10-02 08:59:34 +00:00
|
|
|
Key: "Woodpecker",
|
2023-11-14 16:12:12 +00:00
|
|
|
URL: common.GetPipelineStatusURL(repo, pipeline, nil),
|
2015-11-21 05:22:28 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
return c.newClient(ctx, user).CreateStatus(repo.Owner, repo.Name, pipeline.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.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
2024-06-04 06:30:54 +00:00
|
|
|
rawURL, err := url.Parse(link)
|
2015-10-05 00:40:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.Deactivate(ctx, u, r, link)
|
2015-10-05 00:40:27 +00:00
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
return c.newClient(ctx, u).CreateHook(r.Owner, r.Name, &internal.Hook{
|
2015-10-05 00:40:27 +00:00
|
|
|
Active: true,
|
2024-06-04 06:30:54 +00:00
|
|
|
Desc: rawURL.Host,
|
2023-10-08 12:18:49 +00:00
|
|
|
Events: []string{"repo:push", "pullrequest:created"},
|
2021-12-01 13:22:06 +00:00
|
|
|
URL: link,
|
2015-10-05 00:40:27 +00:00
|
|
|
})
|
2015-10-04 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:12:17 +00:00
|
|
|
// Deactivate deactivates the repository be removing repository push hooks from
|
2016-05-01 06:22:30 +00:00
|
|
|
// the Bitbucket repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
client := c.newClient(ctx, u)
|
2015-10-05 00:40:27 +00:00
|
|
|
|
2023-10-23 16:44:25 +00:00
|
|
|
hooks, err := shared_utils.Paginate(func(page int) ([]*internal.Hook, error) {
|
|
|
|
hooks, err := client.ListHooks(r.Owner, r.Name, &internal.ListOpts{
|
|
|
|
Page: page,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hooks.Values, nil
|
|
|
|
})
|
2016-04-29 19:39:56 +00:00
|
|
|
if err != nil {
|
2016-04-30 08:00:39 +00:00
|
|
|
return err
|
2016-04-29 19:39:56 +00:00
|
|
|
}
|
2023-10-23 16:44:25 +00:00
|
|
|
hook := matchingHooks(hooks, link)
|
2016-05-01 06:22:30 +00:00
|
|
|
if hook != nil {
|
2021-12-01 13:22:06 +00:00
|
|
|
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.
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-27 00:47:55 +00:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
2023-09-29 16:01:29 +00:00
|
|
|
func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]string, error) {
|
|
|
|
opts := internal.ListOpts{Page: p.Page, PageLen: p.PerPage}
|
|
|
|
bitbucketBranches, err := c.newClient(ctx, u).ListBranches(r.Owner, r.Name, &opts)
|
2022-05-12 17:31:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
branches := make([]string, 0)
|
|
|
|
for _, branch := range bitbucketBranches {
|
|
|
|
branches = append(branches, branch.Name)
|
|
|
|
}
|
|
|
|
return branches, nil
|
2021-10-27 00:47:55 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// BranchHead returns the sha of the head (latest commit) of the specified branch.
|
2024-02-11 09:44:50 +00:00
|
|
|
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
|
|
|
|
commit, err := c.newClient(ctx, u).GetBranchHead(r.Owner, r.Name, branch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &model.Commit{
|
|
|
|
SHA: commit.Hash,
|
|
|
|
ForgeURL: commit.Links.HTML.Href,
|
|
|
|
}, nil
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 21:40:28 +00:00
|
|
|
// 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) {
|
2023-09-29 16:01:29 +00:00
|
|
|
opts := internal.ListOpts{Page: p.Page, PageLen: p.PerPage}
|
2023-06-27 21:40:28 +00:00
|
|
|
pullRequests, err := c.newClient(ctx, u).ListPullRequests(r.Owner, r.Name, &opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-24 09:04:18 +00:00
|
|
|
var result []*model.PullRequest
|
2023-06-27 21:40:28 +00:00
|
|
|
for _, pullRequest := range pullRequests {
|
|
|
|
result = append(result, &model.PullRequest{
|
2023-11-25 23:52:52 +00:00
|
|
|
Index: model.ForgeRemoteID(strconv.Itoa(int(pullRequest.ID))),
|
2023-06-27 21:40:28 +00:00
|
|
|
Title: pullRequest.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
2023-03-19 09:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
// Hook parses the incoming Bitbucket hook and returns the Repository and
|
2022-10-18 01:24:12 +00:00
|
|
|
// 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) {
|
2016-12-19 05:42:56 +00:00
|
|
|
return parseHook(req)
|
2015-10-06 06:17:59 +00:00
|
|
|
}
|
2016-05-01 06:22:30 +00:00
|
|
|
|
2022-07-25 01:09:35 +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
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
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,
|
2024-01-10 21:56:42 +00:00
|
|
|
IsUser: false, // bitbucket uses workspaces (similar to orgs) for teams and single users so we cannot distinguish between them
|
2023-07-21 17:45:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// helper function to return the bitbucket oauth2 client.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) newClient(ctx context.Context, u *model.User) *internal.Client {
|
2022-05-12 17:31:01 +00:00
|
|
|
if u == nil {
|
|
|
|
return c.newClientToken(ctx, "", "")
|
|
|
|
}
|
2021-09-28 10:56:59 +00:00
|
|
|
return c.newClientToken(ctx, u.Token, u.Secret)
|
2016-05-01 06:22:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// helper function to return the bitbucket oauth2 client.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *config) newClientToken(ctx context.Context, token, secret string) *internal.Client {
|
2016-05-01 06:22:30 +00:00
|
|
|
return internal.NewClientToken(
|
2021-09-28 10:56:59 +00:00
|
|
|
ctx,
|
2016-05-01 06:22:30 +00:00
|
|
|
c.API,
|
|
|
|
c.Client,
|
|
|
|
c.Secret,
|
|
|
|
&oauth2.Token{
|
|
|
|
AccessToken: token,
|
|
|
|
RefreshToken: secret,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// helper function to return the bitbucket oauth2 config.
|
2023-07-30 16:28:52 +00:00
|
|
|
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
|
|
|
},
|
2023-09-22 14:43:31 +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.
|
2024-06-04 06:30:54 +00:00
|
|
|
func matchingHooks(hooks []*internal.Hook, rawURL string) *internal.Hook {
|
|
|
|
link, err := url.Parse(rawURL)
|
2016-05-01 06:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
2024-06-04 06:30:54 +00:00
|
|
|
hookURL, err := url.Parse(hook.URL)
|
|
|
|
if err == nil && hookURL.Host == link.Host {
|
2016-05-01 06:22:30 +00:00
|
|
|
return hook
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|