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 a new receiver for Jira Service Management #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions notify/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/grafana/alerting/receivers/discord"
"github.com/grafana/alerting/receivers/email"
"github.com/grafana/alerting/receivers/googlechat"
"github.com/grafana/alerting/receivers/jsm"
"github.com/grafana/alerting/receivers/kafka"
"github.com/grafana/alerting/receivers/line"
"github.com/grafana/alerting/receivers/opsgenie"
Expand Down Expand Up @@ -98,6 +99,9 @@ func BuildReceiverIntegrations(
for i, cfg := range receiver.OpsgenieConfigs {
ci(i, cfg.Metadata, opsgenie.New(cfg.Settings, cfg.Metadata, tmpl, nw(cfg.Metadata), img, nl(cfg.Metadata)))
}
for i, cfg := range receiver.JsmConfigs {
ci(i, cfg.Metadata, jsm.New(cfg.Settings, cfg.Metadata, tmpl, nw(cfg.Metadata), img, nl(cfg.Metadata)))
}
for i, cfg := range receiver.PagerdutyConfigs {
ci(i, cfg.Metadata, pagerduty.New(cfg.Settings, cfg.Metadata, tmpl, nw(cfg.Metadata), img, nl(cfg.Metadata)))
}
Expand Down
2 changes: 1 addition & 1 deletion notify/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestBuildReceiverIntegrations(t *testing.T) {
require.Len(t, loggerNames, qty)
})
t.Run("should call webhook factory for each config that needs it", func(t *testing.T) {
require.Len(t, webhooks, 17) // we have 17 notifiers that support webhook
require.Len(t, webhooks, 18) // we have 17 notifiers that support webhook
})
t.Run("should call email factory for each config that needs it", func(t *testing.T) {
require.Len(t, emails, 1) // we have only email notifier that needs sender
Expand Down
8 changes: 8 additions & 0 deletions notify/receivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/grafana/alerting/receivers/discord"
"github.com/grafana/alerting/receivers/email"
"github.com/grafana/alerting/receivers/googlechat"
"github.com/grafana/alerting/receivers/jsm"
"github.com/grafana/alerting/receivers/kafka"
"github.com/grafana/alerting/receivers/line"
"github.com/grafana/alerting/receivers/opsgenie"
Expand Down Expand Up @@ -337,6 +338,7 @@ type GrafanaReceiverConfig struct {
KafkaConfigs []*NotifierConfig[kafka.Config]
LineConfigs []*NotifierConfig[line.Config]
OpsgenieConfigs []*NotifierConfig[opsgenie.Config]
JsmConfigs []*NotifierConfig[jsm.Config]
PagerdutyConfigs []*NotifierConfig[pagerduty.Config]
PushoverConfigs []*NotifierConfig[pushover.Config]
SensugoConfigs []*NotifierConfig[sensugo.Config]
Expand Down Expand Up @@ -437,6 +439,12 @@ func parseNotifier(ctx context.Context, result *GrafanaReceiverConfig, receiver
return err
}
result.OpsgenieConfigs = append(result.OpsgenieConfigs, newNotifierConfig(receiver, cfg))
case "jsm":
cfg, err := jsm.NewConfig(receiver.Settings, decryptFn)
if err != nil {
return err
}
result.JsmConfigs = append(result.JsmConfigs, newNotifierConfig(receiver, cfg))
case "pagerduty":
cfg, err := pagerduty.NewConfig(receiver.Settings, decryptFn)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions notify/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/grafana/alerting/receivers/discord"
"github.com/grafana/alerting/receivers/email"
"github.com/grafana/alerting/receivers/googlechat"
"github.com/grafana/alerting/receivers/jsm"
"github.com/grafana/alerting/receivers/kafka"
"github.com/grafana/alerting/receivers/line"
"github.com/grafana/alerting/receivers/opsgenie"
Expand Down Expand Up @@ -196,6 +197,7 @@ func TestBuildReceiverConfiguration(t *testing.T) {
require.Len(t, parsed.KafkaConfigs, 1)
require.Len(t, parsed.LineConfigs, 1)
require.Len(t, parsed.OpsgenieConfigs, 1)
require.Len(t, parsed.JsmConfigs, 1)
require.Len(t, parsed.PagerdutyConfigs, 1)
require.Len(t, parsed.PushoverConfigs, 1)
require.Len(t, parsed.SensugoConfigs, 1)
Expand All @@ -218,6 +220,7 @@ func TestBuildReceiverConfiguration(t *testing.T) {
all = append(all, getMetadata(parsed.KafkaConfigs)...)
all = append(all, getMetadata(parsed.LineConfigs)...)
all = append(all, getMetadata(parsed.OpsgenieConfigs)...)
all = append(all, getMetadata(parsed.JsmConfigs)...)
all = append(all, getMetadata(parsed.PagerdutyConfigs)...)
all = append(all, getMetadata(parsed.PushoverConfigs)...)
all = append(all, getMetadata(parsed.SensugoConfigs)...)
Expand Down Expand Up @@ -263,6 +266,7 @@ func TestBuildReceiverConfiguration(t *testing.T) {
require.Len(t, parsed.KafkaConfigs, 1)
require.Len(t, parsed.LineConfigs, 1)
require.Len(t, parsed.OpsgenieConfigs, 1)
require.Len(t, parsed.JsmConfigs, 1)
require.Len(t, parsed.PagerdutyConfigs, 1)
require.Len(t, parsed.PushoverConfigs, 1)
require.Len(t, parsed.SensugoConfigs, 1)
Expand Down Expand Up @@ -316,6 +320,10 @@ var allKnownConfigs = map[string]notifierConfigTest{
config: opsgenie.FullValidConfigForTesting,
secrets: opsgenie.FullValidSecretsForTesting,
},
"jsm": {notifierType: "jsm",
config: jsm.FullValidConfigForTesting,
secrets: jsm.FullValidSecretsForTesting,
},
"pagerduty": {notifierType: "pagerduty",
config: pagerduty.FullValidConfigForTesting,
secrets: pagerduty.FullValidSecretsForTesting,
Expand Down
86 changes: 86 additions & 0 deletions receivers/jsm/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package jsm

import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/grafana/alerting/receivers"
"github.com/grafana/alerting/templates"
)

const (
SendTags = "tags"
SendDetails = "details"
SendBoth = "both"

DefaultAlertsURL = "https://api.atlassian.com/jsm/ops/integration/v2/alerts"
)

type Config struct {
APIKey string
APIUrl string
Message string
Description string
AutoClose bool
OverridePriority bool
SendTagsAs string
}

func NewConfig(jsonData json.RawMessage, decryptFn receivers.DecryptFunc) (Config, error) {
type rawSettings struct {
APIKey string `json:"apiKey,omitempty" yaml:"apiKey,omitempty"`
APIUrl string `json:"apiUrl,omitempty" yaml:"apiUrl,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
AutoClose *bool `json:"autoClose,omitempty" yaml:"autoClose,omitempty"`
OverridePriority *bool `json:"overridePriority,omitempty" yaml:"overridePriority,omitempty"`
SendTagsAs string `json:"sendTagsAs,omitempty" yaml:"sendTagsAs,omitempty"`
}

raw := rawSettings{}
err := json.Unmarshal(jsonData, &raw)
if err != nil {
return Config{}, fmt.Errorf("failed to unmarshal settings: %w", err)
}

raw.APIKey = decryptFn("apiKey", raw.APIKey)
if raw.APIKey == "" {
return Config{}, errors.New("could not find api key property in settings")
}
if raw.APIUrl == "" {
raw.APIUrl = DefaultAlertsURL
}

if strings.TrimSpace(raw.Message) == "" {
raw.Message = templates.DefaultMessageTitleEmbed
}

switch raw.SendTagsAs {
case SendTags, SendDetails, SendBoth:
case "":
raw.SendTagsAs = SendTags
default:
return Config{}, fmt.Errorf("invalid value for sendTagsAs: %q", raw.SendTagsAs)
}

if raw.AutoClose == nil {
autoClose := true
raw.AutoClose = &autoClose
}
if raw.OverridePriority == nil {
overridePriority := true
raw.OverridePriority = &overridePriority
}

return Config{
APIKey: raw.APIKey,
APIUrl: raw.APIUrl,
Message: raw.Message,
Description: raw.Description,
AutoClose: *raw.AutoClose,
OverridePriority: *raw.OverridePriority,
SendTagsAs: raw.SendTagsAs,
}, nil
}
Loading