-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.