diff --git a/pkg/config/config.go b/pkg/config/config.go index 8feb410..72cb3f0 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -23,7 +23,7 @@ import ( "strings" "time" - "github.com/andygrunwald/go-jira" + "github.com/andygrunwald/go-jira" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -199,26 +199,26 @@ func (c Config) String() string { // GetJiraFieldKey returns the jira key associated to a field. func GetJiraFieldKey(client *jira.Client, project string, issueType string, field string) (string, error) { - options := &jira.GetQueryOptions{ - ProjectKeys: project, - Expand: "projects.issuetypes.fields", - } - meta, _, err := client.Issue.GetCreateMetaWithOptions(options) - if err != nil { - return "", err - } - it := meta.Projects[0].GetIssueTypeWithName(issueType) - if it == nil { - return "", fmt.Errorf("jira: Issue type %s not found", issueType) - } - fields, err := it.GetAllFields() - if err != nil { - return "", err - } - if val, ok := fields[field]; ok { - return val, nil - } - return "", fmt.Errorf("jira: Field %s not found in %s issue type", field, issueType) + options := &jira.GetQueryOptions{ + ProjectKeys: project, + Expand: "projects.issuetypes.fields", + } + meta, _, err := client.Issue.GetCreateMetaWithOptions(options) + if err != nil { + return "", err + } + it := meta.Projects[0].GetIssueTypeWithName(issueType) + if it == nil { + return "", fmt.Errorf("jira: Issue type %s not found", issueType) + } + fields, err := it.GetAllFields() + if err != nil { + return "", err + } + if val, ok := fields[field]; ok { + return val, nil + } + return "", fmt.Errorf("jira: Field %s not found in %s issue type", field, issueType) } // UnmarshalYAML implements the yaml.Unmarshaler interface. @@ -325,14 +325,14 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { rc.WontFixResolution = c.Defaults.WontFixResolution } if rc.FieldLabels == "" { - if c.Defaults.FieldLabels != "" { - rc.FieldLabels = c.Defaults.FieldLabels - } else { - rc.FieldLabels = "Labels" - } + if c.Defaults.FieldLabels != "" { + rc.FieldLabels = c.Defaults.FieldLabels + } else { + rc.FieldLabels = "Labels" + } } - // descover jira labels key. + // descover jira labels key. var client *jira.Client var err error if rc.User != "" && rc.Password != "" { @@ -352,13 +352,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } - rc.FieldLabelsKey, err = GetJiraFieldKey(client, rc.Project, rc.IssueType, rc.FieldLabels) - if err != nil { - return err - } - fmt.Printf("\n\nrc.FieldLabelsKey=%s\n",rc.FieldLabels) - fmt.Printf("rc.FieldLabelsKey=%s\n",rc.FieldLabelsKey) - + rc.FieldLabelsKey, err = GetJiraFieldKey(client, rc.Project, rc.IssueType, rc.FieldLabels) + if err != nil { + return err + } + fmt.Printf("\n\nrc.FieldLabelsKey=%s\n", rc.FieldLabels) + fmt.Printf("rc.FieldLabelsKey=%s\n", rc.FieldLabelsKey) if rc.AutoResolve != nil { if rc.AutoResolve.State == "" { diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 1c6e6f0..ba2da97 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -193,11 +193,11 @@ func (r *Receiver) Notify(data *alertmanager.Data, hashJiraLabel bool, updateSum Unknowns: tcontainer.NewMarshalMap(), }, } - if r.conf.FieldLabels == "Labels" { - issue.Fields.Labels = append(staticLabels, issueGroupLabel) - } else { - issue.Fields.Unknowns[r.conf.FieldLabelsKey] = append(staticLabels, issueGroupLabel) - } + if r.conf.FieldLabels == "Labels" { + issue.Fields.Labels = append(staticLabels, issueGroupLabel) + } else { + issue.Fields.Unknowns[r.conf.FieldLabelsKey] = append(staticLabels, issueGroupLabel) + } if r.conf.Priority != "" { issuePrio, err := r.tmpl.Execute(r.conf.Priority, data) if err != nil { @@ -316,14 +316,14 @@ func toGroupTicketLabel(groupLabels alertmanager.KV, hashJiraLabel bool) string } func (r *Receiver) search(projects []string, issueLabel string) (*jira.Issue, bool, error) { - var labelKey string + var labelKey string // Search multiple projects in case issue was moved and further alert firings are desired in existing JIRA. projectList := "'" + strings.Join(projects, "', '") + "'" - if r.conf.FieldLabels == "Labels" { - labelKey = "labels" - } else { - labelKey = fmt.Sprintf("cf[%s]", strings.Split(r.conf.FieldLabelsKey, "_")[1]) - } + if r.conf.FieldLabels == "Labels" { + labelKey = "labels" + } else { + labelKey = fmt.Sprintf("cf[%s]", strings.Split(r.conf.FieldLabelsKey, "_")[1]) + } query := fmt.Sprintf("project in(%s) and %s=%q order by resolutiondate desc", projectList, labelKey, issueLabel) options := &jira.SearchOptions{ Fields: []string{"summary", "status", "resolution", "resolutiondate", "description", "comment"},