Add options to RepoList

This commit is contained in:
Robert Kaussow 2024-04-27 18:46:31 +02:00
parent 27f1cd30f1
commit fefd29b283
No known key found for this signature in database
GPG key ID: 4E692A2EAECC03C0
9 changed files with 122 additions and 30 deletions

View file

@ -78,7 +78,7 @@ func deploy(c *cli.Context) error {
var number int64
if pipelineArg == "last" {
// Fetch the pipeline number from the last pipeline
pipelines, berr := client.PipelineList(repoID, woodpecker.PipelineListsOptions{})
pipelines, berr := client.PipelineList(repoID, woodpecker.PipelineListOptions{})
if berr != nil {
return berr
}

View file

@ -70,7 +70,7 @@ func pipelineList(c *cli.Context, client woodpecker.Client) ([]woodpecker.Pipeli
return resources, err
}
pipelines, err := client.PipelineList(repoID, woodpecker.PipelineListsOptions{})
pipelines, err := client.PipelineList(repoID, woodpecker.PipelineListOptions{})
if err != nil {
return resources, err
}

View file

@ -22,6 +22,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{
@ -35,6 +36,10 @@ var repoListCmd = &cli.Command{
Name: "org",
Usage: "filter by organization",
},
&cli.BoolFlag{
Name: "all",
Usage: "query all repos, including inactive ones",
},
},
}
@ -44,7 +49,11 @@ func repoList(c *cli.Context) 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
}

View file

@ -22,6 +22,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{
@ -39,7 +40,11 @@ func repoSync(c *cli.Context) 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
}

View file

@ -54,11 +54,7 @@ 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)
@ -87,7 +83,7 @@ type Client interface {
// PipelineList returns a list of recent pipelines for the
// the specified repository.
PipelineList(repoID int64, opt PipelineListsOptions) ([]*Pipeline, error)
PipelineList(repoID int64, opt PipelineListOptions) ([]*Pipeline, error)
// PipelineQueue returns a list of enqueued pipelines.
PipelineQueue() ([]*Feed, error)

View file

@ -28,14 +28,14 @@ const (
pathRepoCron = "%s/api/repos/%d/cron/%d"
)
type PipelineListsOptions struct {
type PipelineListOptions struct {
ListOptions
Before time.Time
After time.Time
}
// QueryEncode returns the URL query parameters for the PipelineListsOptions.
func (opt *PipelineListsOptions) QueryEncode() string {
// QueryEncode returns the URL query parameters for the PipelineListOptions.
func (opt *PipelineListOptions) QueryEncode() string {
query := opt.getURLQuery()
if !opt.Before.IsZero() {
query.Add("before", opt.Before.Format(time.RFC3339))
@ -237,7 +237,7 @@ func (c *client) PipelineLast(repoID int64, branch string) (*Pipeline, error) {
// PipelineList returns a list of recent pipelines for the
// the specified repository.
func (c *client) PipelineList(repoID int64, opt PipelineListsOptions) ([]*Pipeline, error) {
func (c *client) PipelineList(repoID int64, opt PipelineListOptions) ([]*Pipeline, error) {
var out []*Pipeline
uri, _ := url.Parse(fmt.Sprintf(pathPipelines, c.addr, repoID))

View file

@ -14,7 +14,7 @@ func TestPipelineList(t *testing.T) {
tests := []struct {
name string
fixtureHandler http.HandlerFunc
opts PipelineListsOptions
opts PipelineListOptions
wantErr bool
expectedLength int
expectedIDs []int64
@ -29,7 +29,7 @@ func TestPipelineList(t *testing.T) {
_, err := fmt.Fprint(w, `[{"id":1},{"id":2}]`)
assert.NoError(t, err)
},
opts: PipelineListsOptions{
opts: PipelineListOptions{
ListOptions: ListOptions{
Page: 2,
PerPage: 10,
@ -50,7 +50,7 @@ func TestPipelineList(t *testing.T) {
_, err := fmt.Fprint(w, `[{"id":1},{"id":2}]`)
assert.NoError(t, err)
},
opts: PipelineListsOptions{},
opts: PipelineListOptions{},
expectedLength: 2,
expectedIDs: []int64{1, 2},
},
@ -59,7 +59,7 @@ func TestPipelineList(t *testing.T) {
fixtureHandler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
opts: PipelineListsOptions{},
opts: PipelineListOptions{},
wantErr: true,
},
}

View file

@ -1,6 +1,9 @@
package woodpecker
import "fmt"
import (
"fmt"
"net/url"
)
const (
pathSelf = "%s/api/user"
@ -9,6 +12,19 @@ const (
pathUser = "%s/api/users/%s"
)
type RepoListOptions struct {
All bool // query all repos, including inactive ones
}
// QueryEncode returns the URL query parameters for the RepoListOptions.
func (opt *RepoListOptions) QueryEncode() string {
query := make(url.Values)
if opt.All {
query.Add("all", "true")
}
return query.Encode()
}
// Self returns the currently authenticated user.
func (c *client) Self() (*User, error) {
out := new(User)
@ -58,18 +74,12 @@ func (c *client) UserDel(login string) error {
// RepoList returns a list of all repositories to which
// the user has explicit access in the host system.
func (c *client) RepoList() ([]*Repo, error) {
func (c *client) RepoList(opt RepoListOptions) ([]*Repo, error) {
var out []*Repo
uri := fmt.Sprintf(pathRepos, c.addr)
err := c.get(uri, &out)
return out, err
}
// RepoListOpts returns a list of all repositories to which
// the user has explicit access in the host system.
func (c *client) RepoListOpts(all bool) ([]*Repo, error) {
var out []*Repo
uri := fmt.Sprintf(pathRepos+"?all=%v", c.addr, all)
err := c.get(uri, &out)
uri, _ := url.Parse(fmt.Sprintf(pathRepos, c.addr))
uri.RawQuery = opt.QueryEncode()
err := c.get(uri.String(), &out)
return out, err
}

View file

@ -265,3 +265,75 @@ func TestClient_UserDel(t *testing.T) {
})
}
}
func TestClient_RepoList(t *testing.T) {
tests := []struct {
name string
handler http.HandlerFunc
opt RepoListOptions
expected []*Repo
wantErr bool
}{
{
name: "success",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `[{"id":1,"name":"repo1"},{"id":2,"name":"repo2"}]`)
assert.NoError(t, err)
},
opt: RepoListOptions{},
expected: []*Repo{{ID: 1, Name: "repo1"}, {ID: 2, Name: "repo2"}},
wantErr: false,
},
{
name: "empty response",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `[]`)
assert.NoError(t, err)
},
opt: RepoListOptions{},
expected: []*Repo{},
wantErr: false,
},
{
name: "server error",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
opt: RepoListOptions{},
expected: nil,
wantErr: true,
},
{
name: "with options",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/user/repos?all=true", r.URL.RequestURI())
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `[]`)
assert.NoError(t, err)
},
opt: RepoListOptions{All: true},
expected: []*Repo{},
wantErr: false,
},
}
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)
repos, err := client.RepoList(tt.opt)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expected, repos)
})
}
}