Skip to content
Open
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
52 changes: 42 additions & 10 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ var (
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
APIType: "auto",
Summary: `{{ template "jira.default.summary" . }}`,
Description: `{{ template "jira.default.description" . }}`,
Priority: `{{ template "jira.default.priority" . }}`,
APIType: "auto",
Summary: JiraFieldConfig{
Template: `{{ template "jira.default.summary" . }}`,
},
Description: JiraFieldConfig{
Template: `{{ template "jira.default.description" . }}`,
},
Priority: `{{ template "jira.default.priority" . }}`,
}

DefaultMattermostConfig = MattermostConfig{
Expand Down Expand Up @@ -969,19 +973,26 @@ func (c *MSTeamsV2Config) UnmarshalYAML(unmarshal func(any) error) error {
return nil
}

type JiraFieldConfig struct {
// Template is the template string used to render the field.
Template string `yaml:"template,omitempty" json:"template,omitempty"`
// DisableUpdate indicates whether this field should be omitted when updating an existing issue.
DisableUpdate bool `yaml:"disable_update,omitempty" json:"disable_update,omitempty"`
}

type JiraConfig struct {
NotifierConfig `yaml:",inline" json:",inline"`
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`

APIURL *URL `yaml:"api_url,omitempty" json:"api_url,omitempty"`
APIType string `yaml:"api_type,omitempty" json:"api_type,omitempty"`

Project string `yaml:"project,omitempty" json:"project,omitempty"`
Summary string `yaml:"summary,omitempty" json:"summary,omitempty"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
Labels []string `yaml:"labels,omitempty" json:"labels,omitempty"`
Priority string `yaml:"priority,omitempty" json:"priority,omitempty"`
IssueType string `yaml:"issue_type,omitempty" json:"issue_type,omitempty"`
Project string `yaml:"project,omitempty" json:"project,omitempty"`
Summary JiraFieldConfig `yaml:"summary,omitempty" json:"summary,omitempty"`
Description JiraFieldConfig `yaml:"description,omitempty" json:"description,omitempty"`
Labels []string `yaml:"labels,omitempty" json:"labels,omitempty"`
Priority string `yaml:"priority,omitempty" json:"priority,omitempty"`
IssueType string `yaml:"issue_type,omitempty" json:"issue_type,omitempty"`

ReopenTransition string `yaml:"reopen_transition,omitempty" json:"reopen_transition,omitempty"`
ResolveTransition string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty"`
Expand All @@ -991,6 +1002,27 @@ type JiraConfig struct {
Fields map[string]any `yaml:"fields,omitempty" json:"custom_fields,omitempty"`
}

// Supports both the legacy string and the new object form.
func (f *JiraFieldConfig) UnmarshalYAML(unmarshal func(any) error) error {
// Try simple string first (backward compatibility).
var s string
if err := unmarshal(&s); err == nil {
f.Template = s
// DisableUpdate stays false by default.
return nil
}

// Fallback to full object form.
type plain JiraFieldConfig
var cfg plain
if err := unmarshal(&cfg); err != nil {
return err
}

*f = JiraFieldConfig(cfg)
return nil
}

func (c *JiraConfig) UnmarshalYAML(unmarshal func(any) error) error {
*c = DefaultJiraConfig
type plain JiraConfig
Expand Down
22 changes: 17 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1159,11 +1159,23 @@ The default `jira.default.description` template only works with V2.
# The project key where issues are created.
project: <string>

# Issue summary template.
[ summary: <tmpl_string> | default = '{{ template "jira.default.summary" . }}' ]

# Issue description template.
[ description: <tmpl_string> | default = '{{ template "jira.default.description" . }}' ]
# Issue summary configuration.
[ summary:
# Template for the issue summary.
[ template: <tmpl_string> | default = '{{ template "jira.default.summary" . }}' ]

# If true, the summary will not be updated when updating an existing issue.
[ disable_update: <boolean> | default = false ]
]

# Issue description configuration.
[ description:
# Template for the issue description.
[ template: <tmpl_string> | default = '{{ template "jira.default.description" . }}' ]

# If true, the description will not be updated when updating an existing issue.
[ disable_update: <boolean> | default = false ]
]

# Labels to be added to the issue.
labels:
Expand Down
14 changes: 12 additions & 2 deletions notify/jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
return false, err
}

if method == http.MethodPut && requestBody.Fields != nil {
if n.conf.Description.DisableUpdate {
requestBody.Fields.Description = nil
}
if n.conf.Summary.DisableUpdate {
requestBody.Fields.Summary = nil
}
}

_, shouldRetry, err = n.doAPIRequest(ctx, method, path, requestBody)
if err != nil {
return shouldRetry, fmt.Errorf("failed to %s request to %q: %w", method, path, err)
Expand All @@ -121,10 +130,11 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

func (n *Notifier) prepareIssueRequestBody(_ context.Context, logger *slog.Logger, groupID string, tmplTextFunc template.TemplateFunc) (issue, error) {
summary, err := tmplTextFunc(n.conf.Summary)
summary, err := tmplTextFunc(n.conf.Summary.Template)
if err != nil {
return issue{}, fmt.Errorf("summary template: %w", err)
}

project, err := tmplTextFunc(n.conf.Project)
if err != nil {
return issue{}, fmt.Errorf("project template: %w", err)
Expand Down Expand Up @@ -161,7 +171,7 @@ func (n *Notifier) prepareIssueRequestBody(_ context.Context, logger *slog.Logge
Fields: fieldsWithStringKeys,
}}

issueDescriptionString, err := tmplTextFunc(n.conf.Description)
issueDescriptionString, err := tmplTextFunc(n.conf.Description.Template)
if err != nil {
return issue{}, fmt.Errorf("description template: %w", err)
}
Expand Down
Loading
Loading