diff --git a/cli/pipeline/create.go b/cli/pipeline/create.go index e343c24a0..7f079f818 100644 --- a/cli/pipeline/create.go +++ b/cli/pipeline/create.go @@ -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}) } diff --git a/cli/pipeline/last.go b/cli/pipeline/last.go index 49f161142..68e6cbb2f 100644 --- a/cli/pipeline/last.go +++ b/cli/pipeline/last.go @@ -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}) } diff --git a/cli/pipeline/list.go b/cli/pipeline/list.go index afe1f3803..930e0e84f 100644 --- a/cli/pipeline/list.go +++ b/cli/pipeline/list.go @@ -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 } diff --git a/cli/pipeline/list_test.go b/cli/pipeline/list_test.go index e73bffe57..4668b2fb3 100644 --- a/cli/pipeline/list_test.go +++ b/cli/pipeline/list_test.go @@ -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() diff --git a/cli/pipeline/pipeline.go b/cli/pipeline/pipeline.go index cc86ddf07..04bd256df 100644 --- a/cli/pipeline/pipeline.go +++ b/cli/pipeline/pipeline.go @@ -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 } diff --git a/cli/pipeline/pipeline_test.go b/cli/pipeline/pipeline_test.go index ce69e5946..4bbe52b31 100644 --- a/cli/pipeline/pipeline_test.go +++ b/cli/pipeline/pipeline_test.go @@ -47,7 +47,7 @@ func TestPipelineOutput(t *testing.T) { }, } - pipelines := []woodpecker.Pipeline{ + pipelines := []*woodpecker.Pipeline{ { Number: 1, Status: "success", diff --git a/cli/pipeline/show.go b/cli/pipeline/show.go index 165c6a70a..2f14224af 100644 --- a/cli/pipeline/show.go +++ b/cli/pipeline/show.go @@ -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}) } diff --git a/web/src/components/atomic/Badge.vue b/web/src/components/atomic/Badge.vue index c8bd5d5be..64e31ddf0 100644 --- a/web/src/components/atomic/Badge.vue +++ b/web/src/components/atomic/Badge.vue @@ -1,14 +1,17 @@