Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/protect-pattern-error-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@naverpay/commithelper-go': patch
---

Return error for invalid protect patterns instead of silently ignoring them
2 changes: 1 addition & 1 deletion packages/commithelper-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
19 changes: 14 additions & 5 deletions packages/commithelper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions packages/commithelper-go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading