mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-10 09:35:26 +00:00
feat(bitbucketdatacenter): Add support for fetching and converting projects to teams (#4663)
Co-authored-by: tal <tal@localhost>
This commit is contained in:
parent
a41d66282c
commit
d022bf229a
3 changed files with 85 additions and 4 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue