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 b113770..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-9Xx]+\]\s|Release)`) 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]) + } +}