Skip to content

Commit

Permalink
Merge pull request #42 from beliven-it/fix/cleanup-policy
Browse files Browse the repository at this point in the history
Fix/cleanup policy
  • Loading branch information
beliven-daniele-sarnari authored May 6, 2024
2 parents b87e593 + 840cb95 commit e7cfbde
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 17 deletions.
35 changes: 35 additions & 0 deletions cmd/gitlabUpdateCleanUpPolicyCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var gitlabUpdateCleanUpPolicyCmd = &cobra.Command{
Use: "cleanup-policy {project_id}",
Args: cobra.ExactArgs(1),
Short: "Update Cleanup Policy for Gitlab project",
Long: `
Update Cleanup Policy for a specific Gitlab project.`,
Example: `
Update Cleanup Policy for the project 1234.
opsi gitlab update cleanup-policy 1234
`,
Run: func(cmd *cobra.Command, args []string) {
// Take project ID
projectID := args[0]

// Update cleanup policy
err := gitlab.UpdateCleanUpPolicy(projectID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
gitlabUpdateCmd.AddCommand(gitlabUpdateCleanUpPolicyCmd)
}
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
24 changes: 15 additions & 9 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 @@ -24,6 +25,7 @@ type Gitlab interface {
BulkSettings(*chan string) error
Deprovionioning(string) error
UpdateMirroring() error
UpdateCleanUpPolicy(string) error
}

type GitlabMirrorOptions struct {
Expand All @@ -35,16 +37,20 @@ 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"`
OnlyProtectedBranched bool `json:"only_protected_branches"`
}

type gitlabMirrorResponse struct {
ID int `json:"id"`
Enabled bool `json:"enabled"`
Url string `json:"url"`
ID int `json:"id"`
Enabled bool `json:"enabled"`
Url string `json:"url"`
}

type gitlabDefaultUser struct {
Expand Down Expand Up @@ -244,12 +250,12 @@ var defaultProjectStagingSettings = gitlabSetupBranchRequest{

var defaultCleanUpPolicy = map[string]interface{}{
"container_expiration_policy_attributes": map[string]interface{}{
"cadence": "1month",
"cadence": "7d",
"enabled": true,
"keep_n": 1,
"older_than": "14d",
"older_than": "7d",
"name_regex": ".*",
"name_regex_keep": ".*-main",
"name_regex_keep": ".*",
},
}

Expand Down
38 changes: 33 additions & 5 deletions scopes/gitlab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,36 @@ func (g *gitlab) setDefaultBranch(projectID int, branch string) error {
return err
}

func (g *gitlab) UpdateCleanUpPolicy(projectID string) error {
id, err := strconv.Atoi(projectID)

if err != nil {
return err
}

err = g.applyCleanUpPolicy(id)
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 @@ -892,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 e7cfbde

Please sign in to comment.