Skip to content

Commit

Permalink
Support bitbucket Dir()
Browse files Browse the repository at this point in the history
  • Loading branch information
mzampetakis committed Jul 27, 2023
1 parent bc2e2c7 commit afa1cad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
53 changes: 49 additions & 4 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package bitbucket
import (
"context"
"fmt"
"golang.org/x/oauth2"
"net/http"
"net/url"

"golang.org/x/oauth2"
"path/filepath"

shared_utils "github.com/woodpecker-ci/woodpecker/shared/utils"

Expand Down Expand Up @@ -223,8 +223,53 @@ func (c *config) File(ctx context.Context, u *model.User, r *model.Repo, p *mode
return []byte(*config), err
}

func (c *config) Dir(_ context.Context, _ *model.User, _ *model.Repo, _ *model.Pipeline, _ string) ([]*forge_types.FileMeta, error) {
return nil, forge_types.ErrNotImplemented
// Dir fetches a folder from the bitbucket repository
func (c *config) Dir(ctx context.Context, u *model.User, r *model.Repo, p *model.Pipeline, f string) ([]*forge_types.FileMeta, error) {
var page *string
repoPathFiles := []*forge_types.FileMeta{}
for {
filesResp, err := c.newClient(ctx, u).GetRepoFiles(r.Owner, r.Name, p.Commit, f, page)
if err != nil {
return nil, err
}
totalFiles := 0
for _, file := range filesResp.Values {
totalFiles++
_, filename := filepath.Split(file.Path)
repoFile := forge_types.FileMeta{
Name: filename,
}
if file.Type == "commit_file" {
fileData, err := c.newClient(ctx, u).FindSource(r.Owner, r.Name, p.Commit, file.Path)
if err != nil {
return nil, err
}
if fileData != nil {
repoFile.Data = []byte(*fileData)
}
}
repoPathFiles = append(repoPathFiles, &repoFile)
}

// Check for more results page
if filesResp.Next == nil {
break
}
myUrl, err := url.Parse(*filesResp.Next)
if err != nil {
return nil, err
}
params, err := url.ParseQuery(myUrl.RawQuery)
if err != nil {
return nil, err
}
nextPage := params.Get("page")
if len(nextPage) == 0 {
break
}
page = &nextPage
}
return repoPathFiles, nil
}

// Status creates a pipeline status for the Bitbucket commit.
Expand Down
11 changes: 11 additions & 0 deletions server/forge/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
pathOrgPerms = "%s/2.0/workspaces/%s/permissions?%s"
pathPullRequests = "%s/2.0/repositories/%s/%s/pullrequests"
pathBranchCommits = "%s/2.0/repositories/%s/%s/commits/%s"
pathDir = "%s/2.0/repositories/%s/%s/src/%s%s"
)

type Client struct {
Expand Down Expand Up @@ -233,6 +234,16 @@ func (c *Client) GetWorkspace(name string) (*Workspace, error) {
return out, err
}

func (c *Client) GetRepoFiles(owner, name, revision, path string, page *string) (*DirResp, error) {
out := new(DirResp)
uri := fmt.Sprintf(pathDir, c.base, owner, name, revision, path)
if page != nil {
uri += "?page=" + *page
}
_, err := c.do(uri, get, nil, out)
return out, err
}

func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error) {
uri, err := url.Parse(rawurl)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions server/forge/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,16 @@ type CommitsResp struct {
type Commit struct {
Hash string `json:"hash"`
}

type DirResp struct {
Page uint `json:"page"`
PageLen uint `json:"pagelen"`
Next *string `json:"next"`
Values []*Dir `json:"values"`
}

type Dir struct {
Path string `json:"path"`
Type string `json:"type"`
Size uint `json:"size"`
}

0 comments on commit afa1cad

Please sign in to comment.