Merge branch 'fetch-tags' into 'main'
feat: add all existing tags to exclusion filter See merge request unboundsoftware/gitlab-cleanup-handler!5
This commit was merged in pull request #9.
This commit is contained in:
@@ -42,7 +42,15 @@ func handle(cli CLI, logger log.Interface, kubeClient *kube.Client, gitlabClient
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for image, versions := range images {
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(token string) *RestClient {
|
func New(token string) *RestClient {
|
||||||
@@ -54,6 +55,31 @@ func (r *RestClient) UpdateCleanupPolicy(project string, versions []string) erro
|
|||||||
return err
|
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 {
|
type ProjectConfig struct {
|
||||||
ContainerExpirationPolicyAttributes ContainerExpirationPolicyAttributes `json:"container_expiration_policy_attributes"`
|
ContainerExpirationPolicyAttributes ContainerExpirationPolicyAttributes `json:"container_expiration_policy_attributes"`
|
||||||
}
|
}
|
||||||
@@ -66,3 +92,32 @@ type ContainerExpirationPolicyAttributes struct {
|
|||||||
NameRegex string `json:"name_regex"`
|
NameRegex string `json:"name_regex"`
|
||||||
NameRegexKeep string `json:"name_regex_keep"`
|
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"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user