From ac127565efcfb47e11f952f5f9a6fd5885eccc0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves-Ga=C3=ABl=20Billet?= Date: Wed, 9 Jun 2021 22:26:09 +0200 Subject: [PATCH 1/4] Enhan: Add mutli-pipeline to Gitea MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit from https://woodpecker.laszlo.cloud/multi-pipeline/ Signed-off-by: Yves-Gaël BILLET --- remote/gitea/gitea.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 2a3bd65561..72268a4343 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" @@ -295,7 +297,44 @@ 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 errors string + 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 { + errors += fmt.Sprintf("cannot get %s: %s", e.Path, err) + } + + config := remote.FileMeta{ + Name: e.Path, + Data: data, + } + + configs = append(configs, &config) + } + } + + if errors != "" { + return configs, fmt.Errorf("errors get multi-pipeline: %s", errors) + } + + return configs, nil } // Status is supported by the Gitea driver. From 4d2207b457bda845239450424459dc57c063c386 Mon Sep 17 00:00:00 2001 From: ygbillet Date: Thu, 10 Jun 2021 11:02:27 +0200 Subject: [PATCH 2/4] Update remote/gitea/gitea.go Accept code format suggestion from @6543 Co-authored-by: 6543 <6543@obermui.de> --- remote/gitea/gitea.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 72268a4343..dddcd2f9a1 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -321,12 +321,10 @@ func (c *client) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([] errors += fmt.Sprintf("cannot get %s: %s", e.Path, err) } - config := remote.FileMeta{ + configs = append(configs, &remote.FileMeta{ Name: e.Path, Data: data, - } - - configs = append(configs, &config) + }) } } From 5011c104a3ae4476b28de9fa66dbb7ca86843ffa Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 29 Jun 2021 00:59:04 +0200 Subject: [PATCH 3/4] fail fast --- remote/gitea/gitea.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 2b454865f7..33873e53cb 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -299,7 +299,6 @@ 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) { - var errors string var configs []*remote.FileMeta client, err := c.newClientToken(u.Token) @@ -320,7 +319,7 @@ func (c *client) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([] if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" { data, err := c.File(u, r, b, e.Path) if err != nil { - errors += fmt.Sprintf("cannot get %s: %s", e.Path, err) + return nil, fmt.Errorf("multi-pipeline cannot get %s: %s", e.Path, err) } configs = append(configs, &remote.FileMeta{ @@ -330,10 +329,6 @@ func (c *client) Dir(u *model.User, r *model.Repo, b *model.Build, f string) ([] } } - if errors != "" { - return configs, fmt.Errorf("errors get multi-pipeline: %s", errors) - } - return configs, nil } From 3d2376939ba979a90fa844f37bdb6b564cb5373d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yves-Ga=C3=ABl=20Billet?= Date: Wed, 30 Jun 2021 13:14:42 +0200 Subject: [PATCH 4/4] Add multi-pipeline to Gitea OAuth client. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yves-Gaël BILLET --- remote/gitea/gitea_oauth.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) 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.