Skip to content

Commit

Permalink
Fix bitbucket dir fetching (woodpecker-ci#3668)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 authored and 6543 committed Sep 5, 2024
1 parent c7f6d97 commit 0c180b3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,14 @@ func Test_bitbucket(t *testing.T) {

g.Describe("When requesting repo directory contents", func() {
g.It("Should return the details", func() {
files, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "/dir")
files, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "dir")
g.Assert(err).IsNil()
g.Assert(len(files)).Equal(3)
g.Assert(files[0].Name).Equal("README.md")
g.Assert(string(files[0].Data)).Equal("dummy payload")
})
g.It("Should handle not found errors", func() {
_, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "dir_not_found/")
_, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "dir_not_found")
g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, &types.ErrConfigNotFound{})).IsTrue()
})
Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucket/fixtures/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func getRepoFile(c *gin.Context) {
switch c.Param("file") {
case "dir":
c.String(http.StatusOK, repoDirPayload)
case "dir_not_found/":
case "dir_not_found":
c.String(http.StatusNotFound, "")
case "file_not_found":
c.String(http.StatusNotFound, "")
Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s"
pathPullRequests = "%s/2.0/repositories/%s/%s/pullrequests?%s"
pathBranchCommits = "%s/2.0/repositories/%s/%s/commits/%s"
pathDir = "%s/2.0/repositories/%s/%s/src/%s%s"
pathDir = "%s/2.0/repositories/%s/%s/src/%s/%s"
pageSize = 100
)

Expand Down
13 changes: 11 additions & 2 deletions server/forge/bitbucketdatacenter/bitbucketdatacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,14 @@ func (c *client) File(ctx context.Context, u *model.User, r *model.Repo, p *mode
return nil, fmt.Errorf("unable to create bitbucket client: %w", err)
}

b, _, err := bc.Projects.GetTextFileContent(ctx, r.Owner, r.Name, f, p.Commit)
b, resp, err := bc.Projects.GetTextFileContent(ctx, r.Owner, r.Name, f, p.Commit)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
// requested directory might not exist
return nil, &forge_types.ErrConfigNotFound{
Configs: []string{f},
}
}
return nil, err
}
return b, nil
Expand All @@ -281,7 +287,10 @@ func (c *client) Dir(ctx context.Context, u *model.User, r *model.Repo, p *model
list, resp, err := bc.Projects.ListFiles(ctx, r.Owner, r.Name, path, opts)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
break // requested directory might not exist
// requested directory might not exist
return nil, &forge_types.ErrConfigNotFound{
Configs: []string{path},
}
}
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions server/services/config/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (f *forgeFetcherContext) fetch(c context.Context, config string) ([]*types.

fileMetas, err := f.getFirstAvailableConfig(ctx, configs)
if err == nil {
return fileMetas, err
return fileMetas, nil
}

return nil, fmt.Errorf("user defined config '%s' not found: %w", config, err)
Expand All @@ -102,7 +102,7 @@ func (f *forgeFetcherContext) fetch(c context.Context, config string) ([]*types.
// for the order see shared/constants/constants.go
fileMetas, err := f.getFirstAvailableConfig(ctx, constant.DefaultConfigOrder[:])
if err == nil {
return fileMetas, err
return fileMetas, nil
}

select {
Expand Down

0 comments on commit 0c180b3

Please sign in to comment.