add RepoMoveOptions

This commit is contained in:
Robert Kaussow 2024-04-27 22:12:40 +02:00
parent 6b570680c1
commit 39fb6ef1c5
No known key found for this signature in database
GPG key ID: 4E692A2EAECC03C0
3 changed files with 77 additions and 5 deletions

View file

@ -63,7 +63,7 @@ type Client interface {
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)

View file

@ -11,7 +11,7 @@ const (
pathRepoPost = "%s/api/repos"
pathRepo = "%s/api/repos/%d"
pathRepoLookup = "%s/api/repos/lookup/%s"
pathRepoMove = "%s/api/repos/%d/move?to=%s"
pathRepoMove = "%s/api/repos/%d/move"
pathChown = "%s/api/repos/%d/chown"
pathRepair = "%s/api/repos/%d/repair"
pathPipelines = "%s/api/repos/%d/pipelines"
@ -52,6 +52,10 @@ type RepoPostOptions struct {
ForgeRemoteID int64
}
type RepoMoveOptions struct {
To string
}
// QueryEncode returns the URL query parameters for the PipelineListOptions.
func (opt *PipelineListOptions) QueryEncode() string {
query := opt.getURLQuery()
@ -96,6 +100,13 @@ func (opt *RepoPostOptions) QueryEncode() string {
return query.Encode()
}
// QueryEncode returns the URL query parameters for the RepoMoveOptions.
func (opt *RepoMoveOptions) QueryEncode() string {
query := make(url.Values)
query.Add("to", opt.To)
return query.Encode()
}
// Repo returns a repository by id.
func (c *client) Repo(repoID int64) (*Repo, error) {
out := new(Repo)
@ -152,9 +163,10 @@ func (c *client) RepoDel(repoID int64) error {
}
// RepoMove moves a repository.
func (c *client) RepoMove(repoID int64, newFullName string) error {
uri := fmt.Sprintf(pathRepoMove, c.addr, repoID, newFullName)
return c.post(uri, nil, nil)
func (c *client) RepoMove(repoID int64, opt RepoMoveOptions) error {
uri, _ := url.Parse(fmt.Sprintf(pathRepoMove, c.addr, repoID))
uri.RawQuery = opt.QueryEncode()
return c.post(uri.String(), nil, nil)
}
// Registry returns a registry by hostname.

View file

@ -382,3 +382,63 @@ func TestClientRepoPost(t *testing.T) {
})
}
}
func TestClientRepoMove(t *testing.T) {
tests := []struct {
name string
handler http.HandlerFunc
repoID int64
opts RepoMoveOptions
wantErr bool
}{
{
name: "success",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, "/api/repos/123/move?to=newowner", r.URL.RequestURI())
w.WriteHeader(http.StatusOK)
},
repoID: 123,
opts: RepoMoveOptions{
To: "newowner",
},
wantErr: false,
},
{
name: "server error",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
repoID: 123,
opts: RepoMoveOptions{},
wantErr: true,
},
{
name: "invalid options",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
w.WriteHeader(http.StatusBadRequest)
},
repoID: 123,
opts: RepoMoveOptions{},
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)
err := client.RepoMove(tt.repoID, tt.opts)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
})
}
}