From b547de2dc8514cb9eb8217510eb32d02800f0baf Mon Sep 17 00:00:00 2001 From: cbrgm Date: Thu, 18 Jan 2024 15:28:24 +0100 Subject: [PATCH] fix: ignoredPrefixes and allowedPrefixes --- cmd/cleanup-stale-branches-action/main.go | 24 +++++++++----- .../main_test.go | 31 +++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/cmd/cleanup-stale-branches-action/main.go b/cmd/cleanup-stale-branches-action/main.go index 40ce242..8e0bf01 100644 --- a/cmd/cleanup-stale-branches-action/main.go +++ b/cmd/cleanup-stale-branches-action/main.go @@ -121,9 +121,9 @@ func (g *GitHubClientWrapper) getDeletableBranches(ctx context.Context) ([]strin return nil, err } - ignoreBranches := strings.Split(args.IgnoreBranches, ",") - allowedPrefixes := strings.Split(args.AllowedPrefixes, ",") - ignoredPrefixes := strings.Split(args.IgnoredPrefixes, ",") + ignoreBranches := splitNonEmpty(args.IgnoreBranches) + allowedPrefixes := splitNonEmpty(args.AllowedPrefixes) + ignoredPrefixes := splitNonEmpty(args.IgnoredPrefixes) deletableBranches := []string{} @@ -159,13 +159,13 @@ func (g *GitHubClientWrapper) getDeletableBranches(ctx context.Context) ([]strin continue } - if !startsWith(allowedPrefixes, branchName) { + if len(allowedPrefixes) > 0 && !startsWith(allowedPrefixes, branchName) { log.Printf("- Skipping `%s`: does not match allowed prefixes\n", branchName) continue } - if startsWith(ignoredPrefixes, branchName) { - log.Printf("- Skipping `%s`: matches an ignored prefix\n", branchName) + if len(ignoredPrefixes) > 0 && startsWith(ignoredPrefixes, branchName) { + log.Printf("- Skipping `%s`: does match ignored prefixes\n", branchName) continue } @@ -307,10 +307,20 @@ func contains(slice []string, item string) bool { } func startsWith(prefixes []string, str string) bool { + if len(prefixes) == 0 { + return false + } for _, prefix := range prefixes { if strings.HasPrefix(str, prefix) { return true } } - return len(prefixes) == 0 + return false +} + +func splitNonEmpty(input string) []string { + if input == "" { + return []string{} + } + return strings.Split(input, ",") } diff --git a/cmd/cleanup-stale-branches-action/main_test.go b/cmd/cleanup-stale-branches-action/main_test.go index d564ee9..d50264a 100644 --- a/cmd/cleanup-stale-branches-action/main_test.go +++ b/cmd/cleanup-stale-branches-action/main_test.go @@ -1,6 +1,9 @@ package main -import "testing" +import ( + "reflect" + "testing" +) func TestParseRepoName(t *testing.T) { tests := []struct { @@ -131,7 +134,7 @@ func TestStartsWith(t *testing.T) { name: "Empty prefix list", prefixes: []string{}, str: "testString", - want: true, + want: false, }, { name: "Empty string", @@ -173,3 +176,27 @@ func TestStartsWith(t *testing.T) { }) } } + +func TestSplitNonEmpty(t *testing.T) { + tests := []struct { + name string + input string + want []string + }{ + {"EmptyString", "", []string{}}, + {"SingleElement", "a", []string{"a"}}, + {"TwoElements", "a,b", []string{"a", "b"}}, + {"ElementsWithSpaces", "a, b, c", []string{"a", " b", " c"}}, + {"ElementsWithEmptyString", "a,,c", []string{"a", "", "c"}}, + {"OnlyCommas", ",,,", []string{"", "", "", ""}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := splitNonEmpty(tt.input) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("splitNonEmpty(%q) = %v, want %v", tt.input, got, tt.want) + } + }) + } +}