Skip to content

Commit

Permalink
[Bitbucket] Use workspaces instead of deprecated/removed teams path
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Herren <martin.herren@gmail.com>
  • Loading branch information
Martin Herren authored and MartinHerren committed Sep 7, 2022
1 parent 6433dfe commit 34c93f1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
16 changes: 6 additions & 10 deletions server/remote/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ func (c *config) Refresh(ctx context.Context, user *model.User) (bool, error) {

// 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) {
opts := &internal.ListTeamOpts{
opts := &internal.ListWorkspacesOpts{
PageLen: 100,
Role: "member",
}
resp, err := c.newClient(ctx, u).ListTeams(opts)
resp, err := c.newClient(ctx, u).ListWorkspaces(opts)
if err != nil {
return nil, err
}
return convertTeamList(resp.Values), nil
return convertWorkspaceList(resp.Values), nil
}

// Repo returns the named Bitbucket repository.
Expand All @@ -160,20 +160,16 @@ func (c *config) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error

var all []*model.Repo

accounts := []string{u.Login}
resp, err := client.ListTeams(&internal.ListTeamOpts{
resp, err := client.ListWorkspaces(&internal.ListWorkspacesOpts{
PageLen: 100,
Role: "member",
})
if err != nil {
return all, err
}
for _, team := range resp.Values {
accounts = append(accounts, team.Login)
}

for _, account := range accounts {
repos, err := client.ListReposAll(account)
for _, workspace := range resp.Values {
repos, err := client.ListReposAll(workspace.Slug)
if err != nil {
return all, err
}
Expand Down
10 changes: 5 additions & 5 deletions server/remote/bitbucket/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ func convertUser(from *internal.Account, token *oauth2.Token) *model.User {

// convertTeamList is a helper function used to convert a Bitbucket team list
// structure to the Woodpecker Team structure.
func convertTeamList(from []*internal.Account) []*model.Team {
func convertWorkspaceList(from []*internal.Workspace) []*model.Team {
var teams []*model.Team
for _, team := range from {
teams = append(teams, convertTeam(team))
for _, workspace := range from {
teams = append(teams, convertWorkspace(workspace))
}
return teams
}

// convertTeam is a helper function used to convert a Bitbucket team account
// structure to the Woodpecker Team structure.
func convertTeam(from *internal.Account) *model.Team {
func convertWorkspace(from *internal.Workspace) *model.Team {
return &model.Team{
Login: from.Login,
Login: from.Slug,
Avatar: from.Links.Avatar.Href,
}
}
Expand Down
12 changes: 6 additions & 6 deletions server/remote/bitbucket/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ func Test_helper(t *testing.T) {
})

g.It("should convert team", func() {
from := &internal.Account{Login: "octocat"}
from := &internal.Workspace{Slug: "octocat"}
from.Links.Avatar.Href = "http://..."
to := convertTeam(from)
to := convertWorkspace(from)
g.Assert(to.Avatar).Equal(from.Links.Avatar.Href)
g.Assert(to.Login).Equal(from.Login)
g.Assert(to.Login).Equal(from.Slug)
})

g.It("should convert team list", func() {
from := &internal.Account{Login: "octocat"}
from := &internal.Workspace{Slug: "octocat"}
from.Links.Avatar.Href = "http://..."
to := convertTeamList([]*internal.Account{from})
to := convertWorkspaceList([]*internal.Workspace{from})
g.Assert(to[0].Avatar).Equal(from.Links.Avatar.Href)
g.Assert(to[0].Login).Equal(from.Login)
g.Assert(to[0].Login).Equal(from.Slug)
})

g.It("should convert user", func() {
Expand Down
16 changes: 8 additions & 8 deletions server/remote/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails"
pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=%q"
pathTeams = "%s/2.0/teams/?%s"
pathWorkspace = "%s/2.0/workspaces/?%s"
pathRepo = "%s/2.0/repositories/%s/%s"
pathRepos = "%s/2.0/repositories/%s?%s"
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
Expand Down Expand Up @@ -85,9 +85,9 @@ func (c *Client) ListEmail() (*EmailResp, error) {
return out, err
}

func (c *Client) ListTeams(opts *ListTeamOpts) (*AccountResp, error) {
out := new(AccountResp)
uri := fmt.Sprintf(pathTeams, c.base, opts.Encode())
func (c *Client) ListWorkspaces(opts *ListWorkspacesOpts) (*WorkspacesResp, error) {
out := new(WorkspacesResp)
uri := fmt.Sprintf(pathWorkspace, c.base, opts.Encode())
_, err := c.do(uri, get, nil, out)
return out, err
}
Expand All @@ -99,19 +99,19 @@ func (c *Client) FindRepo(owner, name string) (*Repo, error) {
return out, err
}

func (c *Client) ListRepos(account string, opts *ListOpts) (*RepoResp, error) {
func (c *Client) ListRepos(workspace string, opts *ListOpts) (*RepoResp, error) {
out := new(RepoResp)
uri := fmt.Sprintf(pathRepos, c.base, account, opts.Encode())
uri := fmt.Sprintf(pathRepos, c.base, workspace, opts.Encode())
_, err := c.do(uri, get, nil, out)
return out, err
}

func (c *Client) ListReposAll(account string) ([]*Repo, error) {
func (c *Client) ListReposAll(workspace string) ([]*Repo, error) {
page := 1
var repos []*Repo

for {
resp, err := c.ListRepos(account, &ListOpts{Page: page, PageLen: 100})
resp, err := c.ListRepos(workspace, &ListOpts{Page: page, PageLen: 100})
if err != nil {
return repos, err
}
Expand Down
24 changes: 16 additions & 8 deletions server/remote/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ type Account struct {
Links Links `json:"links"`
}

type AccountResp struct {
Page int `json:"page"`
Pages int `json:"pagelen"`
Size int `json:"size"`
Next string `json:"next"`
Values []*Account `json:"values"`
type Workspace struct {
UUID string `json:"uuid"`
Slug string `json:"slug"`
Name string `json:"name"`
Type string `json:"type"`
Links Links `json:"links"`
}

type WorkspacesResp struct {
Page int `json:"page"`
Pages int `json:"pagelen"`
Size int `json:"size"`
Next string `json:"next"`
Values []*Workspace `json:"values"`
}

type BuildStatus struct {
Expand Down Expand Up @@ -201,13 +209,13 @@ func (o *ListOpts) Encode() string {
return params.Encode()
}

type ListTeamOpts struct {
type ListWorkspacesOpts struct {
Page int
PageLen int
Role string
}

func (o *ListTeamOpts) Encode() string {
func (o *ListWorkspacesOpts) Encode() string {
params := url.Values{}
if o.Page != 0 {
params.Set("page", strconv.Itoa(o.Page))
Expand Down

0 comments on commit 34c93f1

Please sign in to comment.