add PipelineLastOptions

This commit is contained in:
Robert Kaussow 2024-04-27 21:34:33 +02:00
parent 9767201c73
commit ccbc1d5d5e
No known key found for this signature in database
GPG key ID: 4E692A2EAECC03C0
7 changed files with 102 additions and 16 deletions

View file

@ -47,7 +47,7 @@ func pipelineInfo(c *cli.Context) 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
}

View file

@ -47,7 +47,11 @@ func pipelineLast(c *cli.Context) 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
}

View file

@ -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 pipelinePsCmd = &cli.Command{
@ -49,7 +50,7 @@ func pipelinePs(c *cli.Context) 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
}

View file

@ -54,7 +54,7 @@ func pipelineStart(c *cli.Context) (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
}

View file

@ -77,9 +77,8 @@ 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.

View file

@ -36,11 +36,15 @@ type PipelineListOptions struct {
type DeployOptions struct {
DeployTo string // override the target deploy value
Params map[string]string // custom parameters to be injected into the step environment. Format: KEY=value
Params map[string]string // custom KEY=value parameters to be injected into the step environment
}
type PipelineStartOptions struct {
Params map[string]string // custom parameters to be injected into the step environment. Format: KEY=value
Params map[string]string // custom KEY=value parameters to be injected into the step environment
}
type PipelineLastOptions struct {
Branch string // last pipeline from given branch, an empty branch will result in the default branch
}
// QueryEncode returns the URL query parameters for the PipelineListOptions.
@ -71,6 +75,15 @@ func (opt *PipelineStartOptions) QueryEncode() string {
return query.Encode()
}
// QueryEncode returns the URL query parameters for the PipelineLastOptions.
func (opt *PipelineLastOptions) QueryEncode() string {
query := make(url.Values)
if opt.Branch != "" {
query.Add("branch", opt.Branch)
}
return query.Encode()
}
// Repo returns a repository by id.
func (c *client) Repo(repoID int64) (*Repo, error) {
out := new(Repo)
@ -249,14 +262,12 @@ func (c *client) Pipeline(repoID, pipeline int64) (*Pipeline, error) {
return out, err
}
// Pipeline returns the latest repository pipeline by branch.
func (c *client) PipelineLast(repoID int64, branch string) (*Pipeline, error) {
// Pipeline returns the latest repository pipeline.
func (c *client) PipelineLast(repoID int64, opt PipelineLastOptions) (*Pipeline, error) {
out := new(Pipeline)
uri := fmt.Sprintf(pathPipeline, c.addr, repoID, "latest")
if len(branch) != 0 {
uri += "?branch=" + branch
}
err := c.get(uri, out)
uri, _ := url.Parse(fmt.Sprintf(pathPipeline, c.addr, repoID, "latest"))
uri.RawQuery = opt.QueryEncode()
err := c.get(uri.String(), out)
return out, err
}

View file

@ -240,3 +240,74 @@ func TestClientPipelineStart(t *testing.T) {
})
}
}
func TestClient_PipelineLast(t *testing.T) {
tests := []struct {
name string
handler http.HandlerFunc
repoID int64
opt PipelineLastOptions
expected *Pipeline
wantErr bool
}{
{
name: "success",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/repos/1/pipelines/latest?branch=main", r.URL.Path+"?"+r.URL.RawQuery)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `{"id":1,"number":1,"status":"success","event":"push","branch":"main"}`)
assert.NoError(t, err)
},
repoID: 1,
opt: PipelineLastOptions{Branch: "main"},
expected: &Pipeline{
ID: 1,
Number: 1,
Status: "success",
Event: "push",
Branch: "main",
},
wantErr: false,
},
{
name: "server error",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
repoID: 1,
opt: PipelineLastOptions{},
expected: nil,
wantErr: true,
},
{
name: "invalid response",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `invalid json`)
assert.NoError(t, err)
},
repoID: 1,
opt: PipelineLastOptions{},
expected: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(tt.handler)
defer ts.Close()
client := NewClient(ts.URL, http.DefaultClient)
pipeline, err := client.PipelineLast(tt.repoID, tt.opt)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expected, pipeline)
})
}
}