Use pagination helper to list pipelines in cli (#4478)

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Robert Kaussow 2024-12-01 20:39:47 +01:00 committed by GitHub
parent 7189cd61bf
commit 3c31284e7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 62 additions and 86 deletions

View file

@ -74,5 +74,5 @@ func pipelineCreate(ctx context.Context, c *cli.Command) error {
return err
}
return pipelineOutput(c, []woodpecker.Pipeline{*pipeline})
return pipelineOutput(c, []*woodpecker.Pipeline{pipeline})
}

View file

@ -58,5 +58,5 @@ func pipelineLast(ctx context.Context, c *cli.Command) error {
return err
}
return pipelineOutput(c, []woodpecker.Pipeline{*pipeline})
return pipelineOutput(c, []*woodpecker.Pipeline{pipeline})
}

View file

@ -22,6 +22,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
shared_utils "go.woodpecker-ci.org/woodpecker/v2/shared/utils"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)
@ -77,60 +78,51 @@ func List(ctx context.Context, c *cli.Command) error {
if err != nil {
return err
}
resources, err := pipelineList(c, client)
pipelines, err := pipelineList(c, client)
if err != nil {
return err
}
return pipelineOutput(c, resources)
return pipelineOutput(c, pipelines)
}
func pipelineList(c *cli.Command, client woodpecker.Client) ([]woodpecker.Pipeline, error) {
resources := make([]woodpecker.Pipeline, 0)
func pipelineList(c *cli.Command, client woodpecker.Client) ([]*woodpecker.Pipeline, error) {
repoIDOrFullName := c.Args().First()
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return resources, err
return nil, err
}
opt := woodpecker.PipelineListOptions{}
before := c.Timestamp("before")
after := c.Timestamp("after")
if !before.IsZero() {
if before := c.Timestamp("before"); !before.IsZero() {
opt.Before = before
}
if !after.IsZero() {
if after := c.Timestamp("after"); !after.IsZero() {
opt.After = after
}
pipelines, err := client.PipelineList(repoID, opt)
if err != nil {
return resources, err
}
branch := c.String("branch")
event := c.String("event")
status := c.String("status")
limit := int(c.Int("limit"))
var count int
for _, pipeline := range pipelines {
if count >= limit {
break
}
if branch != "" && pipeline.Branch != branch {
continue
}
if event != "" && pipeline.Event != event {
continue
}
if status != "" && pipeline.Status != status {
continue
}
resources = append(resources, *pipeline)
count++
pipelines, err := shared_utils.Paginate(func(page int) ([]*woodpecker.Pipeline, error) {
return client.PipelineList(repoID,
woodpecker.PipelineListOptions{
ListOptions: woodpecker.ListOptions{
Page: page,
},
Before: opt.Before,
After: opt.After,
Branch: branch,
Events: []string{event},
Status: status,
},
)
}, limit)
if err != nil {
return nil, err
}
return resources, nil
return pipelines, nil
}

View file

@ -22,7 +22,7 @@ func TestPipelineList(t *testing.T) {
pipelines []*woodpecker.Pipeline
pipelineErr error
args []string
expected []woodpecker.Pipeline
expected []*woodpecker.Pipeline
wantErr error
}{
{
@ -34,53 +34,12 @@ func TestPipelineList(t *testing.T) {
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
args: []string{"ls", "repo/name"},
expected: []woodpecker.Pipeline{
expected: []*woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
},
{
name: "filter by branch",
repoID: 1,
pipelines: []*woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
args: []string{"ls", "--branch", "main", "repo/name"},
expected: []woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
},
{
name: "filter by event",
repoID: 1,
pipelines: []*woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
args: []string{"ls", "--event", "push", "repo/name"},
expected: []woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
},
{
name: "filter by status",
repoID: 1,
pipelines: []*woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
args: []string{"ls", "--status", "success", "repo/name"},
expected: []woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
},
},
{
name: "limit results",
repoID: 1,
@ -90,7 +49,7 @@ func TestPipelineList(t *testing.T) {
{ID: 3, Branch: "main", Event: "push", Status: "failure"},
},
args: []string{"ls", "--limit", "2", "repo/name"},
expected: []woodpecker.Pipeline{
expected: []*woodpecker.Pipeline{
{ID: 1, Branch: "main", Event: "push", Status: "success"},
{ID: 2, Branch: "develop", Event: "pull_request", Status: "running"},
},
@ -107,7 +66,15 @@ func TestPipelineList(t *testing.T) {
for _, tt := range testtases {
t.Run(tt.name, func(t *testing.T) {
mockClient := mocks.NewClient(t)
mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(tt.pipelines, tt.pipelineErr)
mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(func(_ int64, opt woodpecker.PipelineListOptions) ([]*woodpecker.Pipeline, error) {
if tt.pipelineErr != nil {
return nil, tt.pipelineErr
}
if opt.Page == 1 {
return tt.pipelines, nil
}
return []*woodpecker.Pipeline{}, nil
}).Maybe()
mockClient.On("RepoLookup", mock.Anything).Return(&woodpecker.Repo{ID: tt.repoID}, nil)
command := buildPipelineListCmd()

View file

@ -50,7 +50,7 @@ var Command = &cli.Command{
},
}
func pipelineOutput(c *cli.Command, resources []woodpecker.Pipeline, fd ...io.Writer) error {
func pipelineOutput(c *cli.Command, pipelines []*woodpecker.Pipeline, fd ...io.Writer) error {
outFmt, outOpt := output.ParseOutputOptions(c.String("output"))
noHeader := c.Bool("output-no-headers")
@ -74,7 +74,7 @@ func pipelineOutput(c *cli.Command, resources []woodpecker.Pipeline, fd ...io.Wr
if err != nil {
return err
}
if err := tmpl.Execute(out, resources); err != nil {
if err := tmpl.Execute(out, pipelines); err != nil {
return err
}
case "table":
@ -89,7 +89,7 @@ func pipelineOutput(c *cli.Command, resources []woodpecker.Pipeline, fd ...io.Wr
if !noHeader {
table.WriteHeader(cols)
}
for _, resource := range resources {
for _, resource := range pipelines {
if err := table.Write(cols, resource); err != nil {
return err
}

View file

@ -47,7 +47,7 @@ func TestPipelineOutput(t *testing.T) {
},
}
pipelines := []woodpecker.Pipeline{
pipelines := []*woodpecker.Pipeline{
{
Number: 1,
Status: "success",

View file

@ -65,5 +65,5 @@ func pipelineShow(ctx context.Context, c *cli.Command) error {
return err
}
return pipelineOutput(c, []woodpecker.Pipeline{*pipeline})
return pipelineOutput(c, []*woodpecker.Pipeline{pipeline})
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
@ -31,8 +32,12 @@ const (
type PipelineListOptions struct {
ListOptions
Before time.Time
After time.Time
Before time.Time
After time.Time
Branch string
Events []string
RefContains string
Status string
}
type CronListOptions struct {
@ -77,6 +82,18 @@ func (opt *PipelineListOptions) QueryEncode() string {
if !opt.After.IsZero() {
query.Add("after", opt.After.Format(time.RFC3339))
}
if opt.Branch != "" {
query.Add("branch", opt.Branch)
}
if len(opt.Events) > 0 {
query.Add("event", strings.Join(opt.Events, ","))
}
if opt.RefContains != "" {
query.Add("ref", opt.RefContains)
}
if opt.Status != "" {
query.Add("status", opt.Status)
}
return query.Encode()
}