-
Notifications
You must be signed in to change notification settings - Fork 881
feat(cli): pause notifications #13873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fc12d54
feat(cli): pause notifications
mtojek 3777e94
stub
mtojek acab552
cli tests
mtojek 4cfdc08
make gen
mtojek 85179b3
golden
mtojek 9e94fef
1.25
mtojek 3694fae
unpause -> resume
mtojek d907ef2
check error message
mtojek 94987d2
table tests
mtojek c8d208a
rm golden
mtojek 727620d
missing colon
mtojek 54404fa
linter complains
mtojek 6c1e2d8
fix golden
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,85 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/serpent" | ||
|
||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func (r *RootCmd) notifications() *serpent.Command { | ||
cmd := &serpent.Command{ | ||
Use: "notifications", | ||
Short: "Manage Coder notifications", | ||
Long: "Administrators can use these commands to change notification settings.\n" + FormatExamples( | ||
Example{ | ||
Description: "Pause Coder notifications. Administrators can temporarily stop notifiers from dispatching messages in case of the target outage (for example: unavailable SMTP server or Webhook not responding).", | ||
Command: "coder notifications pause", | ||
}, | ||
Example{ | ||
Description: "Resume Coder notifications", | ||
Command: "coder notifications resume", | ||
}, | ||
), | ||
Aliases: []string{"notification"}, | ||
Handler: func(inv *serpent.Invocation) error { | ||
return inv.Command.HelpHandler(inv) | ||
}, | ||
Children: []*serpent.Command{ | ||
r.pauseNotifications(), | ||
r.resumeNotifications(), | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func (r *RootCmd) pauseNotifications() *serpent.Command { | ||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
Use: "pause", | ||
Short: "Pause notifications", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(0), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
err := client.PutNotificationsSettings(inv.Context(), codersdk.NotificationsSettings{ | ||
NotifierPaused: true, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("unable to pause notifications: %w", err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(inv.Stderr, "Notifications are now paused.") | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func (r *RootCmd) resumeNotifications() *serpent.Command { | ||
client := new(codersdk.Client) | ||
cmd := &serpent.Command{ | ||
Use: "resume", | ||
Short: "Resume notifications", | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(0), | ||
r.InitClient(client), | ||
), | ||
Handler: func(inv *serpent.Invocation) error { | ||
err := client.PutNotificationsSettings(inv.Context(), codersdk.NotificationsSettings{ | ||
NotifierPaused: false, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("unable to resume notifications: %w", err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(inv.Stderr, "Notifications are now resumed.") | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains hidden or 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,102 @@ | ||
package cli_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/coder/v2/coderd/coderdtest" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
||
func TestNotifications(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
command string | ||
expectPaused bool | ||
}{ | ||
{ | ||
name: "PauseNotifications", | ||
command: "pause", | ||
expectPaused: true, | ||
}, | ||
{ | ||
name: "ResumeNotifications", | ||
command: "resume", | ||
expectPaused: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// given | ||
ownerClient, db := coderdtest.NewWithDatabase(t, nil) | ||
_ = coderdtest.CreateFirstUser(t, ownerClient) | ||
|
||
// when | ||
inv, root := clitest.New(t, "notifications", tt.command) | ||
clitest.SetupConfig(t, ownerClient, root) | ||
|
||
var buf bytes.Buffer | ||
inv.Stdout = &buf | ||
err := inv.Run() | ||
require.NoError(t, err) | ||
|
||
// then | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
t.Cleanup(cancel) | ||
settingsJSON, err := db.GetNotificationsSettings(ctx) | ||
require.NoError(t, err) | ||
|
||
var settings codersdk.NotificationsSettings | ||
err = json.Unmarshal([]byte(settingsJSON), &settings) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expectPaused, settings.NotifierPaused) | ||
}) | ||
} | ||
} | ||
|
||
func TestPauseNotifications_RegularUser(t *testing.T) { | ||
t.Parallel() | ||
|
||
// given | ||
ownerClient, db := coderdtest.NewWithDatabase(t, nil) | ||
owner := coderdtest.CreateFirstUser(t, ownerClient) | ||
anotherClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID) | ||
|
||
// when | ||
inv, root := clitest.New(t, "notifications", "pause") | ||
clitest.SetupConfig(t, anotherClient, root) | ||
|
||
var buf bytes.Buffer | ||
inv.Stdout = &buf | ||
err := inv.Run() | ||
var sdkError *codersdk.Error | ||
require.Error(t, err) | ||
require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error") | ||
assert.Equal(t, http.StatusForbidden, sdkError.StatusCode()) | ||
assert.Contains(t, sdkError.Message, "Insufficient permissions to update notifications settings.") | ||
|
||
// then | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
t.Cleanup(cancel) | ||
settingsJSON, err := db.GetNotificationsSettings(ctx) | ||
require.NoError(t, err) | ||
|
||
var settings codersdk.NotificationsSettings | ||
err = json.Unmarshal([]byte(settingsJSON), &settings) | ||
require.NoError(t, err) | ||
require.False(t, settings.NotifierPaused) // still running | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
coder v0.0.0-devel | ||
|
||
USAGE: | ||
coder notifications | ||
|
||
Manage Coder notifications | ||
|
||
Aliases: notification | ||
|
||
Administrators can use these commands to change notification settings. | ||
- Pause Coder notifications. Administrators can temporarily stop notifiers | ||
from | ||
dispatching messages in case of the target outage (for example: unavailable | ||
SMTP | ||
server or Webhook not responding).: | ||
|
||
$ coder notifications pause | ||
|
||
- Resume Coder notifications: | ||
|
||
$ coder notifications resume | ||
|
||
SUBCOMMANDS: | ||
pause Pause notifications | ||
resume Resume notifications | ||
|
||
——— | ||
Run `coder --help` for a list of global options. |
This file contains hidden or 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,9 @@ | ||
coder v0.0.0-devel | ||
|
||
USAGE: | ||
coder notifications pause | ||
|
||
Pause notifications | ||
|
||
——— | ||
Run `coder --help` for a list of global options. |
This file contains hidden or 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,9 @@ | ||
coder v0.0.0-devel | ||
|
||
USAGE: | ||
coder notifications resume | ||
|
||
Resume notifications | ||
|
||
——— | ||
Run `coder --help` for a list of global options. |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note for the future: this is not necessary as of Go 1.22 🎉