Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PipelineListsOptions to woodpecker-go #3652

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
9 changes: 6 additions & 3 deletions cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func deploy(ctx context.Context, c *cli.Command) error {
var number int64
if pipelineArg == "last" {
// Fetch the pipeline number from the last pipeline
pipelines, err := client.PipelineList(repoID)
pipelines, err := client.PipelineList(repoID, woodpecker.PipelineListOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -121,9 +121,12 @@ func deploy(ctx context.Context, c *cli.Command) error {
return fmt.Errorf("please specify the target environment (i.e. production)")
}

params := internal.ParseKeyPair(c.StringSlice("param"))
opt := woodpecker.DeployOptions{
DeployTo: env,
Params: internal.ParseKeyPair(c.StringSlice("param")),
}

deploy, err := client.Deploy(repoID, number, env, params)
deploy, err := client.Deploy(repoID, number, opt)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/pipeline/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func pipelineInfo(ctx context.Context, c *cli.Command) error {
var number int64
if pipelineArg == "last" || len(pipelineArg) == 0 {
// Fetch the pipeline number from the last pipeline
pipeline, err := client.PipelineLast(repoID, "")
pipeline, err := client.PipelineLast(repoID, woodpecker.PipelineLastOptions{})
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/pipeline/last.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func pipelineLast(ctx context.Context, c *cli.Command) error {
return err
}

pipeline, err := client.PipelineLast(repoID, c.String("branch"))
opt := woodpecker.PipelineLastOptions{
Branch: c.String("branch"),
}

pipeline, err := client.PipelineLast(repoID, opt)
if err != nil {
return err
}
Expand Down
28 changes: 27 additions & 1 deletion cli/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pipeline

import (
"context"
"time"

"github.com/urfave/cli/v3"

Expand Down Expand Up @@ -48,6 +49,20 @@ var pipelineListCmd = &cli.Command{
Usage: "limit the list size",
Value: 25,
},
&cli.TimestampFlag{
Name: "before",
Usage: "only return pipelines before this RFC3339 date",
Config: cli.TimestampConfig{
Layout: time.RFC3339,
},
},
&cli.TimestampFlag{
Name: "after",
Usage: "only return pipelines after this RFC3339 date",
Config: cli.TimestampConfig{
Layout: time.RFC3339,
},
},
}...),
}

Expand All @@ -72,7 +87,18 @@ func pipelineList(_ context.Context, c *cli.Command, client woodpecker.Client) (
return resources, err
}

pipelines, err := client.PipelineList(repoID)
opt := woodpecker.PipelineListOptions{}
before := c.Timestamp("before")
after := c.Timestamp("after")

if !before.IsZero() {
opt.Before = before
}
if !after.IsZero() {
opt.After = after
}

pipelines, err := client.PipelineList(repoID, opt)
if err != nil {
return resources, err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/pipeline/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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).Return(tt.pipelines, tt.pipelineErr)
mockClient.On("PipelineList", mock.Anything, mock.Anything).Return(tt.pipelines, tt.pipelineErr)
mockClient.On("RepoLookup", mock.Anything).Return(&woodpecker.Repo{ID: tt.repoID}, nil)

command := pipelineListCmd
Expand Down
3 changes: 2 additions & 1 deletion cli/pipeline/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var pipelinePsCmd = &cli.Command{
Expand All @@ -51,7 +52,7 @@ func pipelinePs(ctx context.Context, c *cli.Command) error {

if pipelineArg == "last" || len(pipelineArg) == 0 {
// Fetch the pipeline number from the last pipeline
pipeline, err := client.PipelineLast(repoID, "")
pipeline, err := client.PipelineLast(repoID, woodpecker.PipelineLastOptions{})
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions cli/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/urfave/cli/v3"

"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var pipelineStartCmd = &cli.Command{
Expand Down Expand Up @@ -54,7 +55,7 @@ func pipelineStart(ctx context.Context, c *cli.Command) (err error) {
var number int64
if pipelineArg == "last" {
// Fetch the pipeline number from the last pipeline
pipeline, err := client.PipelineLast(repoID, "")
pipeline, err := client.PipelineLast(repoID, woodpecker.PipelineLastOptions{})
if err != nil {
return err
}
Expand All @@ -69,9 +70,11 @@ func pipelineStart(ctx context.Context, c *cli.Command) (err error) {
}
}

params := internal.ParseKeyPair(c.StringSlice("param"))
opt := woodpecker.PipelineStartOptions{
Params: internal.ParseKeyPair(c.StringSlice("param")),
}

pipeline, err := client.PipelineStart(repoID, number, params)
pipeline, err := client.PipelineStart(repoID, number, opt)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cli/repo/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/urfave/cli/v3"

"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var repoAddCmd = &cli.Command{
Expand All @@ -43,7 +44,11 @@ func repoAdd(ctx context.Context, c *cli.Command) error {
return err
}

repo, err := client.RepoPost(int64(forgeRemoteID))
opt := woodpecker.RepoPostOptions{
ForgeRemoteID: int64(forgeRemoteID),
}

repo, err := client.RepoPost(opt)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions cli/repo/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var repoListCmd = &cli.Command{
Expand All @@ -36,6 +37,10 @@ var repoListCmd = &cli.Command{
Name: "org",
Usage: "filter by organization",
},
&cli.BoolFlag{
Name: "all",
Usage: "query all repos, including inactive ones",
},
},
}

Expand All @@ -45,7 +50,11 @@ func repoList(ctx context.Context, c *cli.Command) error {
return err
}

repos, err := client.RepoList()
opt := woodpecker.RepoListOptions{
All: c.Bool("all"),
}

repos, err := client.RepoList(opt)
if err != nil || len(repos) == 0 {
return err
}
Expand All @@ -68,4 +77,4 @@ func repoList(ctx context.Context, c *cli.Command) error {
}

// Template for repository list items.
var tmplRepoList = "\x1b[33m{{ .FullName }}\x1b[0m (id: {{ .ID }}, forgeRemoteID: {{ .ForgeRemoteID }})"
var tmplRepoList = "\x1b[33m{{ .FullName }}\x1b[0m (id: {{ .ID }}, forgeRemoteID: {{ .ForgeRemoteID }}, isActive: {{ .IsActive }})"
7 changes: 6 additions & 1 deletion cli/repo/repo_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var repoSyncCmd = &cli.Command{
Expand All @@ -40,7 +41,11 @@ func repoSync(ctx context.Context, c *cli.Command) error {
return err
}

repos, err := client.RepoListOpts(true)
opt := woodpecker.RepoListOptions{
All: true,
}

repos, err := client.RepoList(opt)
if err != nil || len(repos) == 0 {
return err
}
Expand Down
21 changes: 8 additions & 13 deletions woodpecker-go/woodpecker/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,16 @@ type Client interface {

// RepoList returns a list of all repositories to which the user has explicit
// access in the host system.
RepoList() ([]*Repo, error)

// RepoListOpts returns a list of all repositories to which the user has
// explicit access in the host system.
RepoListOpts(bool) ([]*Repo, error)
RepoList(opt RepoListOptions) ([]*Repo, error)

// RepoPost activates a repository.
RepoPost(forgeRemoteID int64) (*Repo, error)
RepoPost(opt RepoPostOptions) (*Repo, error)

// RepoPatch updates a repository.
RepoPatch(repoID int64, repo *RepoPatch) (*Repo, error)

// RepoMove moves the repository
RepoMove(repoID int64, dst string) error
RepoMove(repoID int64, opt RepoMoveOptions) error

// RepoChown updates a repository owner.
RepoChown(repoID int64) (*Repo, error)
Expand All @@ -81,13 +77,12 @@ type Client interface {
// Pipeline returns a repository pipeline by number.
Pipeline(repoID, pipeline int64) (*Pipeline, error)

// PipelineLast returns the latest repository pipeline by branch. An empty branch
// will result in the default branch.
PipelineLast(repoID int64, branch string) (*Pipeline, error)
// PipelineLast returns the latest repository pipeline.
PipelineLast(repoID int64, opt PipelineLastOptions) (*Pipeline, error)

// PipelineList returns a list of recent pipelines for the
// the specified repository.
PipelineList(repoID int64) ([]*Pipeline, error)
PipelineList(repoID int64, opt PipelineListOptions) ([]*Pipeline, error)

// PipelineQueue returns a list of enqueued pipelines.
PipelineQueue() ([]*Feed, error)
Expand All @@ -96,7 +91,7 @@ type Client interface {
PipelineCreate(repoID int64, opts *PipelineOptions) (*Pipeline, error)

// PipelineStart re-starts a stopped pipeline.
PipelineStart(repoID, num int64, params map[string]string) (*Pipeline, error)
PipelineStart(repoID, num int64, opt PipelineStartOptions) (*Pipeline, error)

// PipelineStop stops the given pipeline.
PipelineStop(repoID, pipeline int64) error
Expand All @@ -115,7 +110,7 @@ type Client interface {

// Deploy triggers a deployment for an existing pipeline using the specified
// target environment.
Deploy(repoID, pipeline int64, env string, params map[string]string) (*Pipeline, error)
Deploy(repoID, pipeline int64, opt DeployOptions) (*Pipeline, error)

// LogsPurge purges the pipeline logs for the specified pipeline.
LogsPurge(repoID, pipeline int64) error
Expand Down
25 changes: 25 additions & 0 deletions woodpecker-go/woodpecker/list_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package woodpecker

import (
"fmt"
"net/url"
)

// ListOptions represents the options for the Woodpecker API pagination.
type ListOptions struct {
Page int
PerPage int
}

// getURLQuery returns the query string for the ListOptions.
func (o ListOptions) getURLQuery() url.Values {
query := make(url.Values)
if o.Page > 0 {
query.Add("page", fmt.Sprintf("%d", o.Page))
}
if o.PerPage > 0 {
query.Add("perPage", fmt.Sprintf("%d", o.PerPage))
}

return query
}
44 changes: 44 additions & 0 deletions woodpecker-go/woodpecker/list_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package woodpecker

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListOptions_getURLQuery(t *testing.T) {
tests := []struct {
name string
opts ListOptions
expected url.Values
}{
{
name: "no options",
opts: ListOptions{},
expected: url.Values{},
},
{
name: "with page",
opts: ListOptions{Page: 2},
expected: url.Values{"page": {"2"}},
},
{
name: "with per page",
opts: ListOptions{PerPage: 10},
expected: url.Values{"perPage": {"10"}},
},
{
name: "with page and per page",
opts: ListOptions{Page: 3, PerPage: 20},
expected: url.Values{"page": {"3"}, "perPage": {"20"}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := tt.opts.getURLQuery()
assert.Equal(t, tt.expected, actual)
})
}
}
Loading