From afd57e77a8b9c5940872b2f31bee64b0777acbbc Mon Sep 17 00:00:00 2001 From: djk01281 Date: Thu, 19 Feb 2026 20:19:47 +0900 Subject: [PATCH 1/2] refactor(commithelper-go): return error from isProtectedBranch and improve tests --- packages/commithelper-go/README.md | 2 +- packages/commithelper-go/main.go | 19 ++++++++++++++----- packages/commithelper-go/main_test.go | 20 ++++++++++++++++---- 3 files changed, 31 insertions(+), 10 deletions(-) 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") } } From 5e933347fa3fe24968bbafba1f7b30cd784c0df6 Mon Sep 17 00:00:00 2001 From: djk01281 Date: Thu, 19 Feb 2026 20:19:51 +0900 Subject: [PATCH 2/2] chore: add changeset --- .changeset/protect-pattern-error-handling.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/protect-pattern-error-handling.md 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