Skip to content

Commit

Permalink
feat: add exclusions for cleanup policy
Browse files Browse the repository at this point in the history
  • Loading branch information
beliven-alex-zongaro committed May 3, 2024
1 parent 5b3d2a9 commit 840cb95
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
2 changes: 0 additions & 2 deletions cmd/gitlabUpdateCleanUpPolicyCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ var gitlabUpdateCleanUpPolicyCmd = &cobra.Command{
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Updated cleanup policy of the project with ID", projectID)
},
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func initConfig() {
mainConfig.Gitlab.ApiURL,
mainConfig.Gitlab.Token,
mainConfig.Gitlab.Mirror,
mainConfig.Gitlab.Exclusions,
)

onepassword = op.NewOnePassword(mainConfig.OnePassword.Address)
Expand Down
7 changes: 4 additions & 3 deletions config/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ type ConfigPostamark struct {
}

type ConfigGitlab struct {
Token string `mapstructure:"token"`
ApiURL string `mapstructure:"api_url"`
Mirror gitlab.GitlabMirrorOptions `mapstructure:"mirror"`
Token string `mapstructure:"token"`
ApiURL string `mapstructure:"api_url"`
Exclusions gitlab.GitlabExclusionsConfig `mapstructure:"exclusions"`
Mirror gitlab.GitlabMirrorOptions `mapstructure:"mirror"`
}

type ConfigOnePassword struct {
Expand Down
2 changes: 2 additions & 0 deletions config/template.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
gitlab:
api_url: "https://company.gitlab.com/api/v4"
token: "<GITLAB_TOKEN>"
exclusions:
cleanup_policies: [<PROJECT_IDS_LIST>]
mirror:
url: "https://gitlab.com/api/v4"
group_id: "<GITLAB_MIRROR_GROUP_ID>"
Expand Down
11 changes: 8 additions & 3 deletions scopes/gitlab/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const gitlabDefaultGroupMemberOwner string = "default_group_member_owner"
const gitlabDefaultGroupMember string = "default_group_member"

type gitlab struct {
token string
apiURL string
mirror GitlabMirrorOptions
token string
apiURL string
mirror GitlabMirrorOptions
exclusions GitlabExclusionsConfig
}

type Gitlab interface {
Expand All @@ -36,6 +37,10 @@ type GitlabMirrorOptions struct {
GroupID int `mapstructure:"group_id"`
}

type GitlabExclusionsConfig struct {
CleanupPolicies []int `mapstructure:"cleanup_policies"`
}

type gitlabCreateMirrorRequest struct {
Enabled bool `json:"enabled"`
URL string `json:"url"`
Expand Down
27 changes: 22 additions & 5 deletions scopes/gitlab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,25 @@ func (g *gitlab) UpdateCleanUpPolicy(projectID string) error {
return err
}

// Function to check if an array contains an element
func (g *gitlab) contains(array []int, item int) (bool, error) {
for i := 0; i < len(array); i++ {
// check
if array[i] == item {
return true, nil
}
}
return false, nil
}

// Apply a cleanUP policy on gitlab project.
func (g *gitlab) applyCleanUpPolicy(projectID int) error {
_, err := g.request("PUT", fmt.Sprintf("/projects/%d", projectID), defaultCleanUpPolicy, nil)
isExcluded, err := g.contains(g.exclusions.CleanupPolicies, projectID)
if !isExcluded {
_, err = g.request("PUT", fmt.Sprintf("/projects/%d", projectID), defaultCleanUpPolicy, nil)
} else {
fmt.Println("Cleanup policy not updated for the project with ID", projectID)
}

return err
}
Expand Down Expand Up @@ -903,10 +919,11 @@ func (g *gitlab) Deprovionioning(username string) error {
return nil
}

func NewGitlab(apiURL string, token string, mirror GitlabMirrorOptions) Gitlab {
func NewGitlab(apiURL string, token string, mirror GitlabMirrorOptions, exclusions GitlabExclusionsConfig) Gitlab {
return &gitlab{
apiURL: apiURL,
token: token,
mirror: mirror,
apiURL: apiURL,
token: token,
mirror: mirror,
exclusions: exclusions,
}
}

0 comments on commit 840cb95

Please sign in to comment.