Compare commits

...

4 commits

Author SHA1 Message Date
a9d9c0d077 [#7] Check empty line after commit's header
Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
2024-06-26 22:27:10 +03:00
b8aea16a20 [#2] Write tests for the regex
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-01-25 19:17:12 +03:00
b57feca5f3 [#3] Disallow using [xX] in commit prefix
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-01-25 19:17:07 +03:00
c2f55eea82 [#5] Add "Revert" to the list of allowed commit prefixes
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2024-01-25 19:17:01 +03:00
3 changed files with 43 additions and 1 deletions

4
go.mod
View file

@ -5,6 +5,7 @@ go 1.20
require ( require (
github.com/go-git/go-git/v5 v5.7.0 github.com/go-git/go-git/v5 v5.7.0
github.com/sethvargo/go-githubactions v1.1.0 github.com/sethvargo/go-githubactions v1.1.0
github.com/stretchr/testify v1.4.0
) )
require ( require (
@ -12,6 +13,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // 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/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // 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/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.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/sergi/go-diff v1.1.0 // indirect
github.com/sethvargo/go-envconfig v0.8.0 // indirect github.com/sethvargo/go-envconfig v0.8.0 // indirect
github.com/skeema/knownhosts v1.1.1 // 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/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect golang.org/x/sys v0.8.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
) )

10
main.go
View file

@ -14,9 +14,10 @@ import (
gha "github.com/sethvargo/go-githubactions" gha "github.com/sethvargo/go-githubactions"
) )
var rxHeader = regexp.MustCompile(`^(\[\#[0-9]+\]\s|Release|Revert|Reapply)`)
func main() { func main() {
// Prepare regexp templates. // Prepare regexp templates.
rxHeader := regexp.MustCompile(`^(\[\#[0-9Xx]+\]\s|Release)`)
rxSignOff := regexp.MustCompile(`^Signed-off-by:`) rxSignOff := regexp.MustCompile(`^Signed-off-by:`)
// Open current git dir. // Open current git dir.
@ -76,6 +77,13 @@ func main() {
return nil return nil
} }
// Check empty line after header.
if 1 < len(lines) && lines[1] != "" {
fail = true
fmt.Printf("Error: second line must be empty '%s' [%s]\n", lines[1], id)
return nil
}
// Check commit sign-off. // Check commit sign-off.
if !rxSignOff.MatchString(lines[len(lines)-1]) { if !rxSignOff.MatchString(lines[len(lines)-1]) {
fail = true fail = true

30
main_test.go Normal file
View file

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