Skip to content

Commit

Permalink
feat(conf): add optional field_labels to override Labels
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Melo <[email protected]>
  • Loading branch information
cropalato committed May 28, 2024
1 parent 749b943 commit 3611d42
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
67 changes: 33 additions & 34 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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 == "" {
Expand Down
22 changes: 11 additions & 11 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit 3611d42

Please sign in to comment.