Skip to content

Commit 346583f

Browse files
fix: Audit log human parse message and nullable diffs (coder#3978)
* fix: Audit log human parse message and nullable diffs * Fix diff values
1 parent abb804f commit 346583f

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

site/src/components/AuditLogRow/AuditLogDiff.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import { colors } from "theme/colors"
44
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
55
import { combineClasses } from "util/combineClasses"
66

7-
const getDiffValue = (value: number | string | boolean) => {
7+
const getDiffValue = (value: unknown): string => {
88
if (typeof value === "string") {
99
return `"${value}"`
1010
}
1111

12+
if (Array.isArray(value)) {
13+
const values = value.map((v) => getDiffValue(v))
14+
return `[${values.join(", ")}]`
15+
}
16+
17+
if (value === null || value === undefined) {
18+
return "null"
19+
}
20+
1221
return value.toString()
1322
}
1423

site/src/components/AuditLogRow/AuditLogRow.tsx

+7-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { CloseDropdown, OpenDropdown } from "components/DropdownArrows/DropdownA
77
import { Pill } from "components/Pill/Pill"
88
import { Stack } from "components/Stack/Stack"
99
import { UserAvatar } from "components/UserAvatar/UserAvatar"
10-
import { t } from "i18next"
1110
import { ComponentProps, useState } from "react"
1211
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
1312
import userAgentParser from "ua-parser-js"
@@ -26,26 +25,10 @@ const pillTypeByHttpStatus = (httpStatus: number): ComponentProps<typeof Pill>["
2625
return "success"
2726
}
2827

29-
const actionLabelByAction: Record<AuditLog["action"], string> = {
30-
create: t("actions.create", { ns: "auditLog" }),
31-
write: t("actions.write", { ns: "auditLog" }),
32-
delete: t("actions.delete", { ns: "auditLog" }),
33-
}
34-
35-
const resourceLabelByResourceType: Record<AuditLog["resource_type"], string> = {
36-
organization: "organization",
37-
template: "template",
38-
template_version: "template version",
39-
user: "user",
40-
workspace: "workspace",
41-
git_ssh_key: "git ssh key",
42-
api_key: "api key",
43-
}
44-
4528
const readableActionMessage = (auditLog: AuditLog) => {
46-
return `${actionLabelByAction[auditLog.action]} ${
47-
resourceLabelByResourceType[auditLog.resource_type]
48-
}`
29+
return auditLog.description
30+
.replace("{user}", `<strong>${auditLog.user?.username}</strong>`)
31+
.replace("{target}", `<strong>${auditLog.resource_target}</strong>`)
4932
}
5033

5134
export interface AuditLogRowProps {
@@ -98,10 +81,10 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
9881
avatarURL={auditLog.user?.avatar_url}
9982
/>
10083
<div>
101-
<span className={styles.auditLogResume}>
102-
<strong>{auditLog.user?.username}</strong> {readableActionMessage(auditLog)}{" "}
103-
<strong>{auditLog.resource_target}</strong>
104-
</span>
84+
<span
85+
className={styles.auditLogResume}
86+
dangerouslySetInnerHTML={{ __html: readableActionMessage(auditLog) }}
87+
/>
10588
<span className={styles.auditLogTime}>{createDayString(auditLog.time)}</span>
10689
</div>
10790
</Stack>

site/src/testHelpers/entities.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ export const MockAuditLog: TypesGen.AuditLog = {
775775
diff: {},
776776
status_code: 200,
777777
additional_fields: "",
778-
description: "Colin Adler updated the workspace bruno-dev",
778+
description: "{user} updated workspace {target}",
779779
user: MockUser,
780780
}
781781

@@ -799,5 +799,10 @@ export const MockAuditLog2: TypesGen.AuditLog = {
799799
new: "53bded77-7b9d-4e82-8771-991a34d759f9",
800800
secret: false,
801801
},
802+
roles: {
803+
old: null,
804+
new: ["admin", "auditor"],
805+
secret: false,
806+
},
802807
},
803808
}

0 commit comments

Comments
 (0)