Skip to content

Commit

Permalink
Fix bitbucket file fetching (#3604)
Browse files Browse the repository at this point in the history
closes #3600
  • Loading branch information
qwerty287 authored Apr 9, 2024
1 parent c9a3bfb commit b0c9dfd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 14 additions & 1 deletion server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package bitbucket

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bitbucket
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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()
})
})

Expand Down Expand Up @@ -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()
})
})

Expand Down

0 comments on commit b0c9dfd

Please sign in to comment.