From e6dd75c7a176b01f87a364c378b1dd9e36e56faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thor=20Anker=20Kvisg=C3=A5rd=20Lange?= Date: Wed, 27 Sep 2023 12:06:26 +0200 Subject: [PATCH] fix: Woodpecker now using remote forge ID to fetch repository --- .../forge/bitbucketserver/bitbucketserver.go | 40 +++++++++++++++---- .../bitbucketserver/bitbucketserver_test.go | 2 +- server/forge/bitbucketserver/convert.go | 6 ++- .../forge/bitbucketserver/fixtures/handler.go | 25 ++++++++++++ 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/server/forge/bitbucketserver/bitbucketserver.go b/server/forge/bitbucketserver/bitbucketserver.go index cad83fd180..eb55efaf1a 100644 --- a/server/forge/bitbucketserver/bitbucketserver.go +++ b/server/forge/bitbucketserver/bitbucketserver.go @@ -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) { @@ -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 } diff --git a/server/forge/bitbucketserver/bitbucketserver_test.go b/server/forge/bitbucketserver/bitbucketserver_test.go index fbfbc9080f..7c4099c2db 100644 --- a/server/forge/bitbucketserver/bitbucketserver_test.go +++ b/server/forge/bitbucketserver/bitbucketserver_test.go @@ -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") diff --git a/server/forge/bitbucketserver/convert.go b/server/forge/bitbucketserver/convert.go index 986d012633..d661ac231a 100644 --- a/server/forge/bitbucketserver/convert.go +++ b/server/forge/bitbucketserver/convert.go @@ -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, diff --git a/server/forge/bitbucketserver/fixtures/handler.go b/server/forge/bitbucketserver/fixtures/handler.go index 3208a64e8b..5f82fa2706 100644 --- a/server/forge/bitbucketserver/fixtures/handler.go +++ b/server/forge/bitbucketserver/fixtures/handler.go @@ -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",