From c699ea5789c136903a72aee935de5f51f6728c82 Mon Sep 17 00:00:00 2001 From: ygbillet Date: Mon, 5 Jul 2021 19:33:02 +0200 Subject: [PATCH] Add mutli-pipeline to Gitea (#225) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Yves-Gaël Billet --- remote/gitea/gitea.go | 34 +++++++++++++++++++++++++++++++++- remote/gitea/gitea_oauth.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index a552dcf50e..33873e53cb 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -23,6 +23,8 @@ import ( "net" "net/http" "net/url" + "path" + "path/filepath" "code.gitea.io/sdk/gitea" "github.com/woodpecker-ci/woodpecker/model" @@ -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. diff --git a/remote/gitea/gitea_oauth.go b/remote/gitea/gitea_oauth.go index fb93e92def..5fd97676b0 100644 --- a/remote/gitea/gitea_oauth.go +++ b/remote/gitea/gitea_oauth.go @@ -23,6 +23,8 @@ import ( "net" "net/http" "net/url" + "path" + "path/filepath" "code.gitea.io/sdk/gitea" "github.com/woodpecker-ci/woodpecker/model" @@ -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.