Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignoredPrefixes and allowedPrefixes #7

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}
Loading