From a6101507c8075e4c091fc1c6c389eb03bbec471a Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Fri, 20 Jan 2023 21:42:51 +0000 Subject: [PATCH 1/3] feat: indicate when workspace_builds are stopped/started by Coder --- coderd/audit.go | 6 ++++-- coderd/provisionerdserver/provisionerdserver.go | 2 ++ .../components/AuditLogRow/AuditLogDescription.tsx | 11 +++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/coderd/audit.go b/coderd/audit.go index 79aade193ec66..5b87b51c16a49 100644 --- a/coderd/audit.go +++ b/coderd/audit.go @@ -182,6 +182,7 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit type AdditionalFields struct { WorkspaceName string BuildNumber string + BuildReason string } 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 resourceInfo := map[string]string{ "workspaceName": "unknown", "buildNumber": "unknown", + "buildReason": "unknown", } dblog.AdditionalFields, err = json.Marshal(resourceInfo) api.Logger.Error(ctx, "marshal additional fields", slog.Error(err)) @@ -262,8 +264,8 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow, additionalFields A ) // Strings for starting/stopping workspace builds follow the below format: - // "{user} started build #{build_number} for workspace {target}" - // where target is a workspace instead of a workspace build + // "{user | 'Coder automatically'} started build #{build_number} for workspace {target}" + // where target is a workspace (name) instead of a workspace build // passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35 if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete { if len(additionalFields.BuildNumber) == 0 { diff --git a/coderd/provisionerdserver/provisionerdserver.go b/coderd/provisionerdserver/provisionerdserver.go index ebed06dd1b5bc..978fbd598c5ee 100644 --- a/coderd/provisionerdserver/provisionerdserver.go +++ b/coderd/provisionerdserver/provisionerdserver.go @@ -549,6 +549,7 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p buildResourceInfo := map[string]string{ "workspaceName": workspace.Name, "buildNumber": strconv.FormatInt(int64(build.BuildNumber), 10), + "buildReason": fmt.Sprintf("%v", build.Reason), } wriBytes, err := json.Marshal(buildResourceInfo) @@ -798,6 +799,7 @@ func (server *Server) CompleteJob(ctx context.Context, completed *proto.Complete buildResourceInfo := map[string]string{ "workspaceName": workspace.Name, "buildNumber": strconv.FormatInt(int64(workspaceBuild.BuildNumber), 10), + "buildReason": fmt.Sprintf("%v", workspaceBuild.Reason), } wriBytes, err := json.Marshal(buildResourceInfo) diff --git a/site/src/components/AuditLogRow/AuditLogDescription.tsx b/site/src/components/AuditLogRow/AuditLogDescription.tsx index 38382f96ff9a2..01d7792158ba6 100644 --- a/site/src/components/AuditLogRow/AuditLogDescription.tsx +++ b/site/src/components/AuditLogRow/AuditLogDescription.tsx @@ -12,10 +12,17 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({ const { t } = i18next let target = auditLog.resource_target.trim() + let user = auditLog.user?.username.trim() - // audit logs with a resource_type of workspace build use workspace name as a target if (auditLog.resource_type === "workspace_build") { + // audit logs with a resource_type of workspace build use workspace name as a target target = auditLog.additional_fields.workspaceName.trim() + // workspaces can be started/stopped by a user, or kicked off automatically by Coder + user = + auditLog.additional_fields.buildReason && + auditLog.additional_fields.buildReason !== "initiator" + ? "Coder automatically" + : auditLog.user?.username.trim() } // SSH key entries have no links @@ -30,7 +37,7 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({ } const truncatedDescription = auditLog.description - .replace("{user}", `${auditLog.user?.username.trim()}`) + .replace("{user}", `${user}`) .replace("{target}", "") return ( From d6da9ec0a82ac72709dbe5975d793086d835019f Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Fri, 20 Jan 2023 21:45:58 +0000 Subject: [PATCH 2/3] added translattion --- site/src/components/AuditLogRow/AuditLogDescription.tsx | 2 +- site/src/i18n/en/auditLog.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/site/src/components/AuditLogRow/AuditLogDescription.tsx b/site/src/components/AuditLogRow/AuditLogDescription.tsx index 01d7792158ba6..58eb4926b5814 100644 --- a/site/src/components/AuditLogRow/AuditLogDescription.tsx +++ b/site/src/components/AuditLogRow/AuditLogDescription.tsx @@ -21,7 +21,7 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({ user = auditLog.additional_fields.buildReason && auditLog.additional_fields.buildReason !== "initiator" - ? "Coder automatically" + ? t("auditLog:table.logRow.buildReason") : auditLog.user?.username.trim() } diff --git a/site/src/i18n/en/auditLog.json b/site/src/i18n/en/auditLog.json index d4cd519a688a0..c0b9c5864d97a 100644 --- a/site/src/i18n/en/auditLog.json +++ b/site/src/i18n/en/auditLog.json @@ -13,7 +13,8 @@ "os": "OS: ", "browser": "Browser: ", "notAvailable": "Not available", - "onBehalfOf": " on behalf of {{owner}}" + "onBehalfOf": " on behalf of {{owner}}", + "buildReason": "Coder automatically" } }, "paywall": { From bcd460f841a7934c92f9cafe63203b593484e67d Mon Sep 17 00:00:00 2001 From: Kira Pilot Date: Mon, 23 Jan 2023 20:30:03 +0000 Subject: [PATCH 3/3] added json tags and adjust type --- coderd/audit.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/audit.go b/coderd/audit.go index 209517432b174..ba29ace95bdfb 100644 --- a/coderd/audit.go +++ b/coderd/audit.go @@ -180,9 +180,9 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit } type AdditionalFields struct { - WorkspaceName string - BuildNumber string - BuildReason string + WorkspaceName string `json:"workspace_name"` + BuildNumber string `json:"build_number"` + BuildReason database.BuildReason `json:"build_reason"` } func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {