-
Notifications
You must be signed in to change notification settings - Fork 173
/
jira.go
45 lines (39 loc) · 1.16 KB
/
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
package transform
import (
"fmt"
"log"
"regexp"
"github.com/prymitive/karma/internal/models"
)
type jiraDetectRule struct {
Regexp *regexp.Regexp
URL string
}
var jiraDetectRules = []jiraDetectRule{}
// ParseRules will parse and validate list of JIRA detection rules provided
// from config, valid rules will be stored for future use in DetectJIRAs() calls
func ParseRules(rules []models.JiraRule) {
for _, rule := range rules {
if rule.Regex == "" || rule.URI == "" {
log.Fatalf("Invalid JIRA rule with regexp '%s' and url '%s'", rule.Regex, rule.URI)
}
jdr := jiraDetectRule{
Regexp: regexp.MustCompile(rule.Regex),
URL: rule.URI,
}
jiraDetectRules = append(jiraDetectRules, jdr)
}
}
// DetectJIRAs will try to find JIRA links in Alertmanager silence objects
// using regexp rules from configuration that were parsed and populated
// by ParseRules call
func DetectJIRAs(silence *models.Silence) (jiraID, jiraLink string) {
for _, jdr := range jiraDetectRules {
jiraID := jdr.Regexp.FindString(silence.Comment)
if jiraID != "" {
jiraLink := fmt.Sprintf("%s/browse/%s", jdr.URL, jiraID)
return jiraID, jiraLink
}
}
return "", ""
}