From efd7fbb0dbc0042f54af587ffa07a829fdc88f1c Mon Sep 17 00:00:00 2001 From: Andrew Rynhard Date: Mon, 16 Nov 2020 12:24:45 -0800 Subject: [PATCH] feat: improve Jira check Fixes a few minor issues: - make square brackets optional - allow for conventional commit scopes to be present Signed-off-by: Andrew Rynhard --- internal/policy/commit/check_jira.go | 9 ++++---- internal/policy/commit/check_jira_test.go | 26 ++++++++++++++++++++++- internal/policy/commit/commit.go | 5 +++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/internal/policy/commit/check_jira.go b/internal/policy/commit/check_jira.go index 1fd439e2..9e6455aa 100644 --- a/internal/policy/commit/check_jira.go +++ b/internal/policy/commit/check_jira.go @@ -38,11 +38,12 @@ func (j *JiraCheck) Errors() []error { // ValidateJiraCheck validates if a Jira issue is mentioned in the header func (c Commit) ValidateJiraCheck() policy.Check { check := &JiraCheck{} - compile := regexp.MustCompile(`^(\w+)( \w+)?: \[(\w+)-\d+\] .*`) - if compile.MatchString(c.msg) { - submatch := compile.FindStringSubmatch(c.msg) - jiraProject := submatch[3] + reg := regexp.MustCompile(`.* \[?(\w+)-\d+\]?.*`) + + if reg.MatchString(c.msg) { + submatch := reg.FindStringSubmatch(c.msg) + jiraProject := submatch[1] if !find(c.Header.Jira.Keys, jiraProject) { check.errors = append(check.errors, errors.Errorf("Jira project %s is not a valid jira project", jiraProject)) diff --git a/internal/policy/commit/check_jira_test.go b/internal/policy/commit/check_jira_test.go index f8e616da..32ea5740 100644 --- a/internal/policy/commit/check_jira_test.go +++ b/internal/policy/commit/check_jira_test.go @@ -78,7 +78,7 @@ func TestCommit_ValidateJiraCheck(t *testing.T) { want: want{errorCount: 0}, }, { - name: "invalid jira project", + name: "Invalid jira project", fields: fields{ Header: &HeaderChecks{ Jira: &JiraChecks{ @@ -89,6 +89,30 @@ func TestCommit_ValidateJiraCheck(t *testing.T) { }, want: want{errorCount: 1}, }, + { + name: "Valid commit with scope", + fields: fields{ + Header: &HeaderChecks{ + Jira: &JiraChecks{ + Keys: []string{"JIRA", "PROJ"}, + }, + }, + msg: "fix(test): [PROJ-1234] valid commit", + }, + want: want{errorCount: 0}, + }, + { + name: "Valid commit without square brackets", + fields: fields{ + Header: &HeaderChecks{ + Jira: &JiraChecks{ + Keys: []string{"JIRA", "PROJ"}, + }, + }, + msg: "fix: PROJ-1234 valid commit", + }, + want: want{errorCount: 0}, + }, } for _, tt := range tests { tt := tt diff --git a/internal/policy/commit/commit.go b/internal/policy/commit/commit.go index 9b229994..58e96595 100644 --- a/internal/policy/commit/commit.go +++ b/internal/policy/commit/commit.go @@ -25,8 +25,9 @@ type HeaderChecks struct { // HeaderCase is the case that the first word of the header must have ("upper" or "lower"). Case string `mapstructure:"case"` // HeaderInvalidLastCharacters is a string containing all invalid last characters for the header. - InvalidLastCharacters string `mapstructure:"invalidLastCharacters"` - Jira *JiraChecks `mapstructure:"jira"` + InvalidLastCharacters string `mapstructure:"invalidLastCharacters"` + // Jira checks if the header containers a Jira project key. + Jira *JiraChecks `mapstructure:"jira"` } // JiraChecks is the configuration for checks for Jira issues