Skip to content

Commit be564db

Browse files
tecnologernikoksr
andauthored
feat(service): PagerDuty service integration (#933)
Co-authored-by: Niko Köser <koeserniko@gmail.com>
1 parent 1df6960 commit be564db

File tree

9 files changed

+652
-0
lines changed

9 files changed

+652
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Yes, please! Contributions of all kinds are very welcome! Feel free to check our
9595
| [Mailgun](https://www.mailgun.com) | [service/mailgun](service/mailgun) | [mailgun/mailgun-go](https://github.com/mailgun/mailgun-go) | :heavy_check_mark: |
9696
| [Matrix](https://www.matrix.org) | [service/matrix](service/matrix) | [mautrix/go](https://github.com/mautrix/go) | :heavy_check_mark: |
9797
| [Microsoft Teams](https://www.microsoft.com/microsoft-teams) | [service/msteams](service/msteams) | [atc0005/go-teams-notify](https://github.com/atc0005/go-teams-notify) | :heavy_check_mark: |
98+
| [PagerDuty](https://www.pagerduty.com) | [service/pagerduty](service/pagerduty) | [PagerDuty/go-pagerduty](https://github.com/PagerDuty/go-pagerduty) | :heavy_check_mark: |
9899
| [Plivo](https://www.plivo.com) | [service/plivo](service/plivo) | [plivo/plivo-go](https://github.com/plivo/plivo-go) | :heavy_check_mark: |
99100
| [Pushover](https://pushover.net/) | [service/pushover](service/pushover) | [gregdel/pushover](https://github.com/gregdel/pushover) | :heavy_check_mark: |
100101
| [Pushbullet](https://www.pushbullet.com) | [service/pushbullet](service/pushbullet) | [cschomburg/go-pushbullet](https://github.com/cschomburg/go-pushbullet) | :heavy_check_mark: |

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
6464
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
6565
github.com/MicahParks/keyfunc v1.9.0 // indirect
66+
github.com/PagerDuty/go-pagerduty v1.8.0 // indirect
6667
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
6768
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
6869
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
4040
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
4141
github.com/MicahParks/keyfunc v1.9.0 h1:lhKd5xrFHLNOWrDc4Tyb/Q1AJ4LCzQ48GVJyVIID3+o=
4242
github.com/MicahParks/keyfunc v1.9.0/go.mod h1:IdnCilugA0O/99dW+/MkvlyrsX8+L8+x95xuVNtM5jw=
43+
github.com/PagerDuty/go-pagerduty v1.8.0 h1:MTFqTffIcAervB83U7Bx6HERzLbyaSPL/+oxH3zyluI=
44+
github.com/PagerDuty/go-pagerduty v1.8.0/go.mod h1:nzIeAqyFSJAFkjWKvMzug0JtwDg+V+UoCWjFrfFH5mI=
4345
github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20240116134246-a8cbe886bab0 h1:ztLQGVQsey3BjCoh0TvHc/iKTQmkio2OmsIxhuu+EeY=
4446
github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20240116134246-a8cbe886bab0/go.mod h1:rjP7sIipbZcagro/6TCk6X0ZeFT2eyudH5+fve/cbBA=
4547
github.com/SherClockHolmes/webpush-go v1.4.0 h1:ocnzNKWN23T9nvHi6IfyrQjkIc0oJWv1B1pULsf9i3s=

service/pagerduty/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# PagerDuty Notifications
2+
3+
## Prerequisites
4+
5+
Ensure you have a valid PagerDuty API token to authenticate requests.
6+
7+
### Compatibility
8+
9+
This service is compatible with the PagerDuty API for creating incidents.
10+
11+
## Usage
12+
```go
13+
package main
14+
15+
import (
16+
"context"
17+
"log"
18+
19+
"github.com/nikoksr/notify"
20+
"github.com/nikoksr/notify/service/pagerduty"
21+
)
22+
23+
func main() {
24+
// Create a new PagerDuty service. Replace 'your_pagerduty_api_token' with your actual PagerDuty API token.
25+
pagerDutyService, err := pagerduty.New("your_pagerduty_api_token")
26+
if err != nil {
27+
log.Fatalf("failed to create pagerduty service: %s", err)
28+
}
29+
30+
// Set the sender address and add receivers. (required)
31+
pagerDutyService.SetFromAddress("sender@example.com")
32+
pagerDutyService.AddReceivers("ServiceDirectory1", "ServiceDirectory2")
33+
34+
// Set the urgency, priority ID, and notification type. (optional)
35+
pagerDutyService.SetUrgency("high")
36+
pagerDutyService.SetPriorityID("P123456")
37+
pagerDutyService.SetNotificationType("incident")
38+
39+
// Create a notifier instance and register the PagerDuty service to it.
40+
notifier := notify.New()
41+
notifier.UseServices(pagerDutyService)
42+
43+
// Send a notification.
44+
err = notifier.Send(context.Background(), "Test Alert", "This is a test alert from PagerDuty service.")
45+
if err != nil {
46+
log.Fatalf("failed to send notification: %s", err)
47+
}
48+
49+
log.Println("Notification sent successfully")
50+
}
51+
```
52+
53+
## Configuration
54+
55+
### Required Properties
56+
- **API Token**: Your PagerDuty API token.
57+
- **From Address**: The email address of the sender. The author of the incident.
58+
- **Receivers**: List of PagerDuty service directories to send the incident to.
59+
60+
### Optional Properties
61+
- **Urgency**: The urgency of the incident (e.g., "high", "low").
62+
- **PriorityID**: The ID of the priority level assigned to the incident.
63+
- **NotificationType**: Type of notification (default is "incident").
64+
65+
These properties can be set using the respective setter methods provided by the `Config` struct:
66+
- `SetFromAddress(string)`
67+
- `AddReceivers(...string)`
68+
- `SetUrgency(string)`
69+
- `SetPriorityID(string)`
70+
- `SetNotificationType(string)`

service/pagerduty/config.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package pagerduty
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/mail"
7+
8+
"github.com/PagerDuty/go-pagerduty"
9+
)
10+
11+
const (
12+
APIReferenceType = "service_reference"
13+
APIPriorityReference = "priority_reference"
14+
DefaultNotificationType = "incident"
15+
)
16+
17+
// Config contains the configuration for the PagerDuty service.
18+
type Config struct {
19+
FromAddress string
20+
Receivers []string
21+
NotificationType string
22+
Urgency string
23+
PriorityID string
24+
}
25+
26+
func NewConfig() *Config {
27+
return &Config{
28+
NotificationType: DefaultNotificationType,
29+
Receivers: make([]string, 0, 1),
30+
}
31+
}
32+
33+
// OK checks if the configuration is valid.
34+
// It returns an error if the configuration is invalid.
35+
func (c *Config) OK() error {
36+
if c.FromAddress == "" {
37+
return errors.New("from address is required")
38+
}
39+
40+
_, err := mail.ParseAddress(c.FromAddress)
41+
if err != nil {
42+
return fmt.Errorf("from address is invalid: %w", err)
43+
}
44+
45+
if len(c.Receivers) == 0 {
46+
return errors.New("at least one receiver is required")
47+
}
48+
49+
if c.NotificationType == "" {
50+
return errors.New("notification type is required")
51+
}
52+
53+
return nil
54+
}
55+
56+
// PriorityReference returns the PriorityID reference if it is set, otherwise it returns nil.
57+
func (c *Config) PriorityReference() *pagerduty.APIReference {
58+
if c.PriorityID == "" {
59+
return nil
60+
}
61+
62+
return &pagerduty.APIReference{
63+
ID: c.PriorityID,
64+
Type: APIPriorityReference,
65+
}
66+
}
67+
68+
// SetFromAddress sets the from address in the configuration.
69+
func (c *Config) SetFromAddress(fromAddress string) {
70+
c.FromAddress = fromAddress
71+
}
72+
73+
// AddReceivers appends the receivers to the configuration.
74+
func (c *Config) AddReceivers(receivers ...string) {
75+
c.Receivers = append(c.Receivers, receivers...)
76+
}
77+
78+
// SetPriorityID sets the PriorityID in the configuration.
79+
func (c *Config) SetPriorityID(priorityID string) {
80+
c.PriorityID = priorityID
81+
}
82+
83+
// SetUrgency sets the urgency in the configuration.
84+
func (c *Config) SetUrgency(urgency string) {
85+
c.Urgency = urgency
86+
}
87+
88+
// SetNotificationType sets the notification type in the configuration.
89+
// If the notification type is empty, it will be set to the default value "incident".
90+
func (c *Config) SetNotificationType(notificationType string) {
91+
if notificationType == "" {
92+
notificationType = DefaultNotificationType
93+
}
94+
95+
c.NotificationType = notificationType
96+
}

0 commit comments

Comments
 (0)