Skip to content

Commit

Permalink
feat: improve Jira check
Browse files Browse the repository at this point in the history
Fixes a few minor issues:

- make square brackets optional
- allow for conventional commit scopes to be present

Signed-off-by: Andrew Rynhard <[email protected]>
  • Loading branch information
andrewrynhard authored and talos-bot committed Nov 18, 2020
1 parent 001de56 commit efd7fbb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
9 changes: 5 additions & 4 deletions internal/policy/commit/check_jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
26 changes: 25 additions & 1 deletion internal/policy/commit/check_jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions internal/policy/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efd7fbb

Please sign in to comment.