Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 450db4d

Browse files
committed
feat: notify owner about failed autobuild
1 parent b00f746 commit 450db4d

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM notification_templates WHERE id = '381df2a9-c0c0-4749-420f-80a9280c66f9');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions)
2+
VALUES ('381df2a9-c0c0-4749-420f-80a9280c66f9', 'Workspace Autobuild Failed', E'Workspace "{{.Labels.name}}" deleted',
3+
E'Hi {{.UserName}}\n\Automatic build of your workspace **{{.Labels.name}}** failed.\nThe specified reason was "**{{.Labels.reason}}**".',
4+
'Workspace Events', '[
5+
{
6+
"label": "View workspaces",
7+
"url": "{{ base_url }}/workspaces"
8+
}
9+
]'::jsonb);

coderd/notifications/events.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ import "github.com/google/uuid"
66
// TODO: autogenerate these.
77

88
// Workspace-related events.
9-
var TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
9+
var (
10+
TemplateWorkspaceDeleted = uuid.MustParse("f517da0b-cdc9-410f-ab89-a86107c420ed")
11+
WorkspaceAutobuildFailed = uuid.MustParse("381df2a9-c0c0-4749-420f-80a9280c66f9")
12+
)

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,18 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
982982
}
983983

984984
var build database.WorkspaceBuild
985+
var workspace database.Workspace
985986
err = s.Database.InTx(func(db database.Store) error {
986987
build, err = db.GetWorkspaceBuildByID(ctx, input.WorkspaceBuildID)
987988
if err != nil {
988989
return xerrors.Errorf("get workspace build: %w", err)
989990
}
990991

992+
workspace, err = db.GetWorkspaceByID(ctx, build.WorkspaceID)
993+
if err != nil {
994+
return xerrors.Errorf("get workspace: %w", err)
995+
}
996+
991997
if jobType.WorkspaceBuild.State != nil {
992998
err = db.UpdateWorkspaceBuildProvisionerStateByID(ctx, database.UpdateWorkspaceBuildProvisionerStateByIDParams{
993999
ID: input.WorkspaceBuildID,
@@ -1014,6 +1020,8 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
10141020
return nil, err
10151021
}
10161022

1023+
s.notifyWorkspaceBuildFailed(ctx, workspace, build)
1024+
10171025
err = s.Pubsub.Publish(codersdk.WorkspaceNotifyChannel(build.WorkspaceID), []byte{})
10181026
if err != nil {
10191027
return nil, xerrors.Errorf("update workspace: %w", err)
@@ -1087,6 +1095,25 @@ func (s *server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*proto.
10871095
return &proto.Empty{}, nil
10881096
}
10891097

1098+
func (s *server) notifyWorkspaceBuildFailed(ctx context.Context, workspace database.Workspace, build database.WorkspaceBuild) {
1099+
var reason string
1100+
if build.Reason.Valid() && build.Reason == database.BuildReasonInitiator {
1101+
return // failed workspace builds initiated by a user should not notify
1102+
}
1103+
1104+
if _, err := s.NotificationEnqueuer.Enqueue(ctx, workspace.OwnerID, notifications.WorkspaceAutobuildFailed,
1105+
map[string]string{
1106+
"name": workspace.Name,
1107+
"initiator": build.InitiatorByUsername,
1108+
"reason": reason,
1109+
}, "provisionerdserver",
1110+
// Associate this notification with all the related entities.
1111+
workspace.ID, workspace.OwnerID, workspace.TemplateID, workspace.OrganizationID,
1112+
); err != nil {
1113+
s.Logger.Warn(ctx, "failed to notify of failed workspace autobuild", slog.Error(err))
1114+
}
1115+
}
1116+
10901117
// CompleteJob is triggered by a provision daemon to mark a provisioner job as completed.
10911118
func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob) (*proto.Empty, error) {
10921119
ctx, span := s.startTrace(ctx, tracing.FuncName())

0 commit comments

Comments
 (0)