Debian Codegraph @

pkg/services/incident/pagerduty/pagerduty.go

raw symbols SCIP
1 package pagerdutyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src . bytes/"
5 "contextscip-go gomod github.com/golang/go/src . context/"
6 "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/"
7 "fmtscip-go gomod github.com/golang/go/src . fmt/"
8 "ioscip-go gomod github.com/golang/go/src . io/"
9 "net/httpscip-go gomod github.com/golang/go/src . `net/http`/"
10 "net/urlscip-go gomod github.com/golang/go/src . `net/url`/"
11 "stringsscip-go gomod github.com/golang/go/src . strings/"
12 "timescip-go gomod github.com/golang/go/src . time/"
13
14 "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/"
15 "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/"
16 "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/"
17 )
18
19 // Service provides PagerDuty as a notification service.
20 type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service# struct {
21 standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.Standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Standard.
22
23 Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#
24 pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#pkr. formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#
25 httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#
26 }
27
28 const (
29 eventEndpointTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/eventEndpointTemplate. = "https://%s:%d/v2/enqueue"
30 defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/defaultHTTPTimeout. = 30 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. // defaultHTTPTimeout is the default timeout for HTTP requests.
31 maxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/maxMessageLength. = 1024 // maxMessageLength is the maximum permitted length of the summary property.
32
33 contextTypeLinkscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeLink. = "link"
34 contextTypeImagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeImage. = "image"
35 )
36
37 // GetID returns the service identifier.
38 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#GetID().() string {
39 return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Scheme.
40 }
41
42 // Initialize loads ServiceConfig from serviceURL and sets logger for this Service.
43 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Initialize().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) error {
44 s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger)
45 s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#{}
46 s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#pkr. = formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config.)
47
48 if err := s.setDefaultsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#setDefaults().(); err != nil {
49 return err
50 }
51
52 if err := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config..setURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#setURL().(&s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#pkr., serviceURL); err != nil {
53 return err
54 }
55
56 if s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. == nil {
57 // Initialize HTTP client with timeout
58 s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{
59 Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/defaultHTTPTimeout.,
60 }
61 }
62
63 return nil
64 }
65
66 // Send a notification message to PagerDuty
67 // See: https://developer.pagerduty.com/docs/events-api-v2-overview
68 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Send().(message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) error {
69 return s.SendWithContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#SendWithContext().(contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), message, params)
70 }
71
72 // SendWithContext sends a notification message to PagerDuty with context support
73 // See: https://developer.pagerduty.com/docs/events-api-v2-overview
74 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) SendWithContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#SendWithContext().(
75 ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#,
76 message string,
77 params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#,
78 ) error {
79 config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config.
80 endpointURL := fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().(eventEndpointTemplatescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/eventEndpointTemplate., config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Host., config.Portscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Port.)
81
82 payload, err := s.newEventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#newEventPayload().(message, params)
83 if err != nil {
84 return err
85 }
86
87 return s.sendAlertscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#sendAlert().(ctx, endpointURL, &payload)
88 }
89
90 // SetHTTPClient allows users to provide a custom HTTP client for enterprise environments
91 // requiring proxies, custom TLS configurations, etc.
92 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#SetHTTPClient().(client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#) {
93 if client == nil {
94 s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. = &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/defaultHTTPTimeout.}
95
96 return
97 }
98
99 s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. = client
100 }
101
102 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) newEventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#newEventPayload().(
103 message string,
104 params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#,
105 ) (EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#, error) {
106 if params == nil {
107 params = &typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#{}
108 }
109
110 // Defensive copy
111 payloadFields := *s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config.
112
113 if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(&payloadFields, params); err != nil {
114 return EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{}, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to update config from params: %w", err)
115 }
116
117 // Validate severity
118 if err := validateSeverityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/validateSeverity().(payloadFields.Severityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Severity.); err != nil {
119 return EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{}, err
120 }
121
122 // Validate event action
123 if err := validateEventActionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/validateEventAction().(payloadFields.Actionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Action.); err != nil {
124 return EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{}, err
125 }
126
127 // The maximum permitted length of this property is 1024 characters.
128 runes := []rune(message)
129 if len(runes) > maxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/maxMessageLength. {
130 message = string(runes[:maxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/maxMessageLength.])
131 }
132
133 result := EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{
134 Payloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#Payload.: Payloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Payload#{
135 Summaryscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Payload#Summary.: message,
136 Severityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Payload#Severity.: payloadFields.Severityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Severity.,
137 Sourcescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Payload#Source.: payloadFields.Sourcescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Source.,
138 },
139 RoutingKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#RoutingKey.: payloadFields.IntegrationKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#IntegrationKey.,
140 EventActionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#EventAction.: payloadFields.Actionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Action.,
141 }
142
143 // Add optional dedup_key if provided
144 if payloadFields.DedupKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#DedupKey. != "" {
145 result.DedupKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#DedupKey. = payloadFields.DedupKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#DedupKey.
146 }
147
148 // Add optional fields if provided
149 if payloadFields.Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Details. != "" {
150 var details any
151 if err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().([]byte(payloadFields.Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Details.), &details); err != nil {
152 return EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{}, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().(
153 "failed to unmarshal details %q: %w",
154 payloadFields.Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Details.,
155 err,
156 )
157 }
158
159 result.Detailsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#Details. = details
160 }
161
162 if payloadFields.Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Client. != "" {
163 result.Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#Client. = payloadFields.Clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Client.
164 }
165
166 if payloadFields.ClientURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#ClientURL. != "" {
167 result.ClientURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#ClientURL. = payloadFields.ClientURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#ClientURL.
168 }
169
170 if payloadFields.Contextsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Contexts. != "" {
171 contexts, err := parseContextsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/parseContexts().(payloadFields.Contextsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Config#Contexts.)
172 if err != nil {
173 return EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#{}, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to parse contexts: %w", err)
174 }
175
176 result.Contextsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#Contexts. = contexts
177 }
178
179 return result, nil
180 }
181
182 // sendAlert sends an alert payload to the specified PagerDuty endpoint URL.
183 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) sendAlertscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#sendAlert().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, endpoint string, payload *EventPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/EventPayload#) error {
184 // Marshal the payload into JSON format
185 jsonBody, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(payload)
186 if err != nil {
187 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to marshal payload: %w", err)
188 }
189
190 jsonBuffer := bytesscip-go gomod github.com/golang/go/src . bytes/.NewBufferscip-go gomod github.com/golang/go/src . bytes/NewBuffer().(jsonBody)
191
192 // Create a new HTTP POST request with the JSON body
193 req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(
194 ctx,
195 httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost.,
196 endpoint,
197 jsonBuffer,
198 )
199 if err != nil {
200 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to create HTTP request: %w", err)
201 }
202
203 // Set the Content-Type header to application/json
204 req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Addscip-go gomod github.com/golang/go/src . `net/http`/Header#Add().("Content-Type", "application/json")
205
206 // Use the custom HTTP client
207 if s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient. == nil {
208 return errServiceNotInitializedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errServiceNotInitialized.
209 }
210
211 // Send the HTTP request to PagerDuty
212 resp, err := s.httpClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#httpClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#Do.(req)
213 if err != nil {
214 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send notification to PagerDuty: %w", err)
215 }
216
217 defer func() { _ = resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }()
218
219 // Check if the response status indicates success (2xx)
220 if resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. < 200 || resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. >= 300 {
221 // Parse error response body for better error reporting
222 errorMsg := fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().("HTTP %d", resp.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode.)
223 if resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body. != nil {
224 bodyBytes, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(resp.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.)
225 if err == nil && len(bodyBytes) > 0 {
226 // Try to parse as PagerDuty error response
227 var errorResponse struct {
228 Status string `json:"status"`
229 Message string `json:"message"`
230 Error string `json:"error"`
231 Errors []string `json:"errors"`
232 }
233 if jsonErr := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().(bodyBytes, &errorResponse); jsonErr == nil {
234 switch {
235 case errorResponse.Message != "":
236 errorMsg = errorResponse.Message
237 case errorResponse.Error != "":
238 errorMsg = errorResponse.Error
239 case len(errorResponse.Errors) > 0:
240 errorMsg = stringsscip-go gomod github.com/golang/go/src . strings/.Joinscip-go gomod github.com/golang/go/src . strings/Join().(errorResponse.Errors, "; ")
241 }
242 } else {
243 // Fallback to raw body if JSON parsing fails
244 errorMsg = string(bodyBytes)
245 }
246 }
247 }
248
249 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %s", errPagerDutyNotificationFailedscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errPagerDutyNotificationFailed., errorMsg)
250 }
251
252 return nil
253 }
254
255 func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#) setDefaultsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#setDefaults().() error {
256 if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#pkr..SetDefaultPropsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#SetDefaultProps().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/Service#Config.); err != nil {
257 return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to set default props: %w", err)
258 }
259
260 return nil
261 }
262
263 // parseContexts parses contexts from either a JSON array format or legacy comma-separated string format.
264 // It first attempts to unmarshal the input as a JSON array of PagerDutyContext objects.
265 // If JSON unmarshaling fails, it falls back to parsing the legacy string format like "type:src,type2:src2".
266 // Legacy format supports:
267 // - "link:http://example.com" -> {Type: "link", Href: "http://example.com"}
268 // - "image:http://example.com/img.png" -> {Type: "image", Src: "http://example.com/img.png"}.
269 func parseContextsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/parseContexts().(contextsStr string) ([]PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#, error) {
270 if contextsStr == "" {
271 return nil, nil
272 }
273
274 // First, attempt to parse as JSON array
275 var result []PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#
276 if err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src . `encoding/json`/Unmarshal().([]byte(contextsStr), &result); err == nil {
277 // Validate Type field and required fields for each context in JSON format
278 for _, ctx := range result {
279 if ctx.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type. != contextTypeLinkscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeLink. && ctx.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type. != contextTypeImagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeImage. {
280 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: found %q", errInvalidContextTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errInvalidContextType., ctx.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type.)
281 }
282
283 if ctx.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type. == contextTypeLinkscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeLink. && ctx.Hrefscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Href. == "" {
284 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %+v", errMissingHrefForLinkContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errMissingHrefForLinkContext., ctx)
285 }
286
287 if ctx.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type. == contextTypeImagescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/contextTypeImage. && ctx.Srcscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Src. == "" {
288 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %+v", errMissingSrcForImageContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errMissingSrcForImageContext., ctx)
289 }
290 }
291
292 return result, nil
293 }
294
295 // Fall back to legacy comma-separated parsing
296 // Split the input string by commas to get individual context entries
297 contexts := stringsscip-go gomod github.com/golang/go/src . strings/.Splitscip-go gomod github.com/golang/go/src . strings/Split().(contextsStr, ",")
298 result = make([]PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#, 0, len(contexts))
299
300 for _, ctx := range contexts {
301 // Trim whitespace from each context entry
302 ctx = stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(ctx)
303 if ctx == "" {
304 continue
305 }
306
307 const expectedParts = 2
308
309 // Split each context by colon to separate type and value, limiting to 2 parts
310 parts := stringsscip-go gomod github.com/golang/go/src . strings/.SplitNscip-go gomod github.com/golang/go/src . strings/SplitN().(ctx, ":", expectedParts)
311 if len(parts) != expectedParts {
312 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", errInvalidContextFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errInvalidContextFormat., ctx)
313 }
314
315 // Trim whitespace from type and value parts
316 contextType := stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(parts[0])
317 value := stringsscip-go gomod github.com/golang/go/src . strings/.TrimSpacescip-go gomod github.com/golang/go/src . strings/TrimSpace().(parts[1])
318
319 // Validate that neither type nor value is empty after trimming
320 if contextType == "" || value == "" {
321 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", errEmptyContextTypeOrValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errEmptyContextTypeOrValue., ctx)
322 }
323
324 var ctxVar PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#
325
326 // Map context types to appropriate PagerDutyContext fields
327 switch contextType {
328 case "link":
329 // Create a link context with href
330 //nolint:exhaustruct_v5 // link type only needs Type+Href
331 ctxVar = PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#{Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type.: "link", Hrefscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Href.: value}
332 case "image":
333 // Create an image context with src
334 //nolint:exhaustruct_v5 // image type only needs Type+Src
335 ctxVar = PagerDutyContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#{Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Type.: "image", Srcscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/PagerDutyContext#Src.: value}
336 case "text":
337 // Skip text contexts
338 continue
339 default:
340 return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().(
341 "%w: unsupported context type %q",
342 errInvalidContextFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errInvalidContextFormat.,
343 contextType,
344 )
345 }
346
347 // Add the parsed context to the result slice
348 result = append(result, ctxVar)
349 }
350
351 return result, nil
352 }
353
354 // validateSeverity checks if the provided severity is one of the allowed values.
355 func validateSeverityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/validateSeverity().(severity string) error {
356 validSeverities := map[string]bool{
357 "critical": true,
358 "error": true,
359 "warning": true,
360 "info": true,
361 }
362
363 if !validSeverities[severity] {
364 return errInvalidSeverityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errInvalidSeverity.
365 }
366
367 return nil
368 }
369
370 // validateEventAction checks if the provided event action is one of the allowed values.
371 func validateEventActionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/validateEventAction().(action string) error {
372 validActions := map[string]bool{
373 "trigger": true,
374 "acknowledge": true,
375 "resolve": true,
376 }
377
378 if !validActions[action] {
379 return errInvalidEventActionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/pagerduty`/errInvalidEventAction.
380 }
381
382 return nil
383 }