feat: add all existing tags to exclusion filter

This commit is contained in:
2022-09-27 10:00:16 +02:00
parent f2c1ccf97d
commit 1ab4f8361b
2 changed files with 64 additions and 1 deletions
+9 -1
View File
@@ -42,7 +42,15 @@ func handle(cli CLI, logger log.Interface, kubeClient *kube.Client, gitlabClient
return err
}
for image, versions := range images {
if err := gitlabClient.UpdateCleanupPolicy(image, versions); err != nil {
tags, err := gitlabClient.GetTags(image)
if err != nil {
return err
}
tagVersions := make([]string, len(tags))
for i, tag := range tags {
tagVersions[i] = tag.Name
}
if err := gitlabClient.UpdateCleanupPolicy(image, append(versions, tagVersions...)); err != nil {
return err
}
}
+55
View File
@@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
)
func New(token string) *RestClient {
@@ -54,6 +55,31 @@ func (r *RestClient) UpdateCleanupPolicy(project string, versions []string) erro
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",
URL: reqUrl,
Header: header,
}
resp, err := r.client.Do(req)
if err != nil {
return nil, err
}
var tags []Tag
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&tags)
return tags, err
}
type ProjectConfig struct {
ContainerExpirationPolicyAttributes ContainerExpirationPolicyAttributes `json:"container_expiration_policy_attributes"`
}
@@ -66,3 +92,32 @@ type ContainerExpirationPolicyAttributes struct {
NameRegex string `json:"name_regex"`
NameRegexKeep string `json:"name_regex_keep"`
}
type Tag struct {
Commit Commit `json:"commit"`
Release Release `json:"release"`
Name string `json:"name"`
Target string `json:"target"`
Message interface{} `json:"message"`
Protected bool `json:"protected"`
}
type Commit struct {
ID string `json:"id"`
ShortID string `json:"short_id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
ParentIds []string `json:"parent_ids"`
Message string `json:"message"`
AuthorName string `json:"author_name"`
AuthorEmail string `json:"author_email"`
AuthoredDate string `json:"authored_date"`
CommitterName string `json:"committer_name"`
CommitterEmail string `json:"committer_email"`
CommittedDate string `json:"committed_date"`
}
type Release struct {
TagName string `json:"tag_name"`
Description string `json:"description"`
}