[fix] Replace /teams API w/ /workspaces endpoints

Signed-off-by: Rahul M Chheda <rahul.chheda@accurics.com>
This commit is contained in:
Rahul M Chheda 2022-01-27 14:08:48 +05:30
parent 49e15945a2
commit 2bc4ad6b56
2 changed files with 25 additions and 25 deletions

View File

@ -351,7 +351,7 @@ func (b *bitbucketConnector) userEmail(ctx context.Context, client *http.Client)
// getGroups retrieves Bitbucket teams a user is in, if any. // getGroups retrieves Bitbucket teams a user is in, if any.
func (b *bitbucketConnector) getGroups(ctx context.Context, client *http.Client, groupScope bool, userLogin string) ([]string, error) { func (b *bitbucketConnector) getGroups(ctx context.Context, client *http.Client, groupScope bool, userLogin string) ([]string, error) {
bitbucketTeams, err := b.userTeams(ctx, client) bitbucketTeams, err := b.userWorkspaces(ctx, client)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -369,33 +369,33 @@ func (b *bitbucketConnector) getGroups(ctx context.Context, client *http.Client,
return nil, nil return nil, nil
} }
type teamName struct { type workspaceSlug struct {
Name string `json:"username"` // The "username" from Bitbucket Cloud is actually the team name here Slug string `json:"slug"`
} }
type team struct { type workspace struct {
Team teamName `json:"team"` Workspace workspaceSlug `json:"workspace"`
} }
type userTeamsResponse struct { type userWorkspacesResponse struct {
pagedResponse pagedResponse
Values []team Values []workspace `json:"values"`
} }
func (b *bitbucketConnector) userTeams(ctx context.Context, client *http.Client) ([]string, error) { func (b *bitbucketConnector) userWorkspaces(ctx context.Context, client *http.Client) ([]string, error) {
var teams []string var teams []string
apiURL := b.apiURL + "/user/permissions/teams" apiURL := b.apiURL + "/user/permissions/workspaces"
for { for {
// https://developer.atlassian.com/bitbucket/api/2/reference/resource/user/permissions/teams // https://developer.atlassian.com/cloud/bitbucket/rest/api-group-workspaces/#api-workspaces-get
var response userTeamsResponse var response userWorkspacesResponse
if err := get(ctx, client, apiURL, &response); err != nil { if err := get(ctx, client, apiURL, &response); err != nil {
return nil, fmt.Errorf("bitbucket: get user teams: %v", err) return nil, fmt.Errorf("bitbucket: get user teams: %v", err)
} }
for _, value := range response.Values { for _, value := range response.Values {
teams = append(teams, value.Team.Name) teams = append(teams, value.Workspace.Slug)
} }
if response.Next == nil { if response.Next == nil {

View File

@ -14,28 +14,28 @@ import (
) )
func TestUserGroups(t *testing.T) { func TestUserGroups(t *testing.T) {
teamsResponse := userTeamsResponse{ teamsResponse := userWorkspacesResponse{
pagedResponse: pagedResponse{ pagedResponse: pagedResponse{
Size: 3, Size: 3,
Page: 1, Page: 1,
PageLen: 10, PageLen: 10,
}, },
Values: []team{ Values: []workspace{
{Team: teamName{Name: "team-1"}}, {Workspace: workspaceSlug{Slug: "team-1"}},
{Team: teamName{Name: "team-2"}}, {Workspace: workspaceSlug{Slug: "team-2"}},
{Team: teamName{Name: "team-3"}}, {Workspace: workspaceSlug{Slug: "team-3"}},
}, },
} }
s := newTestServer(map[string]interface{}{ s := newTestServer(map[string]interface{}{
"/user/permissions/teams": teamsResponse, "/user/permissions/workspaces": teamsResponse,
"/groups/team-1": []group{{Slug: "administrators"}, {Slug: "members"}}, "/groups/team-1": []group{{Slug: "administrators"}, {Slug: "members"}},
"/groups/team-2": []group{{Slug: "everyone"}}, "/groups/team-2": []group{{Slug: "everyone"}},
"/groups/team-3": []group{}, "/groups/team-3": []group{},
}) })
connector := bitbucketConnector{apiURL: s.URL, legacyAPIURL: s.URL} connector := bitbucketConnector{apiURL: s.URL, legacyAPIURL: s.URL}
groups, err := connector.userTeams(context.Background(), newClient()) groups, err := connector.userWorkspaces(context.Background(), newClient())
expectNil(t, err) expectNil(t, err)
expectEquals(t, groups, []string{ expectEquals(t, groups, []string{
@ -45,7 +45,7 @@ func TestUserGroups(t *testing.T) {
}) })
connector.includeTeamGroups = true connector.includeTeamGroups = true
groups, err = connector.userTeams(context.Background(), newClient()) groups, err = connector.userWorkspaces(context.Background(), newClient())
expectNil(t, err) expectNil(t, err)
expectEquals(t, groups, []string{ expectEquals(t, groups, []string{
@ -62,11 +62,11 @@ func TestUserGroups(t *testing.T) {
func TestUserWithoutTeams(t *testing.T) { func TestUserWithoutTeams(t *testing.T) {
s := newTestServer(map[string]interface{}{ s := newTestServer(map[string]interface{}{
"/user/permissions/teams": userTeamsResponse{}, "/user/permissions/workspaces": userWorkspacesResponse{},
}) })
connector := bitbucketConnector{apiURL: s.URL} connector := bitbucketConnector{apiURL: s.URL}
groups, err := connector.userTeams(context.Background(), newClient()) groups, err := connector.userWorkspaces(context.Background(), newClient())
expectNil(t, err) expectNil(t, err)
expectEquals(t, len(groups), 0) expectEquals(t, len(groups), 0)