Skip to content
Open
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/fix-hyphen-branch-prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@naverpay/commithelper-go": patch
---

fix: branch prefix with hyphens not being matched
4 changes: 2 additions & 2 deletions packages/commithelper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func loadConfig() Config {
}

func generatePrefix(branchName string, config Config) string {
pattern := regexp.MustCompile(`^(\w+)/(\d+).*`)
pattern := regexp.MustCompile(`^([\w-]+)/(\d+).*`)
matches := pattern.FindStringSubmatch(branchName)
if len(matches) < 3 {
return ""
Expand All @@ -164,7 +164,7 @@ func generatePrefix(branchName string, config Config) string {
}

func generateTemplateData(branchName string, config Config, message string) *TemplateData {
pattern := regexp.MustCompile(`^(\w+)/(\d+).*`)
pattern := regexp.MustCompile(`^([\w-]+)/(\d+).*`)
matches := pattern.FindStringSubmatch(branchName)
if len(matches) < 3 {
return nil
Expand Down
70 changes: 70 additions & 0 deletions packages/commithelper-go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,73 @@ func TestIsProtectedBranch_InvalidPattern(t *testing.T) {
t.Error("expected error for invalid pattern, got nil")
}
}

func strPtr(s string) *string { return &s }

func TestGenerateTemplateData_HyphenatedPrefix(t *testing.T) {
config := Config{
Rules: map[string]*string{
"fe-plan": strPtr("card-fe/plan"),
"feature": nil,
},
}

tests := []struct {
name string
branch string
wantNil bool
wantPrefix string
}{
{"hyphenated prefix", "fe-plan/11", false, "card-fe/plan#11"},
{"simple prefix", "feature/42", false, "#42"},
{"no match", "main", true, ""},
{"unknown prefix", "unknown/99", true, ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data := generateTemplateData(tt.branch, config, "Test")
if tt.wantNil {
if data != nil {
t.Errorf("expected nil, got %+v", data)
}
return
}
if data == nil {
t.Fatal("expected non-nil TemplateData, got nil")
}
if data.Prefix != tt.wantPrefix {
t.Errorf("Prefix = %q, want %q", data.Prefix, tt.wantPrefix)
}
})
}
}

func TestGeneratePrefix_HyphenatedPrefix(t *testing.T) {
config := Config{
Rules: map[string]*string{
"fe-plan": strPtr("card-fe/plan"),
"feature": nil,
},
}

tests := []struct {
name string
branch string
want string
}{
{"hyphenated prefix", "fe-plan/11", "card-fe/plan#11"},
{"simple prefix", "feature/42", "#42"},
{"no match", "main", ""},
{"unknown prefix", "unknown/99", ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := generatePrefix(tt.branch, config)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
Loading