-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for requiring a Jira issue in the header
This is meant to fix #180 Signed-off-by: Per Abich <per@abich.com>
- Loading branch information
Showing
7 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build | ||
cache | ||
vendor | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package commit | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/talos-systems/conform/internal/policy" | ||
) | ||
|
||
// JiraCheck enforces that a Jira issue is mentioned in the header | ||
type JiraCheck struct { | ||
errors []error | ||
} | ||
|
||
// Name returns the name of the check. | ||
func (j *JiraCheck) Name() string { | ||
return "Jira issues" | ||
} | ||
|
||
// Message returns to check message. | ||
func (j *JiraCheck) Message() string { | ||
if len(j.errors) != 0 { | ||
return j.errors[0].Error() | ||
} | ||
|
||
return "Jira issues are invalid" | ||
} | ||
|
||
// Errors returns any violations of the check. | ||
func (j *JiraCheck) Errors() []error { | ||
return j.errors | ||
} | ||
|
||
// 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] | ||
|
||
if !find(c.Header.Jira.Keys, jiraProject) { | ||
check.errors = append(check.errors, errors.Errorf("Jira project %s is not a valid jira project", jiraProject)) | ||
} | ||
} else { | ||
check.errors = append(check.errors, errors.Errorf("No Jira issue tag found in %q", c.msg)) | ||
} | ||
|
||
return check | ||
} | ||
|
||
func find(slice []string, value string) bool { | ||
for _, elem := range slice { | ||
if elem == value { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package commit | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCommit_ValidateJiraCheck(t *testing.T) { | ||
type fields struct { | ||
SpellCheck *SpellCheck | ||
Conventional *Conventional | ||
Header *HeaderChecks | ||
Body *BodyChecks | ||
DCO bool | ||
GPG bool | ||
MaximumOfOneCommit bool | ||
msg string | ||
} | ||
|
||
type want struct { | ||
errorCount int | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
want want | ||
}{ | ||
{ | ||
name: "Missing jira issue no type", | ||
fields: fields{ | ||
Header: &HeaderChecks{ | ||
Jira: &JiraChecks{ | ||
Keys: []string{"JIRA", "PROJ"}, | ||
}, | ||
}, | ||
msg: "invalid commit", | ||
}, | ||
want: want{errorCount: 1}, | ||
}, | ||
{ | ||
name: "Missing jira issue with type", | ||
fields: fields{ | ||
Header: &HeaderChecks{ | ||
Jira: &JiraChecks{ | ||
Keys: []string{"JIRA", "PROJ"}, | ||
}, | ||
}, | ||
msg: "fix: invalid commit", | ||
}, | ||
want: want{errorCount: 1}, | ||
}, | ||
{ | ||
name: "Valid commit", | ||
fields: fields{ | ||
Header: &HeaderChecks{ | ||
Jira: &JiraChecks{ | ||
Keys: []string{"JIRA", "PROJ"}, | ||
}, | ||
}, | ||
msg: "fix: [JIRA-1234] valid commit", | ||
}, | ||
want: want{errorCount: 0}, | ||
}, | ||
{ | ||
name: "Valid commit 2", | ||
fields: fields{ | ||
Header: &HeaderChecks{ | ||
Jira: &JiraChecks{ | ||
Keys: []string{"JIRA", "PROJ"}, | ||
}, | ||
}, | ||
msg: "fix: [PROJ-1234] valid commit", | ||
}, | ||
want: want{errorCount: 0}, | ||
}, | ||
{ | ||
name: "invalid jira project", | ||
fields: fields{ | ||
Header: &HeaderChecks{ | ||
Jira: &JiraChecks{ | ||
Keys: []string{"JIRA", "PROJ"}, | ||
}, | ||
}, | ||
msg: "fix: [FALSE-1234] valid commit", | ||
}, | ||
want: want{errorCount: 1}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := Commit{ | ||
SpellCheck: tt.fields.SpellCheck, | ||
Conventional: tt.fields.Conventional, | ||
Header: tt.fields.Header, | ||
Body: tt.fields.Body, | ||
DCO: tt.fields.DCO, | ||
GPG: tt.fields.GPG, | ||
MaximumOfOneCommit: tt.fields.MaximumOfOneCommit, | ||
msg: tt.fields.msg, | ||
} | ||
got := c.ValidateJiraCheck() | ||
if len(got.Errors()) != tt.want.errorCount { | ||
t.Errorf("Wanted %d errors but got %d errors: %v", tt.want.errorCount, len(got.Errors()), got.Errors()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters