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 {
|
||||
|
||||
Reference in New Issue
Block a user