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-wildcard-pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@naverpay/commithelper-go': minor
---

Support glob-style wildcard patterns in protect option
17 changes: 17 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ jobs:
version: 8
run_install: true
- run: pnpm run test

GoTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
go:
- 'packages/commithelper-go/**'
- if: steps.filter.outputs.go == 'true'
uses: actions/setup-go@v4
with:
go-version: '1.24'
- if: steps.filter.outputs.go == 'true'
run: cd packages/commithelper-go && go test -v ./...
14 changes: 12 additions & 2 deletions packages/commithelper-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ This is Basic rule of `.commithelperrc.json`.
#### protect
- Defines branch prefixes that are blocked from committing. `main`, `master`, `develop` branch is blocked by default.
- Defines branches that are blocked from committing. Supports glob-style wildcard patterns.
- `*` 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.
```json
{
"protect": ["main", "master", "develop", "release/*", "epic/*"]
}
```
#### template
Expand Down Expand Up @@ -180,7 +190,7 @@ Result:
>
> - commit on `feature/1` branch will be tagged as `[#1]`.
> - commit on `qa/1` branch will be tagged as `[your-org/your-repo#1]`.
> - direct commit attempt toward `main`, `master`, `develop`, `epic/***` branch will be blocked
> - direct commit attempt toward `main`, `master`, `develop`, `epic/*` branch will be blocked
## Environment Variables
Expand Down
5 changes: 3 additions & 2 deletions packages/commithelper-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"strings"
"text/template"
Expand Down Expand Up @@ -215,8 +216,8 @@ func applyTemplate(config Config, data *TemplateData) string {
}

func isProtectedBranch(branchName string, protectedBranches []string) bool {
for _, protected := range protectedBranches {
if branchName == protected {
for _, pattern := range protectedBranches {
if matched, _ := path.Match(pattern, branchName); matched {
return true
}
}
Expand Down
26 changes: 26 additions & 0 deletions packages/commithelper-go/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "testing"

func TestIsProtectedBranch(t *testing.T) {
protected := []string{"main", "release/*", "epic/*"}

tests := []struct {
branch string
want bool
}{
{"main", true},
{"develop", false},
{"release/1.0", true},
{"release/v2", true},
{"feature/123", false},
{"epic/815_fix", true},
}

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)
}
}
}