Skip to content

Commit

Permalink
fix: Woodpecker now using remote forge ID to fetch repository
Browse files Browse the repository at this point in the history
  • Loading branch information
langecode committed Sep 27, 2023
1 parent 3ffd16c commit e6dd75c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
40 changes: 32 additions & 8 deletions server/forge/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,30 +163,54 @@ func (*client) Auth(_ context.Context, _, _ string) (string, error) {
return "", fmt.Errorf("Not Implemented")
}

func (c *client) Repo(ctx context.Context, u *model.User, _ model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
func (c *client) Repo(ctx context.Context, u *model.User, rID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
bc, err := c.newClient(u)
if err != nil {
return nil, fmt.Errorf("unable to create bitbucket client: %w", err)
}

r, _, err := bc.Projects.GetRepository(ctx, owner, name)
if err != nil {
return nil, fmt.Errorf("unable to get repository: %w", err)
var repo *bb.Repository
if rID.IsValid() {
opts := &bb.RepositorySearchOptions{Permission: bb.PermissionRepoRead, ListOptions: bb.ListOptions{Limit: 250}}
for {
repos, resp, err := bc.Projects.SearchRepositories(ctx, opts)
if err != nil {
return nil, fmt.Errorf("unable to search repositories: %w", err)
}
for _, r := range repos {
if rID == convertID(r.ID) {
repo = r
break
}
}
if resp.LastPage {
break
}
opts.Start = resp.NextPageStart
}
if repo == nil {
return nil, fmt.Errorf("unable to find repository with id: %s", rID)
}
} else {
repo, _, err = bc.Projects.GetRepository(ctx, owner, name)
if err != nil {
return nil, fmt.Errorf("unable to get repository: %w", err)
}
}

b, _, err := bc.Projects.GetDefaultBranch(ctx, owner, name)
b, _, err := bc.Projects.GetDefaultBranch(ctx, repo.Project.Key, repo.Slug)
if err != nil {
return nil, fmt.Errorf("unable to fetch default branch: %w", err)
}

perms := &model.Perm{Pull: true}
_, _, err = bc.Projects.ListWebhooks(ctx, owner, name, &bb.ListOptions{})
_, _, err = bc.Projects.ListWebhooks(ctx, repo.Project.Key, repo.Slug, &bb.ListOptions{})
if err == nil {
perms.Push = true
perms.Admin = true
}

return convertRepo(r, perms, b.DisplayID), nil
return convertRepo(repo, perms, b.DisplayID), nil
}

func (c *client) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
Expand Down Expand Up @@ -221,7 +245,7 @@ func (c *client) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error
}
for _, r := range repos {
for i, c := range all {
if c.ForgeRemoteID == model.ForgeRemoteID(fmt.Sprintf("%d", r.ID)) {
if c.ForgeRemoteID == convertID(r.ID) {
all[i].Perm = &model.Perm{Pull: true, Push: true, Admin: true}
break
}
Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucketserver/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Test_stash(t *testing.T) {
g.It("should return repository details", func() {
repo, err := c.Repo(ctx, fakeUser, model.ForgeRemoteID("1234"), "PRJ", "repo-slug")
g.Assert(err).IsNil()
g.Assert(repo.Name).Equal("repo-slug")
g.Assert(repo.Name).Equal("repo-slug-2")
g.Assert(repo.Owner).Equal("PRJ")
g.Assert(repo.Perm).Equal(&model.Perm{Pull: true})
g.Assert(repo.Branch).Equal("main")
Expand Down
6 changes: 5 additions & 1 deletion server/forge/bitbucketserver/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ func convertStatus(status model.StatusValue) bb.BuildStatusState {
}
}

func convertID(id uint64) model.ForgeRemoteID {
return model.ForgeRemoteID(fmt.Sprintf("%d", id))
}

func convertRepo(from *bb.Repository, perm *model.Perm, branch string) *model.Repo {
r := &model.Repo{
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprintf("%d", from.ID)),
ForgeRemoteID: convertID(from.ID),
Name: from.Slug,
Owner: from.Project.Key,
Branch: branch,
Expand Down
25 changes: 25 additions & 0 deletions server/forge/bitbucketserver/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (

func Server() *httptest.Server {
return mock.NewMockServer(
mock.WithRequestMatch(mock.SearchRepositories, bitbucket.RepositoryList{
ListResponse: bitbucket.ListResponse{
LastPage: true,
},
Repositories: []*bitbucket.Repository{
{
ID: uint64(123),
Slug: "repo-slug-1",
Name: "REPO Name 1",
Project: &bitbucket.Project{
ID: uint64(456),
Key: "PRJ",
},
},
{
ID: uint64(1234),
Slug: "repo-slug-2",
Name: "REPO Name 2",
Project: &bitbucket.Project{
ID: uint64(456),
Key: "PRJ",
},
},
},
}),
mock.WithRequestMatch(mock.GetRepository, bitbucket.Repository{
ID: uint64(123),
Slug: "repo-slug",
Expand Down

0 comments on commit e6dd75c

Please sign in to comment.