Skip to content

Commit

Permalink
feat: support regex in conventional commit scope
Browse files Browse the repository at this point in the history
This allows for regular expressions to be used in the scope of a
conventional commit.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
  • Loading branch information
andrewrynhard committed Apr 21, 2020
1 parent ec5e365 commit 72708f2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
3 changes: 2 additions & 1 deletion internal/policy/commit/check_conventional_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
71 changes: 70 additions & 1 deletion internal/policy/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func TestValidateDCO(t *testing.T) {
}
}

// nolint: dupl
func TestValidConventionalCommitPolicy(t *testing.T) {
dir, err := ioutil.TempDir("", "test")
if err != nil {
Expand Down Expand Up @@ -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"},
},
}

Expand Down Expand Up @@ -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
}

0 comments on commit 72708f2

Please sign in to comment.