feat: notify owner about failed autobuild (#13891)

This commit is contained in:
Marcin Tojek
2024-07-16 10:48:17 +02:00
committed by GitHub
parent 36454aa81b
commit a5e4bf38fe
8 changed files with 204 additions and 25 deletions
+17 -15
View File
@@ -80,7 +80,7 @@ func (s *StoreEnqueuer) Enqueue(ctx context.Context, userID, templateID uuid.UUI
// buildPayload creates the payload that the notification will for variable substitution and/or routing.
// The payload contains information about the recipient, the event that triggered the notification, and any subsequent
// actions which can be taken by the recipient.
func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, templateID uuid.UUID, labels map[string]string) (*types.MessagePayload, error) {
func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID, templateID uuid.UUID, labels map[string]string) (*types.MessagePayload, error) {
metadata, err := s.store.FetchNewMessageMetadata(ctx, database.FetchNewMessageMetadataParams{
UserID: userID,
NotificationTemplateID: templateID,
@@ -89,8 +89,21 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, temp
return nil, xerrors.Errorf("new message metadata: %w", err)
}
payload := types.MessagePayload{
Version: "1.0",
NotificationName: metadata.NotificationName,
UserID: metadata.UserID.String(),
UserEmail: metadata.UserEmail,
UserName: metadata.UserName,
Labels: labels,
// No actions yet
}
// Execute any templates in actions.
out, err := render.GoTemplate(string(metadata.Actions), types.MessagePayload{}, s.helpers)
out, err := render.GoTemplate(string(metadata.Actions), payload, s.helpers)
if err != nil {
return nil, xerrors.Errorf("render actions: %w", err)
}
@@ -100,19 +113,8 @@ func (s *StoreEnqueuer) buildPayload(ctx context.Context, userID uuid.UUID, temp
if err = json.Unmarshal(metadata.Actions, &actions); err != nil {
return nil, xerrors.Errorf("new message metadata: parse template actions: %w", err)
}
return &types.MessagePayload{
Version: "1.0",
NotificationName: metadata.NotificationName,
UserID: metadata.UserID.String(),
UserEmail: metadata.UserEmail,
UserName: metadata.UserName,
Actions: actions,
Labels: labels,
}, nil
payload.Actions = actions
return &payload, nil
}
// NoopEnqueuer implements the Enqueuer interface but performs a noop.
+4 -1
View File
@@ -6,4 +6,7 @@ import "github.com/google/uuid"
// TODO: autogenerate these.
// Workspace-related events.
var TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
var (
TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
WorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
)
+7 -4
View File
@@ -98,10 +98,11 @@ func TestBuildPayload(t *testing.T) {
// GIVEN: a set of helpers to be injected into the templates
const label = "Click here!"
const url = "http://xyz.com/"
const baseURL = "http://xyz.com"
const url = baseURL + "/@bobby/my-workspace"
helpers := map[string]any{
"my_label": func() string { return label },
"my_url": func() string { return url },
"my_url": func() string { return baseURL },
}
// GIVEN: an enqueue interceptor which returns mock metadata
@@ -112,7 +113,7 @@ func TestBuildPayload(t *testing.T) {
actions := []types.TemplateAction{
{
Label: "{{ my_label }}",
URL: "{{ my_url }}",
URL: "{{ my_url }}/@{{.UserName}}/{{.Labels.name}}",
},
}
out, err := json.Marshal(actions)
@@ -131,7 +132,9 @@ func TestBuildPayload(t *testing.T) {
require.NoError(t, err)
// WHEN: a notification is enqueued
_, err = enq.Enqueue(ctx, uuid.New(), notifications.TemplateWorkspaceDeleted, nil, "test")
_, err = enq.Enqueue(ctx, uuid.New(), notifications.TemplateWorkspaceDeleted, map[string]string{
"name": "my-workspace",
}, "test")
require.NoError(t, err)
// THEN: expect that a payload will be constructed and have the expected values
+20 -1
View File
@@ -38,6 +38,23 @@ func TestGoTemplate(t *testing.T) {
expectedOutput: userEmail,
expectedErr: nil,
},
{
name: "render workspace URL",
in: `[{
"label": "View workspace",
"url": "{{ base_url }}/@{{.UserName}}/{{.Labels.name}}"
}]`,
payload: types.MessagePayload{
UserName: "johndoe",
Labels: map[string]string{
"name": "my-workspace",
},
},
expectedOutput: `[{
"label": "View workspace",
"url": "https://mocked-server-address/@johndoe/my-workspace"
}]`,
},
}
for _, tc := range tests {
@@ -46,7 +63,9 @@ func TestGoTemplate(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out, err := render.GoTemplate(tc.in, tc.payload, nil)
out, err := render.GoTemplate(tc.in, tc.payload, map[string]any{
"base_url": func() string { return "https://mocked-server-address" },
})
if tc.expectedErr == nil {
require.NoError(t, err)
} else {