Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3b2c48b5e4
commit
c0c4addb85
285 changed files with 25880 additions and 0 deletions
87
pkg/services/googlechat/googlechat.go
Normal file
87
pkg/services/googlechat/googlechat.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package googlechat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// ErrUnexpectedStatus indicates an unexpected HTTP status code from the Google Chat API.
|
||||
var ErrUnexpectedStatus = errors.New("google chat api returned unexpected http status code")
|
||||
|
||||
// Service implements a Google Chat notification service.
|
||||
type Service struct {
|
||||
standard.Standard
|
||||
Config *Config
|
||||
}
|
||||
|
||||
// Initialize configures the service with a URL and logger.
|
||||
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
|
||||
service.SetLogger(logger)
|
||||
service.Config = &Config{}
|
||||
|
||||
return service.Config.SetURL(configURL)
|
||||
}
|
||||
|
||||
// GetID returns the identifier for this service.
|
||||
func (service *Service) GetID() string {
|
||||
return Scheme
|
||||
}
|
||||
|
||||
// Send delivers a notification message to Google Chat.
|
||||
func (service *Service) Send(message string, _ *types.Params) error {
|
||||
config := service.Config
|
||||
|
||||
jsonBody, err := json.Marshal(JSON{Text: message})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling message to JSON: %w", err)
|
||||
}
|
||||
|
||||
postURL := getAPIURL(config)
|
||||
jsonBuffer := bytes.NewBuffer(jsonBody)
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
context.Background(),
|
||||
http.MethodPost,
|
||||
postURL.String(),
|
||||
jsonBuffer,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating HTTP request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sending notification to Google Chat: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("%w: %d", ErrUnexpectedStatus, resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getAPIURL constructs the API URL for Google Chat notifications.
|
||||
func getAPIURL(config *Config) *url.URL {
|
||||
query := url.Values{}
|
||||
query.Set("key", config.Key)
|
||||
query.Set("token", config.Token)
|
||||
|
||||
return &url.URL{
|
||||
Path: config.Path,
|
||||
Host: config.Host,
|
||||
Scheme: "https",
|
||||
RawQuery: query.Encode(),
|
||||
}
|
||||
}
|
73
pkg/services/googlechat/googlechat_config.go
Normal file
73
pkg/services/googlechat/googlechat_config.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package googlechat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
Scheme = "googlechat"
|
||||
)
|
||||
|
||||
// Static error definitions.
|
||||
var (
|
||||
ErrMissingKey = errors.New("missing field 'key'")
|
||||
ErrMissingToken = errors.New("missing field 'token'")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
standard.EnumlessConfig
|
||||
Host string `default:"chat.googleapis.com"`
|
||||
Path string
|
||||
Token string
|
||||
Key string
|
||||
}
|
||||
|
||||
func (config *Config) GetURL() *url.URL {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.getURL(&resolver)
|
||||
}
|
||||
|
||||
func (config *Config) SetURL(url *url.URL) error {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.setURL(&resolver, url)
|
||||
}
|
||||
|
||||
func (config *Config) setURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error {
|
||||
config.Host = serviceURL.Host
|
||||
config.Path = serviceURL.Path
|
||||
|
||||
query := serviceURL.Query()
|
||||
config.Key = query.Get("key")
|
||||
config.Token = query.Get("token")
|
||||
|
||||
// Only enforce if explicitly provided but empty
|
||||
if query.Has("key") && config.Key == "" {
|
||||
return ErrMissingKey
|
||||
}
|
||||
|
||||
if query.Has("token") && config.Token == "" {
|
||||
return ErrMissingToken
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (config *Config) getURL(_ types.ConfigQueryResolver) *url.URL {
|
||||
query := url.Values{}
|
||||
query.Set("key", config.Key)
|
||||
query.Set("token", config.Token)
|
||||
|
||||
return &url.URL{
|
||||
Host: config.Host,
|
||||
Path: config.Path,
|
||||
RawQuery: query.Encode(),
|
||||
Scheme: Scheme,
|
||||
}
|
||||
}
|
6
pkg/services/googlechat/googlechat_json.go
Normal file
6
pkg/services/googlechat/googlechat_json.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package googlechat
|
||||
|
||||
// JSON is the actual payload being sent to the Google Chat API.
|
||||
type JSON struct {
|
||||
Text string `json:"text"`
|
||||
}
|
220
pkg/services/googlechat/googlechat_test.go
Normal file
220
pkg/services/googlechat/googlechat_test.go
Normal file
|
@ -0,0 +1,220 @@
|
|||
package googlechat_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/googlechat"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// TestGooglechat runs the Ginkgo test suite for the Google Chat package.
|
||||
func TestGooglechat(t *testing.T) {
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Shoutrrr Google Chat Suite")
|
||||
}
|
||||
|
||||
var (
|
||||
service *googlechat.Service
|
||||
logger *log.Logger
|
||||
envGooglechatURL *url.URL
|
||||
_ = ginkgo.BeforeSuite(func() {
|
||||
service = &googlechat.Service{}
|
||||
logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
|
||||
var err error
|
||||
envGooglechatURL, err = url.Parse(os.Getenv("SHOUTRRR_GOOGLECHAT_URL"))
|
||||
if err != nil {
|
||||
envGooglechatURL = &url.URL{} // Default to empty URL if parsing fails
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("Google Chat Service", func() {
|
||||
ginkgo.When("running integration tests", func() {
|
||||
ginkgo.It("sends a message successfully with a valid ENV URL", func() {
|
||||
if envGooglechatURL.String() == "" {
|
||||
ginkgo.Skip("No integration test ENV URL was set")
|
||||
|
||||
return
|
||||
}
|
||||
serviceURL := testutils.URLMust(envGooglechatURL.String())
|
||||
err := service.Initialize(serviceURL, testutils.TestLogger())
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
err = service.Send("This is an integration test message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the service", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
service = &googlechat.Service{}
|
||||
service.SetLogger(logger)
|
||||
})
|
||||
ginkgo.It("implements Service interface", func() {
|
||||
var impl types.Service = service
|
||||
gomega.Expect(impl).ToNot(gomega.BeNil())
|
||||
})
|
||||
ginkgo.It("returns the correct service identifier", func() {
|
||||
gomega.Expect(service.GetID()).To(gomega.Equal("googlechat"))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
service = &googlechat.Service{}
|
||||
service.SetLogger(logger)
|
||||
})
|
||||
ginkgo.It("builds a valid Google Chat Incoming Webhook URL", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(service.Config.GetURL().String()).To(gomega.Equal(configURL.String()))
|
||||
})
|
||||
ginkgo.It("is identical after de-/serialization", func() {
|
||||
testURL := "googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz"
|
||||
serviceURL := testutils.URLMust(testURL)
|
||||
err := service.Initialize(serviceURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(service.Config.GetURL().String()).To(gomega.Equal(testURL))
|
||||
})
|
||||
ginkgo.It("returns an error if key is present but empty", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).To(gomega.MatchError("missing field 'key'"))
|
||||
})
|
||||
ginkgo.It("returns an error if token is present but empty", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).To(gomega.MatchError("missing field 'token'"))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("sending the payload", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
service = &googlechat.Service{}
|
||||
service.SetLogger(logger)
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.When("sending via webhook URL", func() {
|
||||
ginkgo.It("does not report an error if the server accepts the payload", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
httpmock.NewStringResponder(200, ""),
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("reports an error if the server rejects the payload", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
httpmock.NewStringResponder(400, "Bad Request"),
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("marshals the payload correctly with the message", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
func(req *http.Request) (*http.Response, error) {
|
||||
body, err := io.ReadAll(req.Body)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(string(body)).To(gomega.MatchJSON(`{"text":"Test Message"}`))
|
||||
|
||||
return httpmock.NewStringResponse(200, ""), nil
|
||||
},
|
||||
)
|
||||
err = service.Send("Test Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("sends the POST request with correct URL and content type", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
func(req *http.Request) (*http.Response, error) {
|
||||
gomega.Expect(req.Method).To(gomega.Equal("POST"))
|
||||
gomega.Expect(req.Header.Get("Content-Type")).
|
||||
To(gomega.Equal("application/json"))
|
||||
|
||||
return httpmock.NewStringResponse(200, ""), nil
|
||||
},
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("returns marshal error if JSON marshaling fails", func() {
|
||||
// Note: Current JSON struct (string) can't fail marshaling naturally
|
||||
// This test is a placeholder for future complex payload changes
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
httpmock.NewStringResponder(200, ""),
|
||||
)
|
||||
err = service.Send("Valid Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("returns formatted error if HTTP POST fails", func() {
|
||||
configURL := testutils.URLMust(
|
||||
"googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
)
|
||||
err := service.Initialize(configURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
|
||||
httpmock.NewErrorResponder(errors.New("network failure")),
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).To(gomega.MatchError(
|
||||
"sending notification to Google Chat: Post \"https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz\": network failure",
|
||||
))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue