Skip to content

fix: Audit log human parse message and nullable diffs #3978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion site/src/components/AuditLogRow/AuditLogDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { colors } from "theme/colors"
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
import { combineClasses } from "util/combineClasses"

const getDiffValue = (value: number | string | boolean) => {
const getDiffValue = (value: number | string | boolean | null) => {
if (typeof value === "string") {
return `"${value}"`
}

if (!value) {
return `""`
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit confusing to default all null values to an empty string, when they could really be of any type. I'll try and update the diffs to make sure one of the values is always non-null so you'll be able to know the type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return null as well. I would avoid rendering custom "empty placeholders" based on value type because what would be an empty placeholder for numbers and booleans? 0 and false? Would not make too much sense to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think null works 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2022-09-09 at 13 36 32

return value.toString()
}

Expand Down
31 changes: 7 additions & 24 deletions site/src/components/AuditLogRow/AuditLogRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { CloseDropdown, OpenDropdown } from "components/DropdownArrows/DropdownA
import { Pill } from "components/Pill/Pill"
import { Stack } from "components/Stack/Stack"
import { UserAvatar } from "components/UserAvatar/UserAvatar"
import { t } from "i18next"
import { ComponentProps, useState } from "react"
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
import userAgentParser from "ua-parser-js"
Expand All @@ -26,26 +25,10 @@ const pillTypeByHttpStatus = (httpStatus: number): ComponentProps<typeof Pill>["
return "success"
}

const actionLabelByAction: Record<AuditLog["action"], string> = {
create: t("actions.create", { ns: "auditLog" }),
write: t("actions.write", { ns: "auditLog" }),
delete: t("actions.delete", { ns: "auditLog" }),
}

const resourceLabelByResourceType: Record<AuditLog["resource_type"], string> = {
organization: "organization",
template: "template",
template_version: "template version",
user: "user",
workspace: "workspace",
git_ssh_key: "git ssh key",
api_key: "api key",
}

const readableActionMessage = (auditLog: AuditLog) => {
return `${actionLabelByAction[auditLog.action]} ${
resourceLabelByResourceType[auditLog.resource_type]
}`
return auditLog.description
.replace("{user}", `<strong>${auditLog.user?.username}</strong>`)
.replace("{target}", `<strong>${auditLog.resource_target}</strong>`)
}

export interface AuditLogRowProps {
Expand Down Expand Up @@ -98,10 +81,10 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
avatarURL={auditLog.user?.avatar_url}
/>
<div>
<span className={styles.auditLogResume}>
<strong>{auditLog.user?.username}</strong> {readableActionMessage(auditLog)}{" "}
<strong>{auditLog.resource_target}</strong>
</span>
<span
className={styles.auditLogResume}
dangerouslySetInnerHTML={{ __html: readableActionMessage(auditLog) }}
/>
<span className={styles.auditLogTime}>{createDayString(auditLog.time)}</span>
</div>
</Stack>
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ export const MockAuditLog: TypesGen.AuditLog = {
diff: {},
status_code: 200,
additional_fields: "",
description: "Colin Adler updated the workspace bruno-dev",
description: "{user} updated workspace {target}",
user: MockUser,
}

Expand Down