Skip to content

Commit 443e218

Browse files
authored
feat: indicate when workspace builds are stopped/started by Coder (#5813)
* feat: indicate when workspace_builds are stopped/started by Coder * added translattion * added json tags and adjust type
1 parent 882832c commit 443e218

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

coderd/audit.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit
180180
}
181181

182182
type AdditionalFields struct {
183-
WorkspaceName string
184-
BuildNumber string
183+
WorkspaceName string `json:"workspace_name"`
184+
BuildNumber string `json:"build_number"`
185+
BuildReason database.BuildReason `json:"build_reason"`
185186
}
186187

187188
func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
@@ -219,6 +220,7 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
219220
resourceInfo := map[string]string{
220221
"workspaceName": "unknown",
221222
"buildNumber": "unknown",
223+
"buildReason": "unknown",
222224
}
223225
dblog.AdditionalFields, err = json.Marshal(resourceInfo)
224226
api.Logger.Error(ctx, "marshal additional fields", slog.Error(err))
@@ -262,8 +264,8 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow, additionalFields A
262264
)
263265

264266
// Strings for starting/stopping workspace builds follow the below format:
265-
// "{user} started build #{build_number} for workspace {target}"
266-
// where target is a workspace instead of a workspace build
267+
// "{user | 'Coder automatically'} started build #{build_number} for workspace {target}"
268+
// where target is a workspace (name) instead of a workspace build
267269
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
268270
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete {
269271
if len(additionalFields.BuildNumber) == 0 {

coderd/provisionerdserver/provisionerdserver.go

+2
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p
550550
buildResourceInfo := map[string]string{
551551
"workspaceName": workspace.Name,
552552
"buildNumber": strconv.FormatInt(int64(build.BuildNumber), 10),
553+
"buildReason": fmt.Sprintf("%v", build.Reason),
553554
}
554555

555556
wriBytes, err := json.Marshal(buildResourceInfo)
@@ -799,6 +800,7 @@ func (server *Server) CompleteJob(ctx context.Context, completed *proto.Complete
799800
buildResourceInfo := map[string]string{
800801
"workspaceName": workspace.Name,
801802
"buildNumber": strconv.FormatInt(int64(workspaceBuild.BuildNumber), 10),
803+
"buildReason": fmt.Sprintf("%v", workspaceBuild.Reason),
802804
}
803805

804806
wriBytes, err := json.Marshal(buildResourceInfo)

site/src/components/AuditLogRow/AuditLogDescription.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
1212
const { t } = i18next
1313

1414
let target = auditLog.resource_target.trim()
15+
let user = auditLog.user?.username.trim()
1516

16-
// audit logs with a resource_type of workspace build use workspace name as a target
1717
if (auditLog.resource_type === "workspace_build") {
18+
// audit logs with a resource_type of workspace build use workspace name as a target
1819
target = auditLog.additional_fields.workspaceName.trim()
20+
// workspaces can be started/stopped by a user, or kicked off automatically by Coder
21+
user =
22+
auditLog.additional_fields.buildReason &&
23+
auditLog.additional_fields.buildReason !== "initiator"
24+
? t("auditLog:table.logRow.buildReason")
25+
: auditLog.user?.username.trim()
1926
}
2027

2128
// SSH key entries have no links
@@ -30,7 +37,7 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
3037
}
3138

3239
const truncatedDescription = auditLog.description
33-
.replace("{user}", `${auditLog.user?.username.trim()}`)
40+
.replace("{user}", `${user}`)
3441
.replace("{target}", "")
3542

3643
return (

site/src/i18n/en/auditLog.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"os": "OS: ",
1414
"browser": "Browser: ",
1515
"notAvailable": "Not available",
16-
"onBehalfOf": " on behalf of {{owner}}"
16+
"onBehalfOf": " on behalf of {{owner}}",
17+
"buildReason": "Coder automatically"
1718
}
1819
},
1920
"paywall": {

0 commit comments

Comments
 (0)