@@ -17,6 +17,7 @@ import (
17
17
"bytes"
18
18
"context"
19
19
"encoding/json"
20
+ "errors"
20
21
"fmt"
21
22
"io"
22
23
"log/slog"
@@ -43,29 +44,36 @@ type Notifier struct {
43
44
44
45
// New returns a new incident.io notifier.
45
46
func New (conf * config.IncidentioConfig , t * template.Template , l * slog.Logger , httpOpts ... commoncfg.HTTPClientOption ) (* Notifier , error ) {
46
- // If alert source token is specified, set authorization in HTTP config
47
+ // Handle authentication configuration
47
48
if conf .HTTPConfig == nil {
48
49
conf .HTTPConfig = & commoncfg.HTTPClientConfig {}
49
50
}
50
51
52
+ // Ensure one of AlertSourceToken or AlertSourceTokenFile is provided
53
+ if conf .AlertSourceToken == "" && conf .AlertSourceTokenFile == "" {
54
+ return nil , errors .New ("one of alert_source_token or alert_source_token_file must be configured" )
55
+ }
56
+
57
+ // Error if authorization is already set in HTTPConfig
58
+ if conf .HTTPConfig .Authorization != nil {
59
+ return nil , errors .New ("cannot specify both alert_source_token/alert_source_token_file and http_config.authorization" )
60
+ }
61
+
62
+ // Set authorization from token or token file
51
63
if conf .AlertSourceToken != "" {
52
- if conf .HTTPConfig .Authorization == nil {
53
- conf .HTTPConfig .Authorization = & commoncfg.Authorization {
54
- Type : "Bearer" ,
55
- Credentials : commoncfg .Secret (conf .AlertSourceToken ),
56
- }
64
+ conf .HTTPConfig .Authorization = & commoncfg.Authorization {
65
+ Type : "Bearer" ,
66
+ Credentials : commoncfg .Secret (conf .AlertSourceToken ),
57
67
}
58
68
} else if conf .AlertSourceTokenFile != "" {
59
69
content , err := os .ReadFile (conf .AlertSourceTokenFile )
60
70
if err != nil {
61
71
return nil , fmt .Errorf ("failed to read alert_source_token_file: %w" , err )
62
72
}
63
73
64
- if conf .HTTPConfig .Authorization == nil {
65
- conf .HTTPConfig .Authorization = & commoncfg.Authorization {
66
- Type : "Bearer" ,
67
- Credentials : commoncfg .Secret (strings .TrimSpace (string (content ))),
68
- }
74
+ conf .HTTPConfig .Authorization = & commoncfg.Authorization {
75
+ Type : "Bearer" ,
76
+ Credentials : commoncfg .Secret (strings .TrimSpace (string (content ))),
69
77
}
70
78
}
71
79
@@ -83,10 +91,6 @@ func New(conf *config.IncidentioConfig, t *template.Template, l *slog.Logger, ht
83
91
retrier : & notify.Retrier {
84
92
RetryCodes : []int {
85
93
http .StatusTooManyRequests , // 429
86
- http .StatusInternalServerError ,
87
- http .StatusBadGateway ,
88
- http .StatusServiceUnavailable ,
89
- http .StatusGatewayTimeout ,
90
94
},
91
95
CustomDetailsFunc : errDetails ,
92
96
},
@@ -124,7 +128,7 @@ func (n *Notifier) Notify(ctx context.Context, alerts ...*types.Alert) (bool, er
124
128
n .logger .Debug ("incident.io notification" , "groupKey" , groupKey )
125
129
126
130
msg := & Message {
127
- Version : "4 " ,
131
+ Version : "1 " ,
128
132
Data : data ,
129
133
GroupKey : groupKey .String (),
130
134
TruncatedAlerts : numTruncated ,
@@ -197,8 +201,5 @@ func errDetails(status int, body io.Reader) string {
197
201
parts = append (parts , strings .Join (errorResponse .Errors , ", " ))
198
202
}
199
203
200
- if len (parts ) > 0 {
201
- return strings .Join (parts , ": " )
202
- }
203
- return ""
204
+ return strings .Join (parts , ": " )
204
205
}
0 commit comments