diff --git a/.changeset/protect-wildcard-pattern.md b/.changeset/protect-wildcard-pattern.md new file mode 100644 index 0000000..73b065b --- /dev/null +++ b/.changeset/protect-wildcard-pattern.md @@ -0,0 +1,5 @@ +--- +'@naverpay/commithelper-go': minor +--- + +Support glob-style wildcard patterns in protect option diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 70b8f8d..9cc3b0c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 ./... diff --git a/packages/commithelper-go/README.md b/packages/commithelper-go/README.md index aba51e5..ff5a5f2 100644 --- a/packages/commithelper-go/README.md +++ b/packages/commithelper-go/README.md @@ -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 @@ -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 diff --git a/packages/commithelper-go/main.go b/packages/commithelper-go/main.go index 981debb..f78c9d4 100644 --- a/packages/commithelper-go/main.go +++ b/packages/commithelper-go/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "os/exec" + "path" "regexp" "strings" "text/template" @@ -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 } } diff --git a/packages/commithelper-go/main_test.go b/packages/commithelper-go/main_test.go new file mode 100644 index 0000000..5655737 --- /dev/null +++ b/packages/commithelper-go/main_test.go @@ -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) + } + } +}