Skip to content

Commit

Permalink
Add Mattermost support
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Aug 9, 2020
1 parent 5af464e commit 6818e07
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ newreleases telegram
newreleases discord
newreleases hangouts-chat
newreleases microsoft-teams
newreleases mattermost
newreleases webhook
```

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.14
require (
github.com/olekukonko/tablewriter v0.0.4
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
github.com/spf13/viper v1.7.1
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
jaytaylor.com/html2text v0.0.0-20200412013138-3577fbdbcff7
newreleases.io/newreleases v1.5.1
newreleases.io/newreleases v1.6.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -337,4 +339,6 @@ newreleases.io/newreleases v1.4.0 h1:rUTHJT15P6sasUBABKpblECTuP8++xAVruoPHs+Fzto
newreleases.io/newreleases v1.4.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
newreleases.io/newreleases v1.5.1 h1:PLX8wmjxxj0ZyKkPdqtddT8krKoNy5SM4OOwdNP9x7E=
newreleases.io/newreleases v1.5.1/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
newreleases.io/newreleases v1.6.0 h1:nrUzvpi0/eJ1mjGTt8Uuur57oCiEk53wpV5FVu4OyEs=
newreleases.io/newreleases v1.6.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
4 changes: 4 additions & 0 deletions newreleases/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type command struct {
discordChannelsService discordChannelsService
hangoutsChatWebhooksService hangoutsChatWebhooksService
microsoftTeamsWebhooksService microsoftTeamsWebhooksService
mattermostWebhooksService mattermostWebhooksService
webhooksService webhooksService
}

Expand Down Expand Up @@ -101,6 +102,9 @@ More information at https://newreleases.io.`,
if err := c.initMicrosoftTeamsCmd(); err != nil {
return nil, err
}
if err := c.initMattermostCmd(); err != nil {
return nil, err
}
if err := c.initWebhookCmd(); err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions newreleases/cmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
DiscordChannelsService = discordChannelsService
HangoutsChatWebhooksService = hangoutsChatWebhooksService
MicrosoftTeamsWebhooksService = microsoftTeamsWebhooksService
MattermostWebhooksService = mattermostWebhooksService
WebhooksService = webhooksService
)

Expand Down Expand Up @@ -130,6 +131,12 @@ func WithMicrosoftTeamsWebhooksService(s MicrosoftTeamsWebhooksService) func(c *
}
}

func WithMattermostWebhooksService(s MattermostWebhooksService) func(c *Command) {
return func(c *Command) {
c.mattermostWebhooksService = s
}
}

func WithWebhooksService(s WebhooksService) func(c *Command) {
return func(c *Command) {
c.webhooksService = s
Expand Down
63 changes: 63 additions & 0 deletions newreleases/cmd/mattermost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2020, NewReleases CLI 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 cmd

import (
"context"

"github.com/spf13/cobra"
"newreleases.io/newreleases"
)

func (c *command) initMattermostCmd() (err error) {
cmd := &cobra.Command{
Use: "mattermost",
Short: "List Mattermost integrations",
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := newClientContext(c.config)
defer cancel()

webhooks, err := c.mattermostWebhooksService.List(ctx)
if err != nil {
return err
}

if len(webhooks) == 0 {
cmd.Println("No Mattermost Webhooks found.")
return nil
}

printWebhooksTable(cmd, webhooks)

return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientConfigOptions(cmd, c.config); err != nil {
return err
}
return c.setMattermostWebhooksService(cmd, args)
},
}

c.root.AddCommand(cmd)
return addClientFlags(cmd)
}

func (c *command) setMattermostWebhooksService(cmd *cobra.Command, args []string) (err error) {
if c.mattermostWebhooksService != nil {
return nil
}
client, err := c.getClient(cmd)
if err != nil {
return err
}
c.mattermostWebhooksService = client.MattermostWebhooks
return nil
}

type mattermostWebhooksService interface {
List(ctx context.Context) (webhooks []newreleases.Webhook, err error)
}
78 changes: 78 additions & 0 deletions newreleases/cmd/mattermost_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2020, NewReleases CLI 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 cmd_test

import (
"bytes"
"context"
"testing"

"newreleases.io/cmd/newreleases/cmd"
"newreleases.io/newreleases"
)

func TestMattermostCmd(t *testing.T) {
for _, tc := range []struct {
name string
mattermostWebhooksService cmd.MattermostWebhooksService
wantOutput string
wantError error
}{
{
name: "no webhooks",
mattermostWebhooksService: newMockMattermostWebhooksService(nil, nil),
wantOutput: "No Mattermost Webhooks found.\n",
},
{
name: "with webhooks",
mattermostWebhooksService: newMockMattermostWebhooksService([]newreleases.Webhook{
{
ID: "4qOpc9t16rpymcw7z8jwn5y6anne0sg5a9b1",
Name: "NewReleases",
},
{
ID: "c6anne0sg9t4qOp16rpymcw7z8jwn5y5a9b1",
Name: "Awesome project",
},
}, nil),
wantOutput: "ID NAME \n4qOpc9t16rpymcw7z8jwn5y6anne0sg5a9b1 NewReleases \nc6anne0sg9t4qOp16rpymcw7z8jwn5y5a9b1 Awesome project \n",
},
{
name: "error",
mattermostWebhooksService: newMockMattermostWebhooksService(nil, errTest),
wantError: errTest,
},
} {
t.Run(tc.name, func(t *testing.T) {
var outputBuf bytes.Buffer
if err := newCommand(t,
cmd.WithArgs("mattermost"),
cmd.WithOutput(&outputBuf),
cmd.WithMattermostWebhooksService(tc.mattermostWebhooksService),
).Execute(); err != tc.wantError {
t.Fatalf("got error %v, want %v", err, tc.wantError)
}

gotOutput := outputBuf.String()
if gotOutput != tc.wantOutput {
t.Errorf("got output %q, want %q", gotOutput, tc.wantOutput)
}
})
}
}

type mockMattermostWebhooksService struct {
webhooks []newreleases.Webhook
err error
}

func newMockMattermostWebhooksService(webhooks []newreleases.Webhook, err error) (s mockMattermostWebhooksService) {
return mockMattermostWebhooksService{webhooks: webhooks, err: err}
}

func (s mockMattermostWebhooksService) List(ctx context.Context) (webhooks []newreleases.Webhook, err error) {
return s.webhooks, s.err
}
13 changes: 13 additions & 0 deletions newreleases/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
hasDiscord bool
hasHangoutsChat bool
hasMicrosoftTeams bool
hasMattermost bool
hasWebhook bool
hasExclusions bool
hasInclusions bool
Expand All @@ -101,6 +102,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if len(p.MSTeamsWebhookIDs) > 0 {
hasMicrosoftTeams = true
}
if len(p.MattermostWebhookIDs) > 0 {
hasMattermost = true
}
if len(p.WebhookIDs) > 0 {
hasWebhook = true
}
Expand Down Expand Up @@ -144,6 +148,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if hasMicrosoftTeams {
header = append(header, "Microsoft Teams")
}
if hasMattermost {
header = append(header, "Mattermost")
}
if hasWebhook {
header = append(header, "Webhook")
}
Expand Down Expand Up @@ -180,6 +187,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if hasMicrosoftTeams {
r = append(r, strings.Join(p.MSTeamsWebhookIDs, ", "))
}
if hasMattermost {
r = append(r, strings.Join(p.MattermostWebhookIDs, ", "))
}
if hasWebhook {
r = append(r, strings.Join(p.WebhookIDs, ", "))
}
Expand Down Expand Up @@ -235,6 +245,9 @@ func printProject(cmd *cobra.Command, p *newreleases.Project) {
if len(p.MSTeamsWebhookIDs) > 0 {
table.Append([]string{"Microsoft Teams:", strings.Join(p.MSTeamsWebhookIDs, ", ")})
}
if len(p.MattermostWebhookIDs) > 0 {
table.Append([]string{"Mattermost:", strings.Join(p.MattermostWebhookIDs, ", ")})
}
if len(p.WebhookIDs) > 0 {
table.Append([]string{"Webhooks:", strings.Join(p.WebhookIDs, ", ")})
}
Expand Down
6 changes: 6 additions & 0 deletions newreleases/cmd/project_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (c *command) initProjectAddCmd(projectCmd *cobra.Command) (err error) {
optionNameDiscord = "discord"
optionNameHangoutsChat = "hangouts-chat"
optionNameMicrosoftTeams = "microsoft-teams"
optionNameMattermost = "mattermost"
optionNameWebhook = "webhook"
optionNameExclusions = "regex-exclude"
optionNameExcludePrereleases = "exclude-prereleases"
Expand Down Expand Up @@ -68,6 +69,10 @@ func (c *command) initProjectAddCmd(projectCmd *cobra.Command) (err error) {
if err != nil {
return err
}
o.MattermostWebhookIDs, err = flags.GetStringArray(optionNameMattermost)
if err != nil {
return err
}
o.WebhookIDs, err = flags.GetStringArray(optionNameWebhook)
if err != nil {
return err
Expand Down Expand Up @@ -124,6 +129,7 @@ func (c *command) initProjectAddCmd(projectCmd *cobra.Command) (err error) {
cmd.Flags().StringArray(optionNameDiscord, nil, "Discord channel ID")
cmd.Flags().StringArray(optionNameHangoutsChat, nil, "Hangouts Chat webhook ID")
cmd.Flags().StringArray(optionNameMicrosoftTeams, nil, "Microsoft Teams webhook ID")
cmd.Flags().StringArray(optionNameMattermost, nil, "Mattermost webhook ID")
cmd.Flags().StringArray(optionNameWebhook, nil, "Webhook ID")
cmd.Flags().StringArray(optionNameExclusions, nil, "Regex version exclusion, suffix with \"-inverse\" for inclusion")
cmd.Flags().Bool(optionNameExcludePrereleases, false, "exclude pre-releases")
Expand Down
3 changes: 2 additions & 1 deletion newreleases/cmd/project_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func TestProjectCmd_Add(t *testing.T) {
"--discord", "zext4mdsbe6tyksdfey0td5gwg",
"--hangouts-chat", "etyksdfeyxt4mdsbe60td5gwgz",
"--microsoft-teams", "0td5gwgzextbe6tyksdfey4mds",
"--mattermost", "wgxtzed4yksd5dfeymsbe6t0tg",
"--webhook", "tbe6tyksdfey4md0td5gwgzexs",
"--regex-exclude", `^0\.1`,
"--regex-exclude", `^0\.3-inverse`,
"--exclude-prereleases",
"--exclude-updated",
},
projectsService: newMockProjectsService(1, nil),
wantOutput: "ID: new \nName: golang/go \nProvider: github \nEmail: weekly \nSlack: mdsbe60td5gwgzetyksdfeyxt4 \nTelegram: sdfeyxt4mdsbe60td5gwgzetyk \nDiscord: 4mdsbe60td5gwgzetyksdfeyxt, zext4mdsbe6tyksdfey0td5gwg \nHangouts Chat: etyksdfeyxt4mdsbe60td5gwgz \nMicrosoft Teams: 0td5gwgzextbe6tyksdfey4mds \nWebhooks: tbe6tyksdfey4md0td5gwgzexs \nRegex Exclude: ^0\\.1 \nRegex Exclude Inverse: ^0\\.3 \nExclude Pre-Releases: yes \nExclude Updated: yes \n",
wantOutput: "ID: new \nName: golang/go \nProvider: github \nEmail: weekly \nSlack: mdsbe60td5gwgzetyksdfeyxt4 \nTelegram: sdfeyxt4mdsbe60td5gwgzetyk \nDiscord: 4mdsbe60td5gwgzetyksdfeyxt, zext4mdsbe6tyksdfey0td5gwg \nHangouts Chat: etyksdfeyxt4mdsbe60td5gwgz \nMicrosoft Teams: 0td5gwgzextbe6tyksdfey4mds \nMattermost: wgxtzed4yksd5dfeymsbe6t0tg \nWebhooks: tbe6tyksdfey4md0td5gwgzexs \nRegex Exclude: ^0\\.1 \nRegex Exclude Inverse: ^0\\.3 \nExclude Pre-Releases: yes \nExclude Updated: yes \n",
},
{
name: "error",
Expand Down
4 changes: 2 additions & 2 deletions newreleases/cmd/project_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestProjectCmd_Get(t *testing.T) {
wantOutput: "ID: mdsbe60td5gwgzetyksdfeyxt4 \nName: golang/go \nProvider: github \n",
},
{
name: "full project",
name: "full project",
projectsService: newMockProjectsService(1, nil, []newreleases.Project{fullProject}),
wantOutput: "ID: mdsbe60td5gwgzetyksdfeyxt4 \nName: golang/go \nProvider: github \nEmail: daily \nSlack: zetyksdfeymdsbe60td5gwgxt4 \nTelegram: sbe60td5gwgxtzetyksdfeymd4 \nDiscord: tyksdfeymsbegxtzed460td5gw \nHangouts Chat: yksdfeymsbe6t0td5gzed4wgxt \nMicrosoft Teams: gwgxtzed4yksdfeymsbe6t0td5 \nWebhooks: e6t0td5ykgwgxtzed4eymsbsdf \nRegex Exclude: ^0\\.1 \nRegex Exclude Inverse: ^0\\.3 \nExclude Pre-Releases: yes \nExclude Updated: yes \n",
wantOutput: "ID: mdsbe60td5gwgzetyksdfeyxt4 \nName: golang/go \nProvider: github \nEmail: daily \nSlack: zetyksdfeymdsbe60td5gwgxt4 \nTelegram: sbe60td5gwgxtzetyksdfeymd4 \nDiscord: tyksdfeymsbegxtzed460td5gw \nHangouts Chat: yksdfeymsbe6t0td5gzed4wgxt \nMicrosoft Teams: gwgxtzed4yksdfeymsbe6t0td5 \nMattermost: wgxtzed4yksd5dfeymsbe6t0tg \nWebhooks: e6t0td5ykgwgxtzed4eymsbsdf \nRegex Exclude: ^0\\.1 \nRegex Exclude Inverse: ^0\\.3 \nExclude Pre-Releases: yes \nExclude Updated: yes \n",
},
{
name: "error",
Expand Down
2 changes: 1 addition & 1 deletion newreleases/cmd/project_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestProjectCmd_List(t *testing.T) {
{
name: "full project",
projectsService: newMockProjectsService(1, nil, []newreleases.Project{fullProject}),
wantOutput: "ID NAME PROVIDER EMAIL SLACK TELEGRAM DISCORD HANGOUTS CHAT MICROSOFT TEAMS WEBHOOK REGEX EXCLUDE REGEX EXCLUDE INVERSE EXCLUDE PRE-RELEASES EXCLUDE UPDATED \nmdsbe60td5gwgzetyksdfeyxt4 golang/go github daily zetyksdfeymdsbe60td5gwgxt4 sbe60td5gwgxtzetyksdfeymd4 tyksdfeymsbegxtzed460td5gw yksdfeymsbe6t0td5gzed4wgxt gwgxtzed4yksdfeymsbe6t0td5 e6t0td5ykgwgxtzed4eymsbsdf ^0\\.1 ^0\\.3 yes yes \n",
wantOutput: "ID NAME PROVIDER EMAIL SLACK TELEGRAM DISCORD HANGOUTS CHAT MICROSOFT TEAMS MATTERMOST WEBHOOK REGEX EXCLUDE REGEX EXCLUDE INVERSE EXCLUDE PRE-RELEASES EXCLUDE UPDATED \nmdsbe60td5gwgzetyksdfeyxt4 golang/go github daily zetyksdfeymdsbe60td5gwgxt4 sbe60td5gwgxtzetyksdfeymd4 tyksdfeymsbegxtzed460td5gw yksdfeymsbe6t0td5gzed4wgxt gwgxtzed4yksdfeymsbe6t0td5 wgxtzed4yksd5dfeymsbe6t0tg e6t0td5ykgwgxtzed4eymsbsdf ^0\\.1 ^0\\.3 yes yes \n",
},
{
name: "error",
Expand Down
2 changes: 1 addition & 1 deletion newreleases/cmd/project_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestProjectCmd_Search(t *testing.T) {
name: "full project",
args: []string{"golang"},
projectsService: newMockProjectsService(1, nil, []newreleases.Project{fullProject}),
wantOutput: "ID NAME PROVIDER EMAIL SLACK TELEGRAM DISCORD HANGOUTS CHAT MICROSOFT TEAMS WEBHOOK REGEX EXCLUDE REGEX EXCLUDE INVERSE EXCLUDE PRE-RELEASES EXCLUDE UPDATED \nmdsbe60td5gwgzetyksdfeyxt4 golang/go github daily zetyksdfeymdsbe60td5gwgxt4 sbe60td5gwgxtzetyksdfeymd4 tyksdfeymsbegxtzed460td5gw yksdfeymsbe6t0td5gzed4wgxt gwgxtzed4yksdfeymsbe6t0td5 e6t0td5ykgwgxtzed4eymsbsdf ^0\\.1 ^0\\.3 yes yes \n",
wantOutput: "ID NAME PROVIDER EMAIL SLACK TELEGRAM DISCORD HANGOUTS CHAT MICROSOFT TEAMS MATTERMOST WEBHOOK REGEX EXCLUDE REGEX EXCLUDE INVERSE EXCLUDE PRE-RELEASES EXCLUDE UPDATED \nmdsbe60td5gwgzetyksdfeyxt4 golang/go github daily zetyksdfeymdsbe60td5gwgxt4 sbe60td5gwgxtzetyksdfeymd4 tyksdfeymsbegxtzed460td5gw yksdfeymsbe6t0td5gzed4wgxt gwgxtzed4yksdfeymsbe6t0td5 wgxtzed4yksd5dfeymsbe6t0tg e6t0td5ykgwgxtzed4eymsbsdf ^0\\.1 ^0\\.3 yes yes \n",
},
{
name: "error",
Expand Down
10 changes: 10 additions & 0 deletions newreleases/cmd/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
DiscordIDs: []string{"tyksdfeymsbegxtzed460td5gw"},
HangoutsChatWebhookIDs: []string{"yksdfeymsbe6t0td5gzed4wgxt"},
MSTeamsWebhookIDs: []string{"gwgxtzed4yksdfeymsbe6t0td5"},
MattermostWebhookIDs: []string{"wgxtzed4yksd5dfeymsbe6t0tg"},
WebhookIDs: []string{"e6t0td5ykgwgxtzed4eymsbsdf"},
Exclusions: []newreleases.Exclusion{
{Value: `^0\.1`},
Expand Down Expand Up @@ -134,6 +135,9 @@ func (s mockProjectsService) Add(ctx context.Context, provider, name string, o *
if o.MSTeamsWebhookIDs != nil {
project.MSTeamsWebhookIDs = o.MSTeamsWebhookIDs
}
if o.MattermostWebhookIDs != nil {
project.MattermostWebhookIDs = o.MattermostWebhookIDs
}
if o.WebhookIDs != nil {
project.WebhookIDs = o.WebhookIDs
}
Expand Down Expand Up @@ -172,6 +176,9 @@ func (s mockProjectsService) UpdateByID(ctx context.Context, id string, o *newre
if o.MSTeamsWebhookIDs != nil {
project.MSTeamsWebhookIDs = o.MSTeamsWebhookIDs
}
if o.MattermostWebhookIDs != nil {
project.MattermostWebhookIDs = o.MattermostWebhookIDs
}
if o.WebhookIDs != nil {
project.WebhookIDs = o.WebhookIDs
}
Expand Down Expand Up @@ -210,6 +217,9 @@ func (s mockProjectsService) UpdateByName(ctx context.Context, provider, name st
if o.MSTeamsWebhookIDs != nil {
project.MSTeamsWebhookIDs = o.MSTeamsWebhookIDs
}
if o.MattermostWebhookIDs != nil {
project.MattermostWebhookIDs = o.MattermostWebhookIDs
}
if o.WebhookIDs != nil {
project.WebhookIDs = o.WebhookIDs
}
Expand Down
Loading

0 comments on commit 6818e07

Please sign in to comment.