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

search for existing issue in multiple projects #162

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -25,6 +25,8 @@ defaults:
reopen_duration: 0h
# Static label that will be added to the JIRA ticket alongisde the JIRALERT{...} or ALERT{...} label
static_labels: ["custom"]
# Other projects that issues may be moved to and further updates are desired
twotired marked this conversation as resolved.
Show resolved Hide resolved
other_projects: ["OTHER1", "OTHER2"]

# Receiver definitions. At least one must be defined.
receivers:
Expand Down Expand Up @@ -56,7 +58,7 @@ 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'

# File containing template definitions. Required.
template: jiralert.tmpl
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type ReceiverConfig struct {

// Required issue fields
Project string `yaml:"project" json:"project"`
OtherProjects []string `yaml:"other_projects" json:"other_projects"`
IssueType string `yaml:"issue_type" json:"issue_type"`
Summary string `yaml:"summary" json:"summary"`
ReopenState string `yaml:"reopen_state" json:"reopen_state"`
Expand Down Expand Up @@ -311,6 +312,9 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if len(c.Defaults.StaticLabels) > 0 {
rc.StaticLabels = append(rc.StaticLabels, c.Defaults.StaticLabels...)
}
if len(c.Defaults.OtherProjects) > 0 {
rc.OtherProjects = append(rc.OtherProjects, c.Defaults.OtherProjects...)
}
}

if len(c.Receivers) == 0 {
Expand Down
16 changes: 13 additions & 3 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool) string
return strings.Replace(buf.String(), " ", "", -1)
}

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)
func (r *Receiver) search(projects []string, issueLabel string) (*jira.Issue, bool, error) {
// search multiple projects in case issue was moved and further alert firings are desired in existing JIRA
twotired marked this conversation as resolved.
Show resolved Hide resolved
projectList := "'" + strings.Join(projects, "', '") + "'"
query := fmt.Sprintf("project in(%s) and labels=%q order by resolutiondate desc", projectList, issueLabel)
options := &jira.SearchOptions{
Fields: []string{"summary", "status", "resolution", "resolutiondate"},
MaxResults: 2,
Expand Down Expand Up @@ -317,7 +319,15 @@ func (r *Receiver) search(project, issueLabel string) (*jira.Issue, bool, error)
}

func (r *Receiver) findIssueToReuse(project string, issueGroupLabel string) (*jira.Issue, bool, error) {
issue, retry, err := r.search(project, issueGroupLabel)
projectsToSearch := []string{project}
// in case issue was moved to a different project, include configured potential other projects in search
twotired marked this conversation as resolved.
Show resolved Hide resolved
for _, other := range r.conf.OtherProjects {
if other != project {
projectsToSearch = append(projectsToSearch, other)
}
}

issue, retry, err := r.search(projectsToSearch, issueGroupLabel)
if err != nil {
return nil, retry, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/notify/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (f *fakeJira) Create(issue *jira.Issue) (*jira.Issue, *jira.Response, error

// Assuming single label.
query := fmt.Sprintf(
"project=\"%s\" and labels=%q order by resolutiondate desc",
"project in('%s') and labels=%q order by resolutiondate desc",
Copy link
Member

Choose a reason for hiding this comment

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

Why is that change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We must use an "in" JQL expression to compare "project" to a list of projects instead of testing equality with a single project. It works fine if list length is 1.

issue.Fields.Project.Key,
issue.Fields.Labels[0],
)
Expand Down