Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jira integration template customfields #4029

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions asset/assets_vfsdata.go
holger-waschke marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions notify/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ func (n *Notifier) prepareIssueRequestBody(ctx context.Context, logger *slog.Log
return issue{}, fmt.Errorf("convertToMarshalMap: %w", err)
}

for key, value := range fieldsWithStringKeys {
if strValue, ok := value.(string); ok {
processedValue, err := tmplTextFunc(strValue)
if err != nil {
return issue{}, fmt.Errorf("field %s template: %w", key, err)
}
fieldsWithStringKeys[key] = processedValue
}
}

summary, truncated := notify.TruncateInRunes(summary, maxSummaryLenRunes)
if truncated {
logger.Warn("Truncated summary", "max_runes", maxSummaryLenRunes)
Expand Down
8 changes: 7 additions & 1 deletion notify/jira/jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func TestJiraTemplating(t *testing.T) {
cfg: &config.JiraConfig{
Summary: `{{ template "jira.default.summary" . }}`,
Description: `{{ template "jira.default.description" . }}`,
Fields: map[string]any{
"customfield_14400": `{{ template "jira.host" . }}`,
},
},
retry: false,
},
Expand Down Expand Up @@ -227,6 +230,7 @@ func TestJiraNotify(t *testing.T) {
"customfield_10006": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": "green"}},
"customfield_10007": []map[any]any{{"value": "red"}, {"value": "blue"}, {"value": 0}},
"customfield_10008": []map[any]any{{"value": 0}, {"value": 1}, {"value": 2}},
"customfield_14400": `{{ template "jira.host" . }}`,
},
ReopenDuration: model.Duration(1 * time.Hour),
ReopenTransition: "REOPEN",
Expand All @@ -238,6 +242,7 @@ func TestJiraNotify(t *testing.T) {
Labels: model.LabelSet{
"alertname": "test",
"instance": "vm1",
"hostname": "host1.exmple.com",
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
Expand All @@ -251,14 +256,15 @@ func TestJiraNotify(t *testing.T) {
Key: "",
Fields: &issueFields{
Summary: strings.Repeat("A", maxSummaryLenRunes-1) + "…",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Description: "\n\n# Alerts Firing:\n\nLabels:\n - alertname = test\n - hostname = host1.exmple.com\n - instance = vm1\n\nAnnotations:\n\nSource: \n\n\n\n\n",
Issuetype: &idNameValue{Name: "Incident"},
Labels: []string{"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}", "alertmanager", "test"},
Project: &issueProject{Key: "OPS"},
},
},
customFieldAssetFn: func(t *testing.T, issue map[string]any) {
require.Equal(t, "value", issue["customfield_10001"])
require.Equal(t, "host1.exmple.com", issue["customfield_14400"])
require.Equal(t, float64(0), issue["customfield_10002"])
require.Equal(t, []any{float64(0)}, issue["customfield_10003"])
require.Equal(t, map[string]any{"value": "red"}, issue["customfield_10004"])
Expand Down
8 changes: 8 additions & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ Alerts Resolved:
{{- $priority -}}
{{- end -}}

{{ define "jira.host" }}
{{- with index .Alerts 0 -}}
{{- if .Labels.hostname -}}
{{- .Labels.hostname -}}
{{- end -}}
{{- end -}}
{{ end }}

{{ define "rocketchat.default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "rocketchat.default.alias" }}{{ template "__alertmanager" . }}{{ end }}
{{ define "rocketchat.default.titlelink" }}{{ template "__alertmanagerURL" . }}{{ end }}
Expand Down