Skip to content

Commit

Permalink
Support Matrix notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Jun 13, 2022
1 parent 06079f9 commit 0676ea1
Show file tree
Hide file tree
Showing 20 changed files with 240 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.43.0
version: v1.46.2
args: --timeout 10m

- name: Vet
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ newreleases hangouts-chat
newreleases microsoft-teams
newreleases mattermost
newreleases rocketchat
newreleases matrix
newreleases webhook
```

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/spf13/viper v1.10.1
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba
newreleases.io/newreleases v1.8.0
newreleases.io/newreleases v1.9.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba h1:3xhBI8FZepFq4YtdqlW6Z8YzdKM3nAV9xpOvgzWX+us=
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba/go.mod h1:OxvTsCwKosqQ1q7B+8FwXqg4rKZ/UG9dUW+g/VL2xH4=
newreleases.io/newreleases v1.8.0 h1:QMf7R2hAr4kMmYcMjeWu0jiQww52GB7uohCSr3BhlRI=
newreleases.io/newreleases v1.8.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
newreleases.io/newreleases v1.9.0 h1:5Nuk4UBQ5WZdiuwtNCUNzCeRa/1gyK4Dp+YneRdYcYI=
newreleases.io/newreleases v1.9.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
4 changes: 4 additions & 0 deletions newreleases/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type command struct {
microsoftTeamsWebhooksService microsoftTeamsWebhooksService
mattermostWebhooksService mattermostWebhooksService
rocketchatWebhooksService rocketchatWebhooksService
matrixRoomsService matrixRoomsService
webhooksService webhooksService
tagsService tagsService
}
Expand Down Expand Up @@ -110,6 +111,9 @@ More information at https://newreleases.io.`,
if err := c.initRocketchatCmd(); err != nil {
return nil, err
}
if err := c.initMatrixCmd(); err != nil {
return nil, err
}
if err := c.initWebhookCmd(); err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions newreleases/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"
Expand All @@ -22,7 +21,7 @@ var homeDir string
var errTest = errors.New("test error")

func TestMain(m *testing.M) {
dir, err := ioutil.TempDir("", "newreleases-cmd-")
dir, err := os.MkdirTemp("", "newreleases-cmd-")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
19 changes: 5 additions & 14 deletions newreleases/cmd/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package cmd_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -67,11 +66,7 @@ func TestConfigureCmd(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "newreleases-cmd-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

cfgFile := filepath.Join(dir, ".newreleases.yaml")
if !tc.newConfig {
Expand Down Expand Up @@ -122,15 +117,15 @@ func TestConfigureCmd(t *testing.T) {
}

if tc.wantData != "" {
gotData, err := ioutil.ReadFile(cfgFile)
gotData, err := os.ReadFile(cfgFile)
if err != nil {
t.Fatal(err)
}
if string(gotData) != tc.wantData {
t.Errorf("got config file data %q, want %q", string(gotData), tc.wantData)
}
} else {
gotData, _ := ioutil.ReadFile(cfgFile)
gotData, _ := os.ReadFile(cfgFile)
if string(gotData) != "" {
t.Errorf("got config file data %q, but it should not be", string(gotData))
}
Expand All @@ -140,11 +135,7 @@ func TestConfigureCmd(t *testing.T) {
}

func TestConfigureCmd_overwrite(t *testing.T) {
dir, err := ioutil.TempDir("", "newreleases-cmd-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

cfgFile := filepath.Join(dir, ".newreleases.yaml")
f, err := os.Create(cfgFile)
Expand Down Expand Up @@ -175,7 +166,7 @@ func TestConfigureCmd_overwrite(t *testing.T) {
t.Errorf("got output %q, want %q", gotOutput, wantOutput)
}

gotData, err := ioutil.ReadFile(cfgFile)
gotData, err := os.ReadFile(cfgFile)
if err != nil {
t.Fatal(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 @@ -23,6 +23,7 @@ type (
MicrosoftTeamsWebhooksService = microsoftTeamsWebhooksService
MattermostWebhooksService = mattermostWebhooksService
RocketchatWebhooksService = rocketchatWebhooksService
MatrixRoomsService = matrixRoomsService
WebhooksService = webhooksService
TagsService = tagsService
)
Expand Down Expand Up @@ -145,6 +146,12 @@ func WithRocketchatWebhooksService(s RocketchatWebhooksService) func(c *Command)
}
}

func WithMatrixRoomsService(s MatrixRoomsService) func(c *Command) {
return func(c *Command) {
c.matrixRoomsService = s
}
}

func WithWebhooksService(s WebhooksService) func(c *Command) {
return func(c *Command) {
c.webhooksService = s
Expand Down
11 changes: 3 additions & 8 deletions newreleases/cmd/get_auth_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -143,11 +142,7 @@ func TestGetAuthKeyCmd(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
dir, err := ioutil.TempDir("", "newreleases-cmd-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

cfgFile := filepath.Join(dir, ".newreleases.yaml")
if !tc.newConfig {
Expand Down Expand Up @@ -200,15 +195,15 @@ func TestGetAuthKeyCmd(t *testing.T) {
}

if tc.wantData != "" {
gotData, err := ioutil.ReadFile(cfgFile)
gotData, err := os.ReadFile(cfgFile)
if err != nil {
t.Fatal(err)
}
if string(gotData) != tc.wantData {
t.Errorf("got config file data %q, want %q", string(gotData), tc.wantData)
}
} else {
gotData, _ := ioutil.ReadFile(cfgFile)
gotData, _ := os.ReadFile(cfgFile)
if string(gotData) != "" {
t.Errorf("got config file data %q, but it should not be", string(gotData))
}
Expand Down
72 changes: 72 additions & 0 deletions newreleases/cmd/matrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2022, 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) initMatrixCmd() (err error) {
cmd := &cobra.Command{
Use: "matrix",
Short: "List Matrix integrations",
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := newClientContext(c.config)
defer cancel()

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

if len(rooms) == 0 {
cmd.Println("No Matrix Rooms found.")
return nil
}

printMatrixRoomsTable(cmd, rooms)

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

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

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

type matrixRoomsService interface {
List(ctx context.Context) (rooms []newreleases.MatrixRoom, err error)
}

func printMatrixRoomsTable(cmd *cobra.Command, rooms []newreleases.MatrixRoom) {
table := newTable(cmd.OutOrStdout())
table.SetHeader([]string{"ID", "Name", "Homeserver URL", "Internal Room ID"})
for _, e := range rooms {
table.Append([]string{e.ID, e.Name, e.HomeserverURL, e.InternalRoomID})
}
table.Render()
}
82 changes: 82 additions & 0 deletions newreleases/cmd/matrix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2022, 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 TestMatrixCmd(t *testing.T) {
for _, tc := range []struct {
name string
matrixRoomsService cmd.MatrixRoomsService
wantOutput string
wantError error
}{
{
name: "no rooms",
matrixRoomsService: newMockMatrixRoomsService(nil, nil),
wantOutput: "No Matrix Rooms found.\n",
},
{
name: "with rooms",
matrixRoomsService: newMockMatrixRoomsService([]newreleases.MatrixRoom{
{
ID: "znne04qO5y6acw7sg5a9b1pc9t16rpym8jwn",
Name: "NewReleases",
HomeserverURL: "https://matrix-client.matrix.org",
InternalRoomID: "!CklbIcwhYKygGsulFi:matrix.org",
},
{
ID: "9t4qOp16gmcw7z8jwrpyc6anne0sn5y5a9b1",
Name: "Awesome project",
HomeserverURL: "https://matrix-client.example.com",
InternalRoomID: "!CkGsIcwhKyulFYlbgi:example.com",
},
}, nil),
wantOutput: "ID NAME HOMESERVER URL INTERNAL ROOM ID \nznne04qO5y6acw7sg5a9b1pc9t16rpym8jwn NewReleases https://matrix-client.matrix.org !CklbIcwhYKygGsulFi:matrix.org \n9t4qOp16gmcw7z8jwrpyc6anne0sn5y5a9b1 Awesome project https://matrix-client.example.com !CkGsIcwhKyulFYlbgi:example.com \n",
},
{
name: "error",
matrixRoomsService: newMockMatrixRoomsService(nil, errTest),
wantError: errTest,
},
} {
t.Run(tc.name, func(t *testing.T) {
var outputBuf bytes.Buffer
if err := newCommand(t,
cmd.WithArgs("matrix"),
cmd.WithOutput(&outputBuf),
cmd.WithMatrixRoomsService(tc.matrixRoomsService),
).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 mockMatrixRoomsService struct {
rooms []newreleases.MatrixRoom
err error
}

func newMockMatrixRoomsService(rooms []newreleases.MatrixRoom, err error) mockMatrixRoomsService {
return mockMatrixRoomsService{rooms: rooms, err: err}
}

func (s mockMatrixRoomsService) List(ctx context.Context) ([]newreleases.MatrixRoom, error) {
return s.rooms, s.err
}
13 changes: 13 additions & 0 deletions newreleases/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
hasMicrosoftTeams bool
hasMattermost bool
hasRocketchat bool
hasMatrix bool
hasWebhook bool
hasExclusions bool
hasInclusions bool
Expand Down Expand Up @@ -112,6 +113,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if len(p.RocketchatWebhookIDs) > 0 {
hasRocketchat = true
}
if len(p.MatrixRoomIDs) > 0 {
hasMatrix = true
}
if len(p.WebhookIDs) > 0 {
hasWebhook = true
}
Expand Down Expand Up @@ -167,6 +171,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if hasRocketchat {
header = append(header, "Rocket.Chat")
}
if hasMatrix {
header = append(header, "Matrix")
}
if hasWebhook {
header = append(header, "Webhook")
}
Expand Down Expand Up @@ -215,6 +222,9 @@ func printProjectsTable(cmd *cobra.Command, projects []newreleases.Project) {
if hasRocketchat {
r = append(r, strings.Join(p.RocketchatWebhookIDs, ", "))
}
if hasMatrix {
r = append(r, strings.Join(p.MatrixRoomIDs, ", "))
}
if hasWebhook {
r = append(r, strings.Join(p.WebhookIDs, ", "))
}
Expand Down Expand Up @@ -286,6 +296,9 @@ func printProject(cmd *cobra.Command, p *newreleases.Project) {
if len(p.RocketchatWebhookIDs) > 0 {
table.Append([]string{"Rocket.Chat:", strings.Join(p.RocketchatWebhookIDs, ", ")})
}
if len(p.MatrixRoomIDs) > 0 {
table.Append([]string{"Matrix:", strings.Join(p.MatrixRoomIDs, ", ")})
}
if len(p.WebhookIDs) > 0 {
table.Append([]string{"Webhooks:", strings.Join(p.WebhookIDs, ", ")})
}
Expand Down
Loading

0 comments on commit 0676ea1

Please sign in to comment.