Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ type JiraConfig struct {
ResolveTransition string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty"`
WontFixResolution string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty"`
ReopenDuration model.Duration `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty"`
HashIdentifier string `yaml:"hash_identifier,omitempty" json:"hash_identifier,omitempty"`

Fields map[string]any `yaml:"fields,omitempty" json:"custom_fields,omitempty"`
}
Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@ project: <string>
# Issue description template.
[ description: <tmpl_string> | default = '{{ template "jira.default.description" . }}' ]

# overrides the default ALERT{<group-hash>} jira label used to correlate issues.
# Example: {{ .GroupLabels }}
[ hash_identifier: <tmpl_string> ]

# Labels to be added to the issue.
labels:
[ - <tmpl_string> ... ]
Expand Down
18 changes: 16 additions & 2 deletions notify/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
method = http.MethodPost
)

existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, key.Hash(), alerts.HasFiring(), tmplTextFunc)
jiraHash := key.Hash()
// If a custom identifier is configured, use its hash instead.
if n.conf.HashIdentifier != "" {
identifier, err := tmplTextFunc(n.conf.HashIdentifier)
if err != nil {
return false, fmt.Errorf("hash_identifier: %w", err)
}

identifier = strings.TrimSpace(identifier)
if identifier != "" {
jiraHash = notify.Key(identifier).Hash()
}
}

existingIssue, shouldRetry, err := n.searchExistingIssue(ctx, logger, jiraHash, alerts.HasFiring(), tmplTextFunc)
if err != nil {
return shouldRetry, fmt.Errorf("failed to look up existing issues: %w", err)
}
Expand All @@ -106,7 +120,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
logger.Debug("updating existing issue", "issue_key", existingIssue.Key)
}

requestBody, err := n.prepareIssueRequestBody(ctx, logger, key.Hash(), tmplTextFunc)
requestBody, err := n.prepareIssueRequestBody(ctx, logger, jiraHash, tmplTextFunc)
if err != nil {
return false, err
}
Expand Down
47 changes: 47 additions & 0 deletions notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,53 @@ func TestJiraNotify(t *testing.T) {
issue issue
errMsg string
}{
{
title: "create new issue with group labels as hash identifier",
cfg: &config.JiraConfig{
Summary: `{{ template "jira.default.summary" . }}`,
Description: `{{ template "jira.default.description" . }}`,
IssueType: "Incident",
Project: "OPS",
Priority: `{{ template "jira.default.priority" . }}`,
Labels: []string{"alertmanager", "{{ .GroupLabels.alertname }}"},
ReopenDuration: model.Duration(1 * time.Hour),
ReopenTransition: "REOPEN",
ResolveTransition: "CLOSE",
WontFixResolution: "WONTFIX",
HashIdentifier: `{{ .GroupLabels }}`,
},
alert: &types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{
"alertname": "test",
"instance": "vm1",
"severity": "critical",
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
},
},
searchResponse: issueSearchResult{
Issues: []issue{},
},
issue: issue{
Key: "",
Fields: &issueFields{
Summary: "[FIRING:1] test (vm1 critical)",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n - severity = critical\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{
"ALERT{602a8de61908ae3ce97961992ec1ed4ea9500c331b79baaf6f565ccdc1750c0c}",
"alertmanager",
"test",
},
Project: &issueProject{Key: "OPS"},
Priority: &idNameValue{Name: "High"},
},
},
customFieldAssetFn: func(t *testing.T, issue map[string]any) {},
errMsg: "",
},
{
title: "create new issue",
cfg: &config.JiraConfig{
Expand Down
Loading