Skip to content

Commit

Permalink
Merge pull request #43 from beliven-it/feat/bulk-update-cleanup-policies
Browse files Browse the repository at this point in the history
feat: bulk cleanup policy command
  • Loading branch information
beliven-daniele-sarnari authored May 6, 2024
2 parents e7cfbde + e239cc9 commit 9f96bc5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cmd/gitlabUpdateCleanUpPolicyCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var gitlabUpdateCleanUpPolicyCmd = &cobra.Command{
Use: "cleanup-policy {project_id}",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Short: "Update Cleanup Policy for Gitlab project",
Long: `
Update Cleanup Policy for a specific Gitlab project.`,
Expand All @@ -18,8 +18,11 @@ var gitlabUpdateCleanUpPolicyCmd = &cobra.Command{
opsi gitlab update cleanup-policy 1234
`,
Run: func(cmd *cobra.Command, args []string) {
// Take project ID
projectID := args[0]
projectID := ""
if len(args) > 0 {
// Take project ID
projectID = args[0]
}

// Update cleanup policy
err := gitlab.UpdateCleanUpPolicy(projectID)
Expand Down
28 changes: 24 additions & 4 deletions scopes/gitlab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,32 @@ func (g *gitlab) setDefaultBranch(projectID int, branch string) error {
}

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

if err != nil {
return err
if err != nil {
return err
}

err = g.applyCleanUpPolicy(id)
if err != nil {
return err
}
} else {
projectsList, err := g.listProjects()

if err != nil {
return err
}
for _, project := range projectsList {
err := g.applyCleanUpPolicy(project.ID)
if err != nil {
return err
}
}
}

err = g.applyCleanUpPolicy(id)
return err
}

Expand All @@ -357,6 +376,7 @@ func (g *gitlab) applyCleanUpPolicy(projectID int) error {
isExcluded, err := g.contains(g.exclusions.CleanupPolicies, projectID)
if !isExcluded {
_, err = g.request("PUT", fmt.Sprintf("/projects/%d", projectID), defaultCleanUpPolicy, nil)
fmt.Println("Cleanup policy updated for the project with ID", projectID)
} else {
fmt.Println("Cleanup policy not updated for the project with ID", projectID)
}
Expand Down

0 comments on commit 9f96bc5

Please sign in to comment.