Skip to content

Commit 91101c8

Browse files
feat(jira.go): allow configuring issue update via parameter
Signed-off-by: Holger Waschke <[email protected]>
1 parent 389a0a5 commit 91101c8

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

config/notifiers.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,11 @@ type JiraConfig struct {
976976
Priority string `yaml:"priority,omitempty" json:"priority,omitempty"`
977977
IssueType string `yaml:"issue_type,omitempty" json:"issue_type,omitempty"`
978978

979-
ReopenTransition string `yaml:"reopen_transition,omitempty" json:"reopen_transition,omitempty"`
980-
ResolveTransition string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty"`
981-
WontFixResolution string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty"`
982-
ReopenDuration model.Duration `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty"`
979+
ReopenTransition string `yaml:"reopen_transition,omitempty" json:"reopen_transition,omitempty"`
980+
ResolveTransition string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty"`
981+
WontFixResolution string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty"`
982+
ReopenDuration model.Duration `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty"`
983+
DisableUpdateDescription bool `yaml:"disable_update_description,omitempty" json:"disable_update_description,omitempty"`
983984

984985
Fields map[string]any `yaml:"fields,omitempty" json:"custom_fields,omitempty"`
985986
}

notify/jira/jira.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
111111
return false, err
112112
}
113113

114+
if method == http.MethodPut && requestBody.Fields != nil && n.conf.DisableUpdateDescription {
115+
requestBody.Fields.Description = nil
116+
}
117+
114118
_, shouldRetry, err = n.doAPIRequest(ctx, method, path, requestBody)
115119
if err != nil {
116120
return shouldRetry, fmt.Errorf("failed to %s request to %q: %w", method, path, err)

notify/jira/jira_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,66 @@ func TestJiraNotify(t *testing.T) {
747747
customFieldAssetFn: func(t *testing.T, issue map[string]any) {},
748748
errMsg: "can't find transition REOPEN for issue OPS-3",
749749
},
750+
{
751+
title: "update existing issue when setting disableupdatedescription to true",
752+
cfg: &config.JiraConfig{
753+
Summary: `{{ template "jira.default.summary" . }}`,
754+
Description: `{{ template "jira.default.description" . }}`,
755+
IssueType: "Incident",
756+
Project: "OPS",
757+
Priority: `{{ template "jira.default.priority" . }}`,
758+
Labels: []string{"alertmanager", "{{ .GroupLabels.alertname }}"},
759+
DisableUpdateDescription: true,
760+
},
761+
alert: &types.Alert{
762+
Alert: model.Alert{
763+
Labels: model.LabelSet{
764+
"alertname": "test",
765+
"instance": "vm1",
766+
"severity": "critical",
767+
},
768+
StartsAt: time.Now(),
769+
EndsAt: time.Now().Add(time.Hour),
770+
},
771+
},
772+
searchResponse: issueSearchResult{
773+
Issues: []issue{
774+
{
775+
Key: "OPS-4",
776+
Fields: &issueFields{
777+
Status: &issueStatus{
778+
Name: "Open",
779+
StatusCategory: struct {
780+
Key string `json:"key"`
781+
}{
782+
Key: "open",
783+
},
784+
},
785+
},
786+
},
787+
},
788+
},
789+
issue: issue{
790+
Key: "",
791+
Fields: &issueFields{
792+
Summary: "[FIRING:1] test (vm1 critical)",
793+
Description: nil,
794+
Issuetype: &idNameValue{Name: "Incident"},
795+
Labels: []string{
796+
"ALERT{6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b}",
797+
"alertmanager",
798+
"test",
799+
},
800+
Project: &issueProject{Key: "OPS"},
801+
Priority: &idNameValue{Name: "High"},
802+
},
803+
},
804+
customFieldAssetFn: func(t *testing.T, fields map[string]any) {
805+
_, has := fields["description"]
806+
require.False(t, has, "description field must be omitted on update when UpdateDescription=false")
807+
},
808+
errMsg: "",
809+
},
750810
} {
751811
tc := tc
752812

@@ -841,6 +901,7 @@ func TestJiraNotify(t *testing.T) {
841901
case "/issue/OPS-1":
842902
case "/issue/OPS-2":
843903
case "/issue/OPS-3":
904+
case "/issue/OPS-4":
844905
fallthrough
845906
case "/issue":
846907
body, err := io.ReadAll(r.Body)

notify/jira/types.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ type issueTransitions struct {
7070

7171
// MarshalJSON merges the struct issueFields and issueFields.CustomField together.
7272
func (i issueFields) MarshalJSON() ([]byte, error) {
73-
jsonFields := map[string]any{
74-
"description": i.Description,
75-
"summary": i.Summary,
73+
jsonFields := map[string]interface{}{
74+
"summary": i.Summary,
7675
}
7776

77+
if i.Description != nil {
78+
jsonFields["description"] = i.Description
79+
}
7880
if i.Issuetype != nil {
7981
jsonFields["issuetype"] = i.Issuetype
8082
}

0 commit comments

Comments
 (0)