diff --git a/server/forge/bitbucketdatacenter/bitbucketdatacenter.go b/server/forge/bitbucketdatacenter/bitbucketdatacenter.go index ccb7beb4d..354df61b6 100644 --- a/server/forge/bitbucketdatacenter/bitbucketdatacenter.go +++ b/server/forge/bitbucketdatacenter/bitbucketdatacenter.go @@ -570,10 +570,32 @@ func (c *client) updatePipelineFromCommit(ctx context.Context, u *model.User, r return p, nil } -// Teams is not supported. -func (*client) Teams(_ context.Context, _ *model.User) ([]*model.Team, error) { - var teams []*model.Team - return teams, nil +// Teams fetches all the projects for a given user and converts them into teams. +func (c *client) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) { + opts := &bb.ListOptions{Limit: listLimit} + allProjects := make([]*bb.Project, 0) + + bc, err := c.newClient(ctx, u) + if err != nil { + return nil, fmt.Errorf("unable to create client: %w", err) + } + + for { + projects, resp, err := bc.Projects.ListProjects(ctx, opts) + if err != nil { + return nil, fmt.Errorf("unable to fetch projects: %w", err) + } + + allProjects = append(allProjects, projects...) + + if resp.LastPage { + break + } + + opts.Start = resp.NextPageStart + } + + return convertProjectsToTeams(allProjects, bc), nil } // TeamPerm is not supported. diff --git a/server/forge/bitbucketdatacenter/convert.go b/server/forge/bitbucketdatacenter/convert.go index f7c99cf04..d1fa741d4 100644 --- a/server/forge/bitbucketdatacenter/convert.go +++ b/server/forge/bitbucketdatacenter/convert.go @@ -172,3 +172,15 @@ func updateUserCredentials(u *model.User, t *oauth2.Token) { u.RefreshToken = t.RefreshToken u.Expiry = t.Expiry.UTC().Unix() } + +func convertProjectsToTeams(projects []*bb.Project, client *bb.Client) []*model.Team { + teams := make([]*model.Team, 0) + for _, project := range projects { + team := &model.Team{ + Login: project.Key, + Avatar: fmt.Sprintf("%s/projects/%s/avatar.png", client.BaseURL, project.Key), + } + teams = append(teams, team) + } + return teams +} diff --git a/server/forge/bitbucketdatacenter/convert_test.go b/server/forge/bitbucketdatacenter/convert_test.go index 10ba54999..74ee30b98 100644 --- a/server/forge/bitbucketdatacenter/convert_test.go +++ b/server/forge/bitbucketdatacenter/convert_test.go @@ -15,6 +15,7 @@ package bitbucketdatacenter import ( + "net/url" "testing" "time" @@ -314,3 +315,49 @@ func Test_convertUser(t *testing.T) { ForgeRemoteID: "1", }, to) } + +func Test_convertProjectsToTeams(t *testing.T) { + tests := []struct { + projects []*bb.Project + baseURL string + expected []*model.Team + }{ + { + projects: []*bb.Project{ + { + Key: "PRJ1", + }, + { + Key: "PRJ2", + }, + }, + baseURL: "https://base.url", + expected: []*model.Team{ + { + Login: "PRJ1", + Avatar: "https://base.url/projects/PRJ1/avatar.png", + }, + { + Login: "PRJ2", + Avatar: "https://base.url/projects/PRJ2/avatar.png", + }, + }, + }, + { + projects: []*bb.Project{}, + baseURL: "https://base.url", + expected: []*model.Team{}, + }, + } + + for _, tt := range tests { + // Parse the baseURL string into a *url.URL + parsedURL, err := url.Parse(tt.baseURL) + assert.NoError(t, err) + + mockClient := &bb.Client{BaseURL: parsedURL} + actual := convertProjectsToTeams(tt.projects, mockClient) + + assert.Equal(t, tt.expected, actual) + } +}