chore: remove some duplication and add a first few tests
This commit is contained in:
+28
-35
@@ -12,20 +12,16 @@ import (
|
||||
)
|
||||
|
||||
func New(token string) *RestClient {
|
||||
return &RestClient{token: token, client: http.DefaultClient}
|
||||
return &RestClient{token: token, client: http.DefaultClient, baseUrl: "https://gitlab.com"}
|
||||
}
|
||||
|
||||
type RestClient struct {
|
||||
client *http.Client
|
||||
token string
|
||||
client *http.Client
|
||||
token string
|
||||
baseUrl string
|
||||
}
|
||||
|
||||
func (r *RestClient) UpdateCleanupPolicy(project string, versions []string) error {
|
||||
encoded := url.QueryEscape(project)
|
||||
reqUrl, err := url.Parse(fmt.Sprintf("https://gitlab.com/api/v4/projects/%s", encoded))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options := ProjectConfig{
|
||||
ContainerExpirationPolicyAttributes: ContainerExpirationPolicyAttributes{
|
||||
Cadence: "1d",
|
||||
@@ -38,7 +34,22 @@ func (r *RestClient) UpdateCleanupPolicy(project string, versions []string) erro
|
||||
}
|
||||
buff := &bytes.Buffer{}
|
||||
encoder := json.NewEncoder(buff)
|
||||
err = encoder.Encode(&options)
|
||||
err := encoder.Encode(&options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.projectApiCall("PUT", project, "", io.NopCloser(buff), nil)
|
||||
}
|
||||
|
||||
func (r *RestClient) GetTags(project string) ([]Tag, error) {
|
||||
var tags []Tag
|
||||
err := r.projectApiCall("GET", project, "/repository/tags", nil, &tags)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
func (r *RestClient) projectApiCall(method, project string, api string, body io.ReadCloser, response interface{}) error {
|
||||
encoded := url.QueryEscape(project)
|
||||
reqUrl, err := url.Parse(fmt.Sprintf("%s/api/v4/projects/%s%s", r.baseUrl, encoded, api))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -46,38 +57,20 @@ func (r *RestClient) UpdateCleanupPolicy(project string, versions []string) erro
|
||||
header.Add("Content-Type", "application/json;charset=UTF-8")
|
||||
header.Add("PRIVATE-TOKEN", r.token)
|
||||
req := &http.Request{
|
||||
Method: "PUT",
|
||||
URL: reqUrl,
|
||||
Header: header,
|
||||
Body: io.NopCloser(buff),
|
||||
}
|
||||
_, err = r.client.Do(req)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *RestClient) GetTags(project string) ([]Tag, error) {
|
||||
encoded := url.QueryEscape(project)
|
||||
reqUrl, err := url.Parse(fmt.Sprintf("https://gitlab.com/api/v4/projects/%s/repository/tags", encoded))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
header := http.Header{}
|
||||
header.Add("Content-Type", "application/json;charset=UTF-8")
|
||||
header.Add("PRIVATE-TOKEN", r.token)
|
||||
req := &http.Request{
|
||||
Method: "GET",
|
||||
Method: method,
|
||||
URL: reqUrl,
|
||||
Header: header,
|
||||
Body: body,
|
||||
}
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var tags []Tag
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(&tags)
|
||||
return tags, err
|
||||
if resp.StatusCode == http.StatusOK && response != nil {
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(response)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type ProjectConfig struct {
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRestClient_UpdateCleanupPolicy(t *testing.T) {
|
||||
type args struct {
|
||||
project string
|
||||
versions []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
handler func(t *testing.T) http.HandlerFunc
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
args: args{
|
||||
project: "unboundsoftware/dummy",
|
||||
versions: []string{"1.0", "1.1"},
|
||||
},
|
||||
handler: func(t *testing.T) http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
buff, err := io.ReadAll(request.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "{\"container_expiration_policy_attributes\":{\"cadence\":\"1d\",\"enabled\":true,\"keep_n\":10,\"older_than\":\"14d\",\"name_regex\":\".*\",\"name_regex_keep\":\"(main|master|1.0|1.1)\"}}\n", string(buff))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(tt.handler(t))
|
||||
defer server.Close()
|
||||
r := &RestClient{
|
||||
client: http.DefaultClient,
|
||||
token: "some-gitlab-token",
|
||||
baseUrl: server.URL,
|
||||
}
|
||||
tt.wantErr(t, r.UpdateCleanupPolicy(tt.args.project, tt.args.versions), fmt.Sprintf("UpdateCleanupPolicy(%v, %v)", tt.args.project, tt.args.versions))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestClient_GetTags(t *testing.T) {
|
||||
type args struct {
|
||||
project string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
handler func(t *testing.T) http.HandlerFunc
|
||||
want []Tag
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}{
|
||||
{
|
||||
name: "error",
|
||||
args: args{
|
||||
project: "unboundsoftware/dummy",
|
||||
},
|
||||
handler: func(t *testing.T) http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.Header().Set("Content-Length", "23")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
_, _ = writer.Write([]byte("abc"))
|
||||
}
|
||||
},
|
||||
want: nil,
|
||||
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
|
||||
return assert.EqualError(t, err, "invalid character 'a' looking for beginning of value")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
args: args{
|
||||
project: "unboundsoftware/dummy",
|
||||
},
|
||||
handler: func(t *testing.T) http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
//writer.Header().Set("Content-Length", "23")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
_, _ = writer.Write([]byte(`[{"name":"1.0"},{"name": "1.1"}]`))
|
||||
}
|
||||
},
|
||||
want: []Tag{
|
||||
{Name: "1.0"},
|
||||
{Name: "1.1"},
|
||||
},
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(tt.handler(t))
|
||||
defer server.Close()
|
||||
r := &RestClient{
|
||||
client: http.DefaultClient,
|
||||
token: "some-gitlab-token",
|
||||
baseUrl: server.URL,
|
||||
}
|
||||
got, err := r.GetTags(tt.args.project)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("GetTags(%v)", tt.args.project)) {
|
||||
return
|
||||
}
|
||||
assert.Equalf(t, tt.want, got, "GetTags(%v)", tt.args.project)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user