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

add ability to update in comments #161

Closed
Closed
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: 3 additions & 1 deletion examples/jiralert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ receivers:
#
# Automatically resolve jira issues when alert is resolved. Optional. If declared, ensure state is not an empty string.
auto_resolve:
state: 'Done'
state: 'Done'
# Include ticket update as comment. Optional (default: false).
update_in_comment: false

# File containing template definitions. Required.
template: jiralert.tmpl
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type ReceiverConfig struct {
// Label copy settings
AddGroupLabels bool `yaml:"add_group_labels" json:"add_group_labels"`

// Flag to enable updates in comments.
UpdateInComment bool `yaml:"update_in_comment" json:"update_in_comment"`

// Flag to auto-resolve opened issue when the alert is resolved.
AutoResolve *AutoResolve `yaml:"auto_resolve" json:"auto_resolve"`

Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ receivers:
project: AB
# Copy all Prometheus labels into separate JIRA labels. Optional (default: false).
add_group_labels: false
update_in_comment: false
static_labels: ["somelabel"]

- name: 'jira-xy'
Expand Down Expand Up @@ -128,6 +129,7 @@ type receiverTestConfig struct {
Description string `yaml:"description,omitempty"`
WontFixResolution string `yaml:"wont_fix_resolution,omitempty"`
AddGroupLabels bool `yaml:"add_group_labels,omitempty"`
UpdateInComment bool `yaml:"update_in_comment,omitempty"`
StaticLabels []string `yaml:"static_labels" json:"static_labels"`

AutoResolve *AutoResolve `yaml:"auto_resolve" json:"auto_resolve"`
Expand Down Expand Up @@ -330,6 +332,8 @@ func TestReceiverOverrides(t *testing.T) {
{"Description", "A nice description", "A nice description"},
{"WontFixResolution", "Won't Fix", "Won't Fix"},
{"AddGroupLabels", false, false},
{"UpdateInComment", false, false},
{"UpdateInComment", true, true},
{"AutoResolve", &AutoResolve{State: "Done"}, &autoResolve},
{"StaticLabels", []string{"somelabel"}, []string{"somelabel"}},
} {
Expand Down
23 changes: 23 additions & 0 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type jiraIssueService interface {

Create(issue *jira.Issue) (*jira.Issue, *jira.Response, error)
UpdateWithOptions(issue *jira.Issue, opts *jira.UpdateQueryOptions) (*jira.Issue, *jira.Response, error)
AddComment(issueID string, comment *jira.Comment) (*jira.Comment, *jira.Response, error)
DoTransition(ticketID, transitionID string) (*jira.Response, error)
}

Expand Down Expand Up @@ -112,6 +113,13 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum
}
}

if r.conf.UpdateInComment {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works better for me to avoid flodding of comments if repeat interval is set low

		if r.conf.UpdateInComment {
			if issue.Fields.Description != issueDesc {
				retry, err := r.addComment(issue.Key, issueDesc)
				if err != nil {
					return retry, err
				}
			}
		}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add descriptionhere

func (r *Receiver) search(project, issueLabel string) (*jira.Issue, bool, error) {
	query := fmt.Sprintf("project=\"%s\" and labels=%q order by resolutiondate desc", project, issueLabel)
	options := &jira.SearchOptions{
		Fields:     []string{"summary", "status", "resolution", "resolutiondate", "description"},
		MaxResults: 2,
	}

retry, err := r.addComment(issue.Key, issueDesc)
if err != nil {
return retry, err
}
}

if len(data.Alerts.Firing()) == 0 {
if r.conf.AutoResolve != nil {
level.Debug(r.logger).Log("msg", "no firing alert; resolving issue", "key", issue.Key, "label", issueGroupLabel)
Expand Down Expand Up @@ -370,6 +378,21 @@ func (r *Receiver) updateDescription(issueKey string, description string) (bool,
return false, nil
}

func (r *Receiver) addComment(issueKey string, content string) (bool, error) {
level.Debug(r.logger).Log("msg", "adding comment to existing issue", "key", issueKey, "content", content)

commentDetails := &jira.Comment{
Body: content,
}

comment, resp, err := r.client.AddComment(issueKey, commentDetails)
if err != nil {
return handleJiraErrResponse("Issue.AddComment", resp, err, r.logger)
}
level.Debug(r.logger).Log("msg", "added comment to issue", "key", issueKey, "id", comment.ID)
return false, nil
}

func (r *Receiver) reopen(issueKey string) (bool, error) {
return r.doTransition(issueKey, r.conf.ReopenState)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (f *fakeJira) UpdateWithOptions(old *jira.Issue, _ *jira.UpdateQueryOptions
return issue, nil, nil
}

func (f *fakeJira) AddComment(issueID string, comment *jira.Comment) (*jira.Comment, *jira.Response, error) {
// This is a placeholder to keep fakeJira compatible with the updated Interface
return nil, nil, nil
}

func (f *fakeJira) DoTransition(ticketID, transitionID string) (*jira.Response, error) {
issue, ok := f.issuesByKey[ticketID]
if !ok {
Expand Down