From c2f55eea82333136917e5c8a601e5a66b09e0730 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 8 Dec 2023 11:20:53 +0300 Subject: [PATCH 1/3] [#5] Add "Revert" to the list of allowed commit prefixes Signed-off-by: Evgenii Stratonikov --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index b113770..029be3b 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( func main() { // Prepare regexp templates. - rxHeader := regexp.MustCompile(`^(\[\#[0-9Xx]+\]\s|Release)`) + rxHeader := regexp.MustCompile(`^(\[\#[0-9Xx]+\]\s|Release|Revert|Reapply)`) rxSignOff := regexp.MustCompile(`^Signed-off-by:`) // Open current git dir. -- 2.40.1 From b57feca5f3a9bda034b37a6f63fff4cd574e0c58 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jan 2024 15:25:43 +0300 Subject: [PATCH 2/3] [#3] Disallow using [xX] in commit prefix Signed-off-by: Evgenii Stratonikov --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 029be3b..7715e35 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( func main() { // Prepare regexp templates. - rxHeader := regexp.MustCompile(`^(\[\#[0-9Xx]+\]\s|Release|Revert|Reapply)`) + rxHeader := regexp.MustCompile(`^(\[\#[0-9]+\]\s|Release|Revert|Reapply)`) rxSignOff := regexp.MustCompile(`^Signed-off-by:`) // Open current git dir. -- 2.40.1 From b8aea16a203b360807411eaa937d770ea8a16057 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 25 Jan 2024 19:13:45 +0300 Subject: [PATCH 3/3] [#2] Write tests for the regex Signed-off-by: Evgenii Stratonikov --- go.mod | 4 ++++ main.go | 3 ++- main_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 main_test.go diff --git a/go.mod b/go.mod index fd30ccd..8dfcc57 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( github.com/go-git/go-git/v5 v5.7.0 github.com/sethvargo/go-githubactions v1.1.0 + github.com/stretchr/testify v1.4.0 ) require ( @@ -12,6 +13,7 @@ require ( github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect github.com/acomagu/bufpipe v1.0.4 // indirect github.com/cloudflare/circl v1.3.3 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.4.1 // indirect @@ -20,6 +22,7 @@ require ( github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/sethvargo/go-envconfig v0.8.0 // indirect github.com/skeema/knownhosts v1.1.1 // indirect @@ -28,4 +31,5 @@ require ( golang.org/x/net v0.10.0 // indirect golang.org/x/sys v0.8.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect + gopkg.in/yaml.v2 v2.2.4 // indirect ) diff --git a/main.go b/main.go index 7715e35..6b6a6e4 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,10 @@ import ( gha "github.com/sethvargo/go-githubactions" ) +var rxHeader = regexp.MustCompile(`^(\[\#[0-9]+\]\s|Release|Revert|Reapply)`) + func main() { // Prepare regexp templates. - rxHeader := regexp.MustCompile(`^(\[\#[0-9]+\]\s|Release|Revert|Reapply)`) rxSignOff := regexp.MustCompile(`^Signed-off-by:`) // Open current git dir. diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..7e41319 --- /dev/null +++ b/main_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCommitMessages(t *testing.T) { + allowed := []string{ + "[#1] omg: Make a commit", + "[#666] Omit component", + `Revert "[#114] local: Fix payload type after recent refactoring"`, + `Reapply "[#114] local: Fix payload type after recent refactoring"`, + `Release v0.4.2`, + } + + for i := range allowed { + assert.True(t, rxHeader.MatchString(allowed[i]), "message: '%s'", allowed[i]) + } + + restricted := []string{ + "[#xx] omg: Forget numbers", + "#12 Forget brackets", + "Refactor something", + } + for i := range restricted { + assert.False(t, rxHeader.MatchString(restricted[i]), "message: '%s'", restricted[i]) + } +} -- 2.40.1