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

[Bitbucket] Use workspaces instead of deprecated/removed teams path #1166

Merged
merged 8 commits into from
Sep 18, 2022
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
9 changes: 5 additions & 4 deletions server/remote/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ func Test_bitbucket(t *testing.T) {
g.It("Should return the details", func() {
teams, err := c.Teams(ctx, fakeUser)
g.Assert(err).IsNil()
g.Assert(teams[0].Login).Equal("superfriends")
g.Assert(teams[0].Avatar).Equal("http://i.imgur.com/ZygP55A.jpg")
g.Assert(teams[0].Login).Equal("ueberdev42")
g.Assert(teams[0].Avatar).Equal("https://bitbucket.org/workspaces/ueberdev42/avatar/?ts=1658761964")
})
g.It("Should handle not found error", func() {
_, err := c.Teams(ctx, fakeUserNoTeams)
Expand Down Expand Up @@ -264,9 +264,10 @@ func Test_bitbucket(t *testing.T) {
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)

r, _, err := c.Hook(ctx, req)
r, b, err := c.Hook(ctx, req)
g.Assert(err).IsNil()
g.Assert(r.FullName).Equal("user_name/repo_name")
g.Assert(r.FullName).Equal("martinherren1984/publictestrepo")
g.Assert(b.Commit).Equal("c14c1bb05dfb1fdcdf06b31485fff61b0ea44277")
})
})
}
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
47 changes: 30 additions & 17 deletions server/remote/bitbucket/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func Handler() http.Handler {

e := gin.New()
e.POST("/site/oauth2/access_token", getOauth)
e.GET("/2.0/workspaces/", getWorkspaces)
e.GET("/2.0/repositories/:owner/:name", getRepo)
e.GET("/2.0/repositories/:owner/:name/hooks", getRepoHooks)
e.GET("/2.0/repositories/:owner/:name/src/:commit/:file", getRepoFile)
e.DELETE("/2.0/repositories/:owner/:name/hooks/:hook", deleteRepoHook)
e.POST("/2.0/repositories/:owner/:name/hooks", createRepoHook)
e.POST("/2.0/repositories/:owner/:name/commit/:commit/statuses/build", createRepoStatus)
e.GET("/2.0/repositories/:owner", getUserRepos)
e.GET("/2.0/teams/", getUserTeams)
e.GET("/2.0/user/", getUser)
e.GET("/2.0/user/permissions/repositories", getPermissions)

Expand Down Expand Up @@ -68,6 +68,18 @@ func getOauth(c *gin.Context) {
}
}

func getWorkspaces(c *gin.Context) {
// TODO: should the role be used ?
// role, _ := c.Params.Get("role")

switch c.Request.Header.Get("Authorization") {
case "Bearer teams_not_found", "Bearer c81e728d":
c.String(404, "")
default:
c.String(200, workspacesPayload)
}
}

func getRepo(c *gin.Context) {
switch c.Param("name") {
case "not_found", "repo_unknown", "repo_not_found":
Expand Down Expand Up @@ -130,15 +142,6 @@ func getUser(c *gin.Context) {
}
}

func getUserTeams(c *gin.Context) {
switch c.Request.Header.Get("Authorization") {
case "Bearer teams_not_found", "Bearer c81e728d":
c.String(404, "")
default:
c.String(200, userTeamPayload)
}
}

func getUserRepos(c *gin.Context) {
switch c.Request.Header.Get("Authorization") {
case "Bearer repos_not_found", "Bearer 70efdf2e":
Expand Down Expand Up @@ -248,18 +251,28 @@ const userRepoPayload = `
}
`

const userTeamPayload = `
const workspacesPayload = `
{
"page": 1,
"pagelen": 100,
"size": 1,
"values": [
{
"username": "superfriends",
"type": "workspace",
"uuid": "{c7a04a76-fa20-43e4-dc42-a7506db4c95b}",
"name": "Ueber Dev",
"slug": "ueberdev42",
"links": {
"avatar": {
"href": "http:\/\/i.imgur.com\/ZygP55A.jpg"
}
},
"type": "team"
"avatar": {
"href": "https://bitbucket.org/workspaces/ueberdev42/avatar/?ts=1658761964"
},
"html": {
"href": "https://bitbucket.org/ueberdev42/"
},
"self": {
"href": "https://api.bitbucket.org/2.0/workspaces/ueberdev42"
}
}
}
]
}
Expand Down
Loading