diff --git a/.changeset/protect-pattern-error-handling.md b/.changeset/protect-pattern-error-handling.md new file mode 100644 index 0000000..acb556c --- /dev/null +++ b/.changeset/protect-pattern-error-handling.md @@ -0,0 +1,5 @@ +--- +'@naverpay/commithelper-go': patch +--- + +Return error for invalid protect patterns instead of silently ignoring them diff --git a/packages/commithelper-go/README.md b/packages/commithelper-go/README.md index ff5a5f2..76f636d 100644 --- a/packages/commithelper-go/README.md +++ b/packages/commithelper-go/README.md @@ -98,7 +98,7 @@ This is Basic rule of `.commithelperrc.json`. - `*` matches any sequence of characters except `/` - `?` matches any single character except `/` - `[...]` matches any character in the set -- `main`, `master`, `develop` branch is blocked by default. + - Note: `*` does not match across `/`, so `release/*` matches `release/1.0` but not `release/1.0/hotfix`. Use `release/*/*` for nested branches. ```json { diff --git a/packages/commithelper-go/main.go b/packages/commithelper-go/main.go index f78c9d4..7830bc2 100644 --- a/packages/commithelper-go/main.go +++ b/packages/commithelper-go/main.go @@ -69,7 +69,12 @@ func main() { config := loadConfig() // Check if current branch is protected - if isProtectedBranch(branchName, config.Protect) { + protected, err := isProtectedBranch(branchName, config.Protect) + if err != nil { + fmt.Printf("Error: %v\n", err) + os.Exit(1) + } + if protected { fmt.Printf("Error: Cannot commit to protected branch '%s'\n", branchName) os.Exit(1) } @@ -215,13 +220,17 @@ func applyTemplate(config Config, data *TemplateData) string { return buf.String() } -func isProtectedBranch(branchName string, protectedBranches []string) bool { +func isProtectedBranch(branchName string, protectedBranches []string) (bool, error) { for _, pattern := range protectedBranches { - if matched, _ := path.Match(pattern, branchName); matched { - return true + matched, err := path.Match(pattern, branchName) + if err != nil { + return false, fmt.Errorf("invalid protect pattern %q: %w", pattern, err) + } + if matched { + return true, nil } } - return false + return false, nil } func isAlreadyTagged(commitMessage string) bool { diff --git a/packages/commithelper-go/main_test.go b/packages/commithelper-go/main_test.go index 5655737..aef4ebf 100644 --- a/packages/commithelper-go/main_test.go +++ b/packages/commithelper-go/main_test.go @@ -18,9 +18,21 @@ func TestIsProtectedBranch(t *testing.T) { } for _, tt := range tests { - got := isProtectedBranch(tt.branch, protected) - if got != tt.want { - t.Errorf("isProtectedBranch(%q) = %v, want %v", tt.branch, got, tt.want) - } + t.Run(tt.branch, func(t *testing.T) { + got, err := isProtectedBranch(tt.branch, protected) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != tt.want { + t.Errorf("got %v, want %v", got, tt.want) + } + }) + } +} + +func TestIsProtectedBranch_InvalidPattern(t *testing.T) { + _, err := isProtectedBranch("main", []string{"["}) + if err == nil { + t.Error("expected error for invalid pattern, got nil") } }