MCPcopy Index your code
hub / github.com/coder/coder / notifyWorkspaceCreated

Method notifyWorkspaceCreated

coderd/workspaces.go:1048–1106  ·  view source on GitHub ↗
(
	ctx context.Context,
	receiverID uuid.UUID,
	workspace database.Workspace,
	parameters []codersdk.WorkspaceBuildParameter,
)

Source from the content-addressed store, hash-verified

1046}
1047
1048func (api *API) notifyWorkspaceCreated(
1049 ctx context.Context,
1050 receiverID uuid.UUID,
1051 workspace database.Workspace,
1052 parameters []codersdk.WorkspaceBuildParameter,
1053) {
1054 log := api.Logger.With(slog.F("workspace_id", workspace.ID))
1055
1056 template, err := api.Database.GetTemplateByID(ctx, workspace.TemplateID)
1057 if err != nil {
1058 log.Warn(ctx, "failed to fetch template for workspace creation notification", slog.F("template_id", workspace.TemplateID), slog.Error(err))
1059 return
1060 }
1061
1062 owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
1063 if err != nil {
1064 log.Warn(ctx, "failed to fetch user for workspace creation notification", slog.F("owner_id", workspace.OwnerID), slog.Error(err))
1065 return
1066 }
1067
1068 version, err := api.Database.GetTemplateVersionByID(ctx, template.ActiveVersionID)
1069 if err != nil {
1070 log.Warn(ctx, "failed to fetch template version for workspace creation notification", slog.F("template_version_id", template.ActiveVersionID), slog.Error(err))
1071 return
1072 }
1073
1074 buildParameters := make([]map[string]any, len(parameters))
1075 for idx, parameter := range parameters {
1076 buildParameters[idx] = map[string]any{
1077 "name": parameter.Name,
1078 "value": parameter.Value,
1079 }
1080 }
1081
1082 if _, err := api.NotificationsEnqueuer.EnqueueWithData(
1083 // nolint:gocritic // Need notifier actor to enqueue notifications
1084 dbauthz.AsNotifier(ctx),
1085 receiverID,
1086 notifications.TemplateWorkspaceCreated,
1087 map[string]string{
1088 "workspace": workspace.Name,
1089 "template": template.Name,
1090 "version": version.Name,
1091 "workspace_owner_username": owner.Username,
1092 },
1093 map[string]any{
1094 "workspace": map[string]any{"id": workspace.ID, "name": workspace.Name},
1095 "template": map[string]any{"id": template.ID, "name": template.Name},
1096 "template_version": map[string]any{"id": version.ID, "name": version.Name},
1097 "owner": map[string]any{"id": owner.ID, "name": owner.Name, "email": owner.Email},
1098 "parameters": buildParameters,
1099 },
1100 "api-workspaces-create",
1101 // Associate this notification with all the related entities
1102 workspace.ID, workspace.OwnerID, workspace.TemplateID, workspace.OrganizationID,
1103 ); err != nil {
1104 log.Warn(ctx, "failed to notify of workspace creation", slog.Error(err))
1105 }

Callers 1

createWorkspaceFunction · 0.80

Calls 6

AsNotifierFunction · 0.92
GetTemplateByIDMethod · 0.65
GetUserByIDMethod · 0.65
EnqueueWithDataMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected