Fix bitbucket file fetching (#3604)

closes https://github.com/woodpecker-ci/woodpecker/issues/3600
This commit is contained in:
qwerty287 2024-04-09 11:30:04 +02:00 committed by GitHub
parent c9a3bfb321
commit b0c9dfd2cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View file

@ -17,6 +17,7 @@ package bitbucket
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
@ -232,9 +233,15 @@ func (c *config) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error
func (c *config) File(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline, f string) ([]byte, error) {
config, err := c.newClient(ctx, u).FindSource(r.Owner, r.Name, p.Commit, f)
if err != nil {
var rspErr internal.Error
if ok := errors.As(err, &rspErr); ok && rspErr.Status == http.StatusNotFound {
return nil, &forge_types.ErrConfigNotFound{
Configs: []string{f},
}
}
return nil, err
}
return []byte(*config), err
return []byte(*config), nil
}
// Dir fetches a folder from the bitbucket repository
@ -245,6 +252,12 @@ func (c *config) Dir(ctx context.Context, u *model.User, r *model.Repo, p *model
for {
filesResp, err := client.GetRepoFiles(r.Owner, r.Name, p.Commit, f, page)
if err != nil {
var rspErr internal.Error
if ok := errors.As(err, &rspErr); ok && rspErr.Status == http.StatusNotFound {
return nil, &forge_types.ErrConfigNotFound{
Configs: []string{f},
}
}
return nil, err
}
for _, file := range filesResp.Values {

View file

@ -18,6 +18,7 @@ package bitbucket
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
@ -179,6 +180,7 @@ func Test_bitbucket(t *testing.T) {
g.It("Should handle not found error", func() {
_, err := c.File(ctx, fakeUser, fakeRepo, fakePipeline, "file_not_found")
g.Assert(err).IsNotNil()
g.Assert(errors.Is(err, &types.ErrConfigNotFound{})).IsTrue()
})
})
@ -222,8 +224,9 @@ func Test_bitbucket(t *testing.T) {
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()
})
})