From 177ee505fb6a9f6e17aa2b84af3c74a422805093 Mon Sep 17 00:00:00 2001 From: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> Date: Tue, 25 Apr 2023 19:43:19 +0200 Subject: [PATCH] disable update existing jira issues with parameter (#150) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Bump all dependencies (#133) Signed-off-by: Jan-Otto Kröpke Signed-off-by: Jan-Otto Kröpke Signed-off-by: Holger Waschke * parameter to disable update jira issues Signed-off-by: Holger Waschke Signed-off-by: Holger Waschke * rename parameter to make it more clear and avoid double negation. fix bug with missing return value. Signed-off-by: Holger Waschke * Update main.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * Update notify.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * Update main.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * Update main.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * Update main.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * Update notify.go Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> * fix for notify test Signed-off-by: Holger Waschke --------- Signed-off-by: Jan-Otto Kröpke Signed-off-by: Holger Waschke Signed-off-by: Holger Waschke Signed-off-by: dvag-holger-waschke <85643002+dvag-holger-waschke@users.noreply.github.com> Co-authored-by: Jan-Otto Kröpke Co-authored-by: Holger Waschke --- cmd/jiralert/main.go | 5 ++++- pkg/notify/notify.go | 41 +++++++++++++++++++++++---------------- pkg/notify/notify_test.go | 5 +++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/cmd/jiralert/main.go b/cmd/jiralert/main.go index a850570..86be130 100644 --- a/cmd/jiralert/main.go +++ b/cmd/jiralert/main.go @@ -48,6 +48,9 @@ var ( logFormat = flag.String("log.format", logFormatLogfmt, "Log format to use ("+logFormatLogfmt+", "+logFormatJSON+")") hashJiraLabel = flag.Bool("hash-jira-label", false, "if enabled: renames ALERT{...} to JIRALERT{...}; also hashes the key-value pairs inside of JIRALERT{...} in the created jira issue labels"+ "- this ensures that the label text does not overflow the allowed length in jira (255)") + updateSummary = flag.Bool("update-summary", true, "When false, jiralert does not update the summary of the existing jira issue, even when changes are spotted.") + updateDescription = flag.Bool("update-description", true, "When false, jiralert does not update the description of the existing jira issue, even when changes are spotted.") + reopenTickets = flag.Bool("reopen-tickets", true, "When false, jiralert does not reopen tickets.") // Version is the build version, set by make to latest git tag/hash via `-ldflags "-X main.Version=$(VERSION)"`. Version = "" @@ -121,7 +124,7 @@ func main() { return } - if retry, err := notify.NewReceiver(logger, conf, tmpl, client.Issue).Notify(&data, *hashJiraLabel); err != nil { + if retry, err := notify.NewReceiver(logger, conf, tmpl, client.Issue).Notify(&data, *hashJiraLabel, *updateSummary, *updateDescription, *reopenTickets); err != nil { var status int if retry { // Instruct Alertmanager to retry. diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index c06e881..0191814 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -60,7 +60,7 @@ func NewReceiver(logger log.Logger, c *config.ReceiverConfig, t *template.Templa } // Notify manages JIRA issues based on alertmanager webhook notify message. -func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool) (bool, error) { +func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSummary bool, updateDescription bool, reopenTickets bool) (bool, error) { project, err := r.tmpl.Execute(r.conf.Project, data) if err != nil { return false, errors.Wrap(err, "generate project from template") @@ -87,18 +87,24 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool) (bool, er } if issue != nil { + // Update summary if needed. - if issue.Fields.Summary != issueSummary { - retry, err := r.updateSummary(issue.Key, issueSummary) - if err != nil { - return retry, err + if updateSummary { + if issue.Fields.Summary != issueSummary { + level.Debug(r.logger).Log("updateSummaryDisabled executing") + retry, err := r.updateSummary(issue.Key, issueSummary) + if err != nil { + return retry, err + } } } - if issue.Fields.Description != issueDesc { - retry, err := r.updateDescription(issue.Key, issueDesc) - if err != nil { - return retry, err + if updateDescription { + if issue.Fields.Description != issueDesc { + retry, err := r.updateDescription(issue.Key, issueDesc) + if err != nil { + return retry, err + } } } @@ -122,18 +128,19 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool) (bool, er return false, nil } - if r.conf.WontFixResolution != "" && issue.Fields.Resolution != nil && - issue.Fields.Resolution.Name == r.conf.WontFixResolution { - level.Info(r.logger).Log("msg", "issue was resolved as won't fix, not reopening", "key", issue.Key, "label", issueGroupLabel, "resolution", issue.Fields.Resolution.Name) - return false, nil - } + if reopenTickets { + if r.conf.WontFixResolution != "" && issue.Fields.Resolution != nil && + issue.Fields.Resolution.Name == r.conf.WontFixResolution { + level.Info(r.logger).Log("msg", "issue was resolved as won't fix, not reopening", "key", issue.Key, "label", issueGroupLabel, "resolution", issue.Fields.Resolution.Name) + return false, nil + } - if r.conf.ReopenEnabled != nil && !*r.conf.ReopenEnabled { - level.Debug(r.logger).Log("msg", "reopening disabled, skipping search for existing issue") - } else { level.Info(r.logger).Log("msg", "issue was recently resolved, reopening", "key", issue.Key, "label", issueGroupLabel) return r.reopen(issue.Key) } + + level.Debug(r.logger).Log("Did not update anything") + return false, nil } if len(data.Alerts.Firing()) == 0 { diff --git a/pkg/notify/notify_test.go b/pkg/notify/notify_test.go index 78ef554..4d3fe65 100644 --- a/pkg/notify/notify_test.go +++ b/pkg/notify/notify_test.go @@ -14,12 +14,13 @@ package notify import ( "fmt" - "github.com/andygrunwald/go-jira" "os" "sort" "testing" "time" + "github.com/andygrunwald/go-jira" + "github.com/trivago/tgo/tcontainer" "github.com/go-kit/log" @@ -550,7 +551,7 @@ func TestNotify_JIRAInteraction(t *testing.T) { return testNowTime } - _, err := receiver.Notify(tcase.inputAlert, true) + _, err := receiver.Notify(tcase.inputAlert, true, true, true, true) require.NoError(t, err) require.Equal(t, tcase.expectedJiraIssues, fakeJira.issuesByKey) }); !ok {