Skip to content

Commit

Permalink
Merge pull request #7 from cbrgm/fix-prefix
Browse files Browse the repository at this point in the history
fix: ignoredPrefixes and allowedPrefixes
  • Loading branch information
cbrgm authored Jan 18, 2024
2 parents fafe963 + b547de2 commit bfd89d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
24 changes: 17 additions & 7 deletions cmd/cleanup-stale-branches-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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, ",")
}
31 changes: 29 additions & 2 deletions cmd/cleanup-stale-branches-action/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"reflect"
"testing"
)

func TestParseRepoName(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -131,7 +134,7 @@ func TestStartsWith(t *testing.T) {
name: "Empty prefix list",
prefixes: []string{},
str: "testString",
want: true,
want: false,
},
{
name: "Empty string",
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit bfd89d9

Please sign in to comment.