Skip to content

Commit b49dfc0

Browse files
Merge pull request #4362 from bl1nk/push-zpxrtwlxttso
Pushover: Add monospace message formatting, add documentation for existing HTML styling
2 parents e060127 + 90f24e0 commit b49dfc0

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

config/notifiers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ type PushoverConfig struct {
741741
Retry duration `yaml:"retry,omitempty" json:"retry,omitempty"`
742742
Expire duration `yaml:"expire,omitempty" json:"expire,omitempty"`
743743
TTL duration `yaml:"ttl,omitempty" json:"ttl,omitempty"`
744-
HTML bool `yaml:"html" json:"html,omitempty"`
744+
HTML bool `yaml:"html,omitempty" json:"html,omitempty"`
745+
Monospace bool `yaml:"monospace,omitempty" json:"monospace,omitempty"`
745746
}
746747

747748
// UnmarshalYAML implements the yaml.Unmarshaler interface.
@@ -763,6 +764,9 @@ func (c *PushoverConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
763764
if c.Token != "" && c.TokenFile != "" {
764765
return errors.New("at most one of token & token_file must be configured")
765766
}
767+
if c.HTML && c.Monospace {
768+
return errors.New("at most one of monospace & html must be configured")
769+
}
766770
return nil
767771
}
768772

config/notifiers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,26 @@ user_key: 'user key'
516516
}
517517
}
518518

519+
func TestPushoverHTMLOrMonospace(t *testing.T) {
520+
in := `
521+
token: 'pushover token'
522+
user_key: 'user key'
523+
html: true
524+
monospace: true
525+
`
526+
var cfg PushoverConfig
527+
err := yaml.UnmarshalStrict([]byte(in), &cfg)
528+
529+
expected := "at most one of monospace & html must be configured"
530+
531+
if err == nil {
532+
t.Fatalf("no error returned, expected:\n%v", expected)
533+
}
534+
if err.Error() != expected {
535+
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error())
536+
}
537+
}
538+
519539
func TestLoadSlackConfiguration(t *testing.T) {
520540
tests := []struct {
521541
in string

docs/configuration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,11 @@ token_file: <filepath>
13301330
# Optional time to live (TTL) to use for notification, see https://pushover.net/api#ttl
13311331
[ ttl: <duration> ]
13321332
1333+
# Optional HTML/monospace formatting for the message, see https://pushover.net/api#html
1334+
# html and monospace formatting are mutually exclusive.
1335+
[ html: <boolean> | default = false ]
1336+
[ monospace: <boolean> | default = false ]
1337+
13331338
# The HTTP client's configuration.
13341339
[ http_config: <http_config> | default = global.http_config ]
13351340
```

notify/pushover/pushover.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
124124
message = tmpl(n.conf.Message)
125125
}
126126

127+
if n.conf.Monospace {
128+
parameters.Add("monospace", "1")
129+
}
130+
127131
message, truncated = notify.TruncateInRunes(message, maxMessageLenRunes)
128132
if truncated {
129133
n.logger.Warn("Truncated message", "incident", key, "max_runes", maxMessageLenRunes)

notify/pushover/pushover_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package pushover
1515

1616
import (
17+
"net/http"
1718
"os"
1819
"testing"
1920

@@ -22,7 +23,9 @@ import (
2223
"github.com/stretchr/testify/require"
2324

2425
"github.com/prometheus/alertmanager/config"
26+
"github.com/prometheus/alertmanager/notify"
2527
"github.com/prometheus/alertmanager/notify/test"
28+
"github.com/prometheus/alertmanager/types"
2629
)
2730

2831
func TestPushoverRetry(t *testing.T) {
@@ -109,3 +112,27 @@ func TestPushoverReadingTokenFromFile(t *testing.T) {
109112

110113
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, token)
111114
}
115+
116+
func TestPushoverMonospaceParameter(t *testing.T) {
117+
ctx, apiURL, fn := test.GetContextWithCancelingURL(func(w http.ResponseWriter, r *http.Request) {
118+
require.NoError(t, r.ParseForm())
119+
require.Equal(t, "1", r.FormValue("monospace"), `expected monospace parameter to be set to "1"`)
120+
})
121+
defer fn()
122+
123+
notifier, err := New(
124+
&config.PushoverConfig{
125+
UserKey: config.Secret("user_key"),
126+
Token: config.Secret("token"),
127+
Monospace: true,
128+
HTTPConfig: &commoncfg.HTTPClientConfig{},
129+
},
130+
test.CreateTmpl(t),
131+
promslog.NewNopLogger(),
132+
)
133+
notifier.apiURL = apiURL.String()
134+
require.NoError(t, err)
135+
136+
_, err = notifier.Notify(notify.WithGroupKey(ctx, "1"), &types.Alert{})
137+
require.NoError(t, err)
138+
}

0 commit comments

Comments
 (0)