Skip to content

feat: add notification dispatch methods endpoint #14150

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ func New(options *Options) *API {
r.Route("/templates", func(r chi.Router) {
r.Get("/system", api.systemNotificationTemplates)
})
r.Get("/dispatch-methods", api.notificationDispatchMethods)
})
})

Expand Down
19 changes: 19 additions & 0 deletions coderd/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ func (api *API) systemNotificationTemplates(rw http.ResponseWriter, r *http.Requ
httpapi.Write(r.Context(), rw, http.StatusOK, out)
}

// @Summary Get notification dispatch methods
// @ID get-notification-dispatch-methods
// @Security CoderSessionToken
// @Produce json
// @Tags Notifications
// @Success 200 {array} codersdk.NotificationMethodsResponse
// @Router /notifications/dispatch-methods [get]
func (api *API) notificationDispatchMethods(rw http.ResponseWriter, r *http.Request) {
var methods []string
for _, nm := range database.AllNotificationMethodValues() {
methods = append(methods, string(nm))
}

httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.NotificationMethodsResponse{
AvailableNotificationMethods: methods,
DefaultNotificationMethod: api.DeploymentValues.Notifications.Method.Value(),
})
}

// @Summary Get user notification preferences
// @ID get-user-notification-preferences
// @Security CoderSessionToken
Expand Down
53 changes: 53 additions & 0 deletions coderd/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"

"github.com/coder/serpent"

"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/notifications"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
Expand Down Expand Up @@ -265,3 +269,52 @@ func TestNotificationPreferences(t *testing.T) {
require.True(t, found, "dormant notification preference was not found")
})
}

func TestNotificationDispatchMethods(t *testing.T) {
t.Parallel()

defaultOpts := createOpts(t)
webhookOpts := createOpts(t)
webhookOpts.DeploymentValues.Notifications.Method = serpent.String(database.NotificationMethodWebhook)

tests := []struct {
name string
opts *coderdtest.Options
expectedDefault string
}{
{
name: "default",
opts: defaultOpts,
expectedDefault: string(database.NotificationMethodSmtp),
},
{
name: "non-default",
opts: webhookOpts,
expectedDefault: string(database.NotificationMethodWebhook),
},
}

var allMethods []string
for _, nm := range database.AllNotificationMethodValues() {
allMethods = append(allMethods, string(nm))
}
slices.Sort(allMethods)

// nolint:paralleltest // Not since Go v1.22.
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitShort)
api := coderdtest.New(t, tc.opts)
_ = coderdtest.CreateFirstUser(t, api)

resp, err := api.GetNotificationDispatchMethods(ctx)
require.NoError(t, err)

slices.Sort(resp.AvailableNotificationMethods)
require.EqualValues(t, resp.AvailableNotificationMethods, allMethods)
require.Equal(t, tc.expectedDefault, resp.DefaultNotificationMethod)
})
}
}
30 changes: 30 additions & 0 deletions codersdk/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type NotificationTemplate struct {
Kind string `json:"kind"`
}

type NotificationMethodsResponse struct {
AvailableNotificationMethods []string `json:"available"`
DefaultNotificationMethod string `json:"default"`
}

type NotificationPreference struct {
NotificationTemplateID uuid.UUID `json:"id" format:"uuid"`
Disabled bool `json:"disabled"`
Expand Down Expand Up @@ -162,6 +167,31 @@ func (c *Client) UpdateUserNotificationPreferences(ctx context.Context, userID u
return prefs, nil
}

// GetNotificationDispatchMethods the available and default notification dispatch methods.
func (c *Client) GetNotificationDispatchMethods(ctx context.Context) (NotificationMethodsResponse, error) {
res, err := c.Request(ctx, http.MethodGet, "/api/v2/notifications/dispatch-methods", nil)
if err != nil {
return NotificationMethodsResponse{}, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return NotificationMethodsResponse{}, ReadBodyAsError(res)
}

var resp NotificationMethodsResponse
body, err := io.ReadAll(res.Body)
if err != nil {
return NotificationMethodsResponse{}, xerrors.Errorf("read response body: %w", err)
}

if err := json.Unmarshal(body, &resp); err != nil {
return NotificationMethodsResponse{}, xerrors.Errorf("unmarshal response body: %w", err)
}

return resp, nil
}

type UpdateNotificationTemplateMethod struct {
Method string `json:"method,omitempty" example:"webhook"`
}
Expand Down
44 changes: 44 additions & 0 deletions docs/api/notifications.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions docs/api/schemas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading