/
check_jira.go
67 lines (51 loc) · 1.52 KB
/
check_jira.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 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/siderolabs/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 valid"
}
// 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 { //nolint:ireturn
check := &JiraCheck{}
reg := regexp.MustCompile(`.* \[?([A-Z]*)-[1-9]{1}\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))
}
} 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
}