From c923becd5bc3239bcf06ace5f3f3d84491b6fec2 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Sat, 30 Nov 2019 16:26:11 +0100 Subject: [PATCH] Add Discord support --- .travis.yml | 2 +- README.md | 1 + discord_channels.go | 33 ++++++++++++++++++++++++++ discord_channels_test.go | 51 ++++++++++++++++++++++++++++++++++++++++ newreleases.go | 2 ++ projects.go | 2 ++ projects_test.go | 2 ++ slack_channels_test.go | 8 +++---- telegram_chats.go | 2 +- 9 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 discord_channels.go create mode 100644 discord_channels_test.go diff --git a/.travis.yml b/.travis.yml index aed0a2c..e0c8339 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.12.4 + - 1.13 install: - go get -v newreleases.io/newreleases/... diff --git a/README.md b/README.md index 32102e0..b339b9e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ This client implements all NewReleases API features. - Get tracked providers - Get added Slack Channels - Get added Telegram Chats +- Get added Dissord Channels - Get added Hangouts Chat webhooks - Get added Microsoft Teams webhooks - Get added custom Webhooks diff --git a/discord_channels.go b/discord_channels.go new file mode 100644 index 0000000..55726fc --- /dev/null +++ b/discord_channels.go @@ -0,0 +1,33 @@ +// Copyright (c) 2019, NewReleases Go client AUTHORS. +// All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package newreleases + +import ( + "context" + "net/http" +) + +// DiscordChannelsService provides information about Discord notifications. +type DiscordChannelsService service + +// DiscordChannel holds information about a Discord Channel that is connected to the +// account. +type DiscordChannel struct { + ID string `json:"id"` + Name string `json:"name"` +} + +// List returns all connected Discord Channels. +func (s *DiscordChannelsService) List(ctx context.Context) (channels []DiscordChannel, err error) { + + type discordChannelsResponse struct { + Channels []DiscordChannel `json:"channels"` + } + + var r discordChannelsResponse + err = s.client.request(ctx, http.MethodGet, "v1/discord-channels", nil, &r) + return r.Channels, err +} diff --git a/discord_channels_test.go b/discord_channels_test.go new file mode 100644 index 0000000..8439590 --- /dev/null +++ b/discord_channels_test.go @@ -0,0 +1,51 @@ +// Copyright (c) 2019, NewReleases Go client AUTHORS. +// All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package newreleases_test + +import ( + "context" + "fmt" + "net/http" + "testing" + + "newreleases.io/newreleases" +) + +func TestDiscordChannelsService_List(t *testing.T) { + client, mux, _, teardown := newClient(t, "") + defer teardown() + + mux.HandleFunc("/v1/discord-channels", requireMethod("GET", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", jsonContentType) + fmt.Fprintln(w, discordChannelsServiceList) + })) + + got, err := client.DiscordChannels.List(context.Background()) + if err != nil { + t.Fatal(err) + } + + assertEqual(t, "", got, discordChannelsServiceListWant) +} + +var ( + discordChannelsServiceList = ` + { + "channels": [ + { + "id": "02bgrl5510q3pe8gf7322d8n5", + "name": "NewReleases Yeah" + } + ] + } + ` + discordChannelsServiceListWant = []newreleases.DiscordChannel{ + { + ID: "02bgrl5510q3pe8gf7322d8n5", + Name: "NewReleases Yeah", + }, + } +) diff --git a/newreleases.go b/newreleases.go index 1721e94..d1b22b0 100644 --- a/newreleases.go +++ b/newreleases.go @@ -44,6 +44,7 @@ type Client struct { Releases *ReleasesService SlackChannels *SlackChannelsService TelegramChats *TelegramChatsService + DiscordChannels *DiscordChannelsService HangoutsChatWebhooks *HangoutsChatWebhooksService MicrosoftTeamsWebhooks *MicrosoftTeamsWebhooksService Webhooks *WebhooksService @@ -87,6 +88,7 @@ func newClient(httpClient *http.Client) (c *Client) { c.Releases = (*ReleasesService)(&c.service) c.SlackChannels = (*SlackChannelsService)(&c.service) c.TelegramChats = (*TelegramChatsService)(&c.service) + c.DiscordChannels = (*DiscordChannelsService)(&c.service) c.HangoutsChatWebhooks = (*HangoutsChatWebhooksService)(&c.service) c.MicrosoftTeamsWebhooks = (*MicrosoftTeamsWebhooksService)(&c.service) c.Webhooks = (*WebhooksService)(&c.service) diff --git a/projects.go b/projects.go index 97247da..6062dd6 100644 --- a/projects.go +++ b/projects.go @@ -25,6 +25,7 @@ type Project struct { EmailNotification EmailNotification `json:"email_notification,omitempty"` SlackIDs []string `json:"slack_channels,omitempty"` TelegramChatIDs []string `json:"telegram_chats,omitempty"` + DiscordIDs []string `json:"discord_channels,omitempty"` HangoutsChatWebhookIDs []string `json:"hangouts_chat_webhooks,omitempty"` MSTeamsWebhookIDs []string `json:"microsoft_teams_webhooks,omitempty"` WebhookIDs []string `json:"webhooks,omitempty"` @@ -149,6 +150,7 @@ type ProjectOptions struct { EmailNotification *EmailNotification `json:"email_notification"` SlackIDs []string `json:"slack_channels"` TelegramChatIDs []string `json:"telegram_chats"` + DiscordIDs []string `json:"discord_channels"` HangoutsChatWebhookIDs []string `json:"hangouts_chat_webhooks"` MSTeamsWebhookIDs []string `json:"microsoft_teams_webhooks"` WebhookIDs []string `json:"webhooks"` diff --git a/projects_test.go b/projects_test.go index 58ff51a..0e8ab4c 100644 --- a/projects_test.go +++ b/projects_test.go @@ -405,6 +405,7 @@ var ( EmailNotification: newreleases.EmailNotificationHourly, SlackIDs: []string{"slack123"}, TelegramChatIDs: []string{"telegram123"}, + DiscordIDs: []string{"discord123"}, HangoutsChatWebhookIDs: []string{"hangouts123"}, MSTeamsWebhookIDs: []string{"teams123"}, WebhookIDs: []string{"webhook123", "webhook124"}, @@ -416,6 +417,7 @@ var ( EmailNotification: &newreleases.EmailNotificationHourly, SlackIDs: []string{"slack123"}, TelegramChatIDs: []string{"telegram123"}, + DiscordIDs: []string{"discord123"}, HangoutsChatWebhookIDs: []string{"hangouts123"}, MSTeamsWebhookIDs: []string{"teams123"}, WebhookIDs: []string{"webhook123", "webhook124"}, diff --git a/slack_channels_test.go b/slack_channels_test.go index 5f6a207..482752c 100644 --- a/slack_channels_test.go +++ b/slack_channels_test.go @@ -20,7 +20,7 @@ func TestSlackChannelsService_List(t *testing.T) { mux.HandleFunc("/v1/slack-channels", requireMethod("GET", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", jsonContentType) - fmt.Fprintln(w, clackChannelsServiceList) + fmt.Fprintln(w, slackChannelsServiceList) })) got, err := client.SlackChannels.List(context.Background()) @@ -28,11 +28,11 @@ func TestSlackChannelsService_List(t *testing.T) { t.Fatal(err) } - assertEqual(t, "", got, clackChannelsServiceListWant) + assertEqual(t, "", got, slackChannelsServiceListWant) } var ( - clackChannelsServiceList = ` + slackChannelsServiceList = ` { "channels": [ { @@ -43,7 +43,7 @@ var ( ] } ` - clackChannelsServiceListWant = []newreleases.SlackChannel{ + slackChannelsServiceListWant = []newreleases.SlackChannel{ { ID: "00q3pe8gf7322d8n52bgrl551", Channel: "releases", diff --git a/telegram_chats.go b/telegram_chats.go index ec8c7cb..e566d89 100644 --- a/telegram_chats.go +++ b/telegram_chats.go @@ -21,7 +21,7 @@ type TelegramChat struct { Name string `json:"name"` } -// List returns all connected Slack Channels. +// List returns all connected Telegram Chats. func (s *TelegramChatsService) List(ctx context.Context) (channels []TelegramChat, err error) { type TelegramChatsResponse struct {