-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release(1.0.3): added support to tidy-up stale tags; added support to…
… delete generic refs that target either branch or tag names
- Loading branch information
Showing
9 changed files
with
224 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pcanilho/gh-tidy/api" | ||
"github.com/pcanilho/gh-tidy/helpers" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var deleteRefCmd = &cobra.Command{ | ||
Use: "delete", | ||
Example: `$ gh tidy delete --ref <ref>`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("only on <owner>/<repository> can to be provided") | ||
} | ||
|
||
if refs == nil || len(refs) == 0 { | ||
return fmt.Errorf("at least one ref must be provided") | ||
} | ||
|
||
repo := args[0] | ||
if len(owner) == 0 && strings.Contains(args[0], "/") { | ||
composite := strings.Split(args[0], "/") | ||
owner, repo = composite[0], composite[1] | ||
} | ||
|
||
// Owner | ||
if len(owner) == 0 { | ||
return fmt.Errorf("the [owner] flag must be provided") | ||
} | ||
brs, err := ghApi.ListRefs(cmd.Context(), owner, repo, api.BranchRefType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tgs, err := ghApi.ListRefs(cmd.Context(), owner, repo, api.TagRefType) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rfs := append(brs, tgs...) | ||
var toDeleteIds []string | ||
for _, rf := range rfs { | ||
for _, ref := range refs { | ||
if rf.Name == ref { | ||
toDeleteIds = append(toDeleteIds, rf.Id) | ||
} | ||
} | ||
} | ||
|
||
if len(toDeleteIds) == 0 { | ||
return nil | ||
} | ||
|
||
if !force { | ||
if !helpers.Prompt(fmt.Sprintf("Delete [%d] refs?", len(toDeleteIds))) { | ||
os.Exit(0) | ||
} | ||
} | ||
_, err = ghApi.DeleteRefs(cmd.Context(), toDeleteIds...) | ||
if err != nil { | ||
return fmt.Errorf("unable to delete [refs=%v]. error: %v%v", refs, err) | ||
} | ||
|
||
out = fmt.Sprintf("Deleted [refs=%v] with [ids=%v]\n", refs, toDeleteIds) | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
deleteRefCmd.PersistentFlags().StringArrayVar(&refs, "ref", nil, "The provided ref(s) to be deleted") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pcanilho/gh-tidy/api" | ||
"github.com/pcanilho/gh-tidy/helpers" | ||
"github.com/pcanilho/gh-tidy/models" | ||
"github.com/spf13/cobra" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var staleTagsCmd = &cobra.Command{ | ||
Use: "tags", | ||
Aliases: []string{"t"}, | ||
Example: `$ gh tidy stale tags <owner/repo> -t 72h`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return fmt.Errorf("at least one <owner>/<repository> needs to be provided") | ||
} | ||
view := make(map[string][]*models.GitHubRef) | ||
for _, repo := range args { | ||
if len(owner) == 0 && strings.Contains(repo, "/") { | ||
composite := strings.Split(repo, "/") | ||
owner, repo = composite[0], composite[1] | ||
} | ||
|
||
// Owner | ||
if len(owner) == 0 { | ||
return fmt.Errorf("the [owner] flag must be provided") | ||
} | ||
tags, err := ghApi.ListRefs(cmd.Context(), owner, repo, api.TagRefType) | ||
if err != nil { | ||
return err | ||
} | ||
view[repo] = tags | ||
} | ||
|
||
for repo, tags := range view { | ||
var filteredTags []*models.GitHubRef | ||
for _, tag := range tags { | ||
if excludeRegex != nil && excludeRegex.MatchString(tag.Name) { | ||
continue | ||
} | ||
|
||
timeToCompare := tag.TagDate | ||
if timeToCompare == nil { | ||
timeToCompare = tag.LastCommitDate | ||
} | ||
if timeToCompare.Before(time.Now().Add(-staleThreshold)) { | ||
filteredTags = append(filteredTags, tag) | ||
} | ||
} | ||
view[repo] = filteredTags | ||
} | ||
out = view | ||
return nil | ||
}, | ||
PostRunE: func(cmd *cobra.Command, args []string) error { | ||
if out == nil { | ||
return fmt.Errorf("no results found") | ||
} | ||
view := out.(map[string][]*models.GitHubRef) | ||
if remove { | ||
for repo, tags := range view { | ||
if !force { | ||
if !helpers.Prompt(fmt.Sprintf("Delete [%d] tags in repo [%v]?", len(tags), repo)) { | ||
continue | ||
} | ||
} | ||
|
||
for _, tag := range tags { | ||
_, err := ghApi.DeleteRefs(cmd.Context(), tag.Id) | ||
if err != nil { | ||
return fmt.Errorf("unable to delete tags: %v. error: %v", tag.Name, err) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
staleTagsCmd.PersistentFlags().StringVar(&excludePattern, "exclude", "", "If provided, it will be used to exclude tags that match the pattern (regexp)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mr/2023.5.10-on-demand-mr | ||
mr/2023.5.10-on-demand-mr-cex | ||
mr/2023.5.11-on-demand-mr | ||
mr/2023.5.11-on-demand-mr-cex | ||
release/NL-2023.7.0 | ||
release/2023.6.0-fix-conflicts | ||
release/2023.6.0-fix-conflicts-update-MR | ||
release/2023.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters