Skip to content
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
cloud.google.com/go/monitoring v1.21.2 // indirect
cloud.google.com/go/storage v1.47.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/AnthonyHewins/gotfy v0.0.10 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
Expand All @@ -75,6 +76,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
firebase.google.com/go/v4 v4.15.1 h1:tR2dzKw1MIfCfG2bhAyxa5KQ57zcE7iFKmeYClET6ZM=
firebase.google.com/go/v4 v4.15.1/go.mod h1:eunxbsh4UXI2rA8po3sOiebvWYuW0DVxAdZFO0I6wdY=
github.com/AnthonyHewins/gotfy v0.0.10 h1:23ZjRVG7wuGuqn7CQq/bOrkXa3gg2XyYrrD3RYEmvE8=
github.com/AnthonyHewins/gotfy v0.0.10/go.mod h1:q2orErDDpl9/gZ5L4oJhejb7TaP/eBdtkzjWDruNRlg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 h1:3c8yed4lgqTt+oTQ+JNMDo+F4xprBf+O/il4ZC0nRLw=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0/go.mod h1:obipzmGjfSjam60XLwGfqUkJsfiheAl+TUjG+4yzyPM=
Expand Down Expand Up @@ -165,6 +167,8 @@ github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaEL
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
Expand Down
1 change: 0 additions & 1 deletion service/msteams/mock_teams_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions service/ntfy/ntfy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package ntfy

import (
"context"
"net/http"
"net/url"

"github.com/AnthonyHewins/gotfy"
"github.com/pkg/errors"
)

// Service allow you to configure Ntfy service.
type Service struct {
message *gotfy.Message
publishers []gotfy.Publisher
}

// DefaultServerURL is the default server to use for the Ntfy service.
const defaultServerURL = "https://ntfy.sh"

// AddReceivers adds server URLs to the list of servers to use for sending messages.
func (s *Service) AddPublishers(publishers ...gotfy.Publisher) {
s.publishers = append(s.publishers, publishers...)
}

// AddMessage adds a message to be used when sending.
func (s *Service) AddMessage(m gotfy.Message) {
s.message = &m
}

// NewWithPublishers returns a new instance of Ntfy service. You can use this service to send messages to Ntfy. You can
// specify the publishers to send the messages to. By default, the service will use the default server
// (https://ntfy.sh/) if you don't specify any servers.
func NewWithPublishers(publishers ...gotfy.Publisher) (*Service, error) {
s := &Service{}

if publishers == nil {
serverURL, err := url.Parse(defaultServerURL)
if err != nil {
return nil, err
}
pub, err := gotfy.NewPublisher(serverURL, http.DefaultClient)
if err != nil {
return nil, err
}
publishers = append(publishers, *pub)
}

s.AddPublishers(publishers...)

return s, nil
}

// New returns a new instance of Ntfy service. You can use this service to send messages to Ntfy. By default, the
// service will use the default server (https://ntfy.sh).
func New(hostName string, httpClient *http.Client, message *gotfy.Message) (*Service, error) {
if hostName == "" {
hostName = defaultServerURL
}
if httpClient == nil {
httpClient = http.DefaultClient
}

hostNameURL, err := url.Parse(hostName)
if err != nil {
return nil, err
}
gotfyPub, err := gotfy.NewPublisher(hostNameURL, httpClient)
if err != nil {
return nil, err
}

return &Service{
publishers: []gotfy.Publisher{*gotfyPub},
message: message,
}, nil
}

func (s *Service) send(ctx context.Context, publisher *gotfy.Publisher, title, content string) error {
if publisher == nil {
return errors.New("publisher is nil")
}

if s.message == nil {
return errors.New("message is nil. Topic needs to be set in the message")
}

if title != "" {
s.message.Title = title
}

if content != "" {
s.message.Message = content
}

_, err := publisher.SendMessage(ctx, s.message)
if err != nil {
return errors.Wrap(err, "sending message")
}

return nil
}

// Send takes a message subject and a message content and sends them to Ntfy application.
func (s *Service) Send(ctx context.Context, subject, content string) error {
if s.publishers == nil {
return errors.New("publishers are nil")
}

for _, publisher := range s.publishers {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := s.send(ctx, &publisher, subject, content)
if err != nil {
return errors.Wrapf(err, "failed to send message to Ntfy server %q", publisher.Headers.Get("Host"))
}
}
}

return nil
}
130 changes: 130 additions & 0 deletions service/ntfy/ntfy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package ntfy

import (
"context"
"net/http"
"net/url"
"testing"

"github.com/AnthonyHewins/gotfy"
"github.com/stretchr/testify/require"
)

func TestNtfy_New(t *testing.T) {
t.Parallel()

assert := require.New(t)

service, _ := New(defaultServerURL, nil, nil)
assert.NotNil(service)
}

func TestNtfy_NewWithPublishers(t *testing.T) {
t.Parallel()

assert := require.New(t)

service, _ := NewWithPublishers()
assert.NotNil(service)

pub1, _ := gotfy.NewPublisher(&url.URL{Host: "xyz.com"}, http.DefaultClient)
pub2, _ := gotfy.NewPublisher(&url.URL{Host: "ahc.com"}, http.DefaultClient)
service, _ = NewWithPublishers(*pub1, *pub2)
assert.NotNil(service)
}

func TestNtfy_AddPublishers(t *testing.T) {
t.Parallel()

assert := require.New(t)

service, _ := New(defaultServerURL, nil, nil)
assert.NotNil(service)

defaultServerU, _ := url.Parse(defaultServerURL)
urlXyz, _ := url.Parse("https://xyz.com")
urlAbc, _ := url.Parse("https://abc.com")

pub1, _ := gotfy.NewPublisher(urlXyz, http.DefaultClient)
pub2, _ := gotfy.NewPublisher(urlAbc, http.DefaultClient)
defaultServerPub, _ := gotfy.NewPublisher(defaultServerU, http.DefaultClient)
expected := []gotfy.Publisher{*defaultServerPub, *pub1, *pub2}

service.AddPublishers(*pub1, *pub2)

assert.Equal(expected, service.publishers)
}

func TestNtfy_send(t *testing.T) {
t.Parallel()

assert := require.New(t)

topic := "go-notify-test"
service, _ := New(defaultServerURL, nil, &gotfy.Message{
Topic: topic,
})
assert.NotNil(service)

ctx := context.Background()

remURL, _ := url.Parse("https://filesrv.lan/space.jpg")
clickURL, _ := url.Parse("https://homecamera.lan/xasds1h2xsSsa/")

content := gotfy.Message{
Message: "Disk space is low at 5.1 GB",
Title: "Low disk space alert",
Tags: []string{"warning", "cd"},
Priority: 4,
AttachURL: remURL,
AttachURLFilename: "diskspace.jpg",
ClickURL: clickURL,
}

defaultServer, _ := url.Parse(defaultServerURL)
pub, _ := gotfy.NewPublisher(defaultServer, http.DefaultClient)

service.AddMessage(content)
err := service.send(ctx, pub, topic, "Disk space is low at 5.1 GB")
assert.Error(err)

err = service.send(ctx, nil, topic, "Disk space is low at 5.1 GB")
assert.Error(err)

pub, _ = gotfy.NewPublisher(&url.URL{Host: "xyz.com"}, http.DefaultClient)
err = service.send(ctx, pub, topic, "sample_body")
assert.Error(err)

err = service.send(ctx, pub, topic, "Disk space is low at 5.1 GB")
assert.Error(err)
}

func TestNtfy_Send(t *testing.T) {
t.Parallel()

assert := require.New(t)

service, _ := New("", nil, &gotfy.Message{
Topic: "go-notify",
Message: "Disk space is low at 5.1 GB",
})
assert.NotNil(service)

ctx := context.Background()
topic := "go-notify"
title := "Low disk space alert"

service.AddMessage(gotfy.Message{
Topic: topic,
})

err := service.Send(ctx, title, "Disk space is low at 5.1 GB")
assert.NoError(err)

service.AddMessage(gotfy.Message{
Topic: "",
})

err = service.Send(ctx, title, "Disk space is low at 5.1 GB")
assert.Error(err)
}
6 changes: 5 additions & 1 deletion service/pagerduty/pager_duty.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
)

type Client interface {
CreateIncidentWithContext(ctx context.Context, from string, options *pagerduty.CreateIncidentOptions) (*pagerduty.Incident, error) //nolint:lll // acceptable in this case, alternative makes the interface even less readable
CreateIncidentWithContext(
ctx context.Context,
from string,
options *pagerduty.CreateIncidentOptions,
) (*pagerduty.Incident, error) //nolint:lll // acceptable in this case, alternative makes the interface even less readable
}

// Compile-time check to verify that the PagerDuty type implements the notifier.Notifier interface.
Expand Down
Loading