Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches support for BitBucket #907

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions server/remote/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,16 @@ func (c *config) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {

// Branches returns the names of all branches for the named repository.
func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil
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
}

// Hook parses the incoming Bitbucket hook and returns the Repository and
Expand All @@ -290,6 +298,9 @@ func (c *config) Hook(ctx context.Context, req *http.Request) (*model.Repo, *mod

// 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)
}

Expand Down
8 changes: 8 additions & 0 deletions server/remote/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s"
pathSource = "%s/2.0/repositories/%s/%s/src/%s/%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
pathBranches = "%s/2.0/repositories/%s/%s/refs/branches"
)

type Client struct {
Expand Down Expand Up @@ -174,6 +175,13 @@ func (c *Client) GetPermission(fullName string) (*RepoPerm, error) {
return out.Values[0], nil
}

func (c *Client) ListBranches(owner, name string) ([]*Branch, error) {
out := new(BranchResp)
uri := fmt.Sprintf(pathBranches, c.base, owner, name)
_, err := c.do(uri, get, nil, out)
return out.Values, err
}

func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error) {
uri, err := url.Parse(rawurl)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions server/remote/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,11 @@ type RepoPermResp struct {
type RepoPerm struct {
Permission string `json:"permission"`
}

type BranchResp struct {
Values []*Branch `json:"values"`
}

type Branch struct {
Name string `json:"name"`
}
13 changes: 11 additions & 2 deletions server/remote/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,17 @@ func (c *Config) Activate(ctx context.Context, u *model.User, r *model.Repo, lin

// Branches returns the names of all branches for the named repository.
func (c *Config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil

6543 marked this conversation as resolved.
Show resolved Hide resolved
bitbucketBranches, err := internal.NewClientWithToken(ctx, c.URL, c.Consumer, u.Token).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
}

func (c *Config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
Expand Down
15 changes: 15 additions & 0 deletions server/remote/bitbucketserver/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
pathHookEnabled = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled"
pathHookSettings = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/settings"
pathStatus = "%s/rest/build-status/1.0/commits/%s"
pathBranches = "%s/2.0/repositories/%s/%s/refs/branches"
)

type Client struct {
Expand Down Expand Up @@ -322,6 +323,20 @@ func (c *Client) paginatedRepos(start int) ([]*Repo, error) {
return repoResponse.Values, nil
}

func (c *Client) ListBranches(owner, name string) ([]*Branch, error) {
uri := fmt.Sprintf(pathBranches, c.base, owner, name)
response, err := c.doGet(uri)
if response != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
out := new(BranchResp)
err = json.NewDecoder(response.Body).Decode(&out)
return out.Values, err
}

func filter(vs []string, f func(string) bool) []string {
var vsf []string
for _, v := range vs {
Expand Down
8 changes: 8 additions & 0 deletions server/remote/bitbucketserver/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,11 @@ type HookSettings struct {
HookURL18 string `json:"hook-url-18,omitempty"`
HookURL19 string `json:"hook-url-19,omitempty"`
}

type BranchResp struct {
Values []*Branch `json:"values"`
}

type Branch struct {
Name string `json:"name"`
}