From 72708f25b12786e10a7f49da584a638ac76bfe92 Mon Sep 17 00:00:00 2001 From: Andrew Rynhard Date: Tue, 21 Apr 2020 08:01:05 -0700 Subject: [PATCH] feat: support regex in conventional commit scope This allows for regular expressions to be used in the scope of a conventional commit. Signed-off-by: Andrew Rynhard --- .../commit/check_conventional_commit.go | 3 +- internal/policy/commit/commit_test.go | 71 ++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/internal/policy/commit/check_conventional_commit.go b/internal/policy/commit/check_conventional_commit.go index ad209a10..2ecc2c1c 100644 --- a/internal/policy/commit/check_conventional_commit.go +++ b/internal/policy/commit/check_conventional_commit.go @@ -84,7 +84,8 @@ func (c Commit) ValidateConventionalCommit() policy.Check { if groups[3] != "" { scopeIsValid := false for _, scope := range c.Conventional.Scopes { - if scope == groups[3] { + re := regexp.MustCompile(scope) + if re.Match([]byte(groups[3])) { scopeIsValid = true break } diff --git a/internal/policy/commit/commit_test.go b/internal/policy/commit/commit_test.go index 27b7af76..e394f972 100644 --- a/internal/policy/commit/commit_test.go +++ b/internal/policy/commit/commit_test.go @@ -128,6 +128,7 @@ func TestValidateDCO(t *testing.T) { } } +// nolint: dupl func TestValidConventionalCommitPolicy(t *testing.T) { dir, err := ioutil.TempDir("", "test") if err != nil { @@ -211,11 +212,67 @@ func TestEmptyConventionalCommitPolicy(t *testing.T) { } } +// nolint: dupl +func TestValidConventionalCommitPolicyRegex(t *testing.T) { + dir, err := ioutil.TempDir("", "test") + if err != nil { + log.Fatal(err) + } + defer RemoveAll(dir) + err = os.Chdir(dir) + if err != nil { + t.Error(err) + } + err = initRepo() + if err != nil { + t.Error(err) + } + err = createValidCommitRegex() + if err != nil { + t.Error(err) + } + report, err := runCompliance() + if err != nil { + t.Error(err) + } + if !report.Valid() { + t.Error("Report is invalid with valid conventional commit") + } +} + +// nolint: dupl +func TestInvalidConventionalCommitPolicyRegex(t *testing.T) { + dir, err := ioutil.TempDir("", "test") + if err != nil { + log.Fatal(err) + } + defer RemoveAll(dir) + err = os.Chdir(dir) + if err != nil { + t.Error(err) + } + err = initRepo() + if err != nil { + t.Error(err) + } + err = createInvalidCommitRegex() + if err != nil { + t.Error(err) + } + report, err := runCompliance() + if err != nil { + t.Error(err) + } + if report.Valid() { + t.Error("Report is valid with invalid conventional commit") + } +} + func runCompliance() (*policy.Report, error) { c := &Commit{ Conventional: &Conventional{ Types: []string{"type"}, - Scopes: []string{"scope"}, + Scopes: []string{"scope", "^valid"}, }, } @@ -253,3 +310,15 @@ func createEmptyCommit() error { return err } + +func createValidCommitRegex() error { + _, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='test@talos-systems.io'", "commit", "-m", "type(valid-1): description").Output() + + return err +} + +func createInvalidCommitRegex() error { + _, err := exec.Command("git", "-c", "user.name='test'", "-c", "user.email='test@talos-systems.io'", "commit", "-m", "type(invalid-1): description").Output() + + return err +}