Skip to content

Commit

Permalink
Add mutli-pipeline to Gitea (#225)
Browse files Browse the repository at this point in the history
* Add mutli-pipeline to Gitea client
* Add multi-pipeline to Gitea OAuth client

ref: https://woodpecker.laszlo.cloud/multi-pipeline/

Signed-off-by: Yves-Gaël BILLET <ygbillet@ifotec.com>
Co-authored-by: Yves-Gaël Billet <yg.billet@gmail.com>
  • Loading branch information
ygbillet and Yves-Gaël Billet committed Jul 5, 2021
1 parent ee3e4bb commit c699ea5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
34 changes: 33 additions & 1 deletion remote/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"net"
"net/http"
"net/url"
"path"
"path/filepath"

"code.gitea.io/sdk/gitea"
"github.com/woodpecker-ci/woodpecker/model"
Expand Down Expand Up @@ -297,7 +299,37 @@ func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([
}

func (c *client) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {
return nil, fmt.Errorf("Not implemented")
var configs []*remote.FileMeta

client, err := c.newClientToken(u.Token)
if err != nil {
return nil, err
}

// List files in repository. Path from root
tree, _, err := client.GetTrees(r.Owner, r.Name, b.Commit, true)
if err != nil {
return nil, err
}

f = path.Clean(f) // We clean path and remove trailing slash
f += "/" + "*" // construct pattern for match i.e. file in subdir
for _, e := range tree.Entries {
// Filter path matching pattern and type file (blob)
if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" {
data, err := c.File(u, r, b, e.Path)
if err != nil {
return nil, fmt.Errorf("multi-pipeline cannot get %s: %s", e.Path, err)
}

configs = append(configs, &remote.FileMeta{
Name: e.Path,
Data: data,
})
}
}

return configs, nil
}

// Status is supported by the Gitea driver.
Expand Down
34 changes: 33 additions & 1 deletion remote/gitea/gitea_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"net"
"net/http"
"net/url"
"path"
"path/filepath"

"code.gitea.io/sdk/gitea"
"github.com/woodpecker-ci/woodpecker/model"
Expand Down Expand Up @@ -280,7 +282,37 @@ func (c *oauthclient) File(u *model.User, r *model.Repo, b *model.Build, f strin
}

func (c *oauthclient) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {
return nil, fmt.Errorf("Not implemented")
var configs []*remote.FileMeta

client, err := c.newClientToken(u.Token)
if err != nil {
return nil, err
}

// List files in repository. Path from root
tree, _, err := client.GetTrees(r.Owner, r.Name, b.Commit, true)
if err != nil {
return nil, err
}

f = path.Clean(f) // We clean path and remove trailing slash
f += "/" + "*" // construct pattern for match i.e. file in subdir
for _, e := range tree.Entries {
// Filter path matching pattern and type file (blob)
if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" {
data, err := c.File(u, r, b, e.Path)
if err != nil {
return nil, fmt.Errorf("multi-pipeline cannot get %s: %s", e.Path, err)
}

configs = append(configs, &remote.FileMeta{
Name: e.Path,
Data: data,
})
}
}

return configs, nil
}

// Status is supported by the Gitea driver.
Expand Down

0 comments on commit c699ea5

Please sign in to comment.