Skip to content

Commit 709e42e

Browse files
committed
cleaned up string
1 parent ddc0dc0 commit 709e42e

File tree

6 files changed

+130
-60
lines changed

6 files changed

+130
-60
lines changed

coderd/audit.go

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,32 +207,84 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
207207

208208
type AdditionalFields struct {
209209
WorkspaceName string
210+
WorkspaceID string
210211
BuildNumber string
211212
}
212213

214+
func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.GetAuditLogsOffsetRow) bool {
215+
switch alog.ResourceType {
216+
case database.ResourceTypeTemplate:
217+
template, err := api.Database.GetTemplateByID(ctx, alog.ResourceID)
218+
if err != nil {
219+
api.Logger.Error(ctx, "could not get template", slog.Error(err))
220+
}
221+
return template.Deleted
222+
case database.ResourceTypeUser:
223+
user, err := api.Database.GetUserByID(ctx, alog.ResourceID)
224+
if err != nil {
225+
api.Logger.Error(ctx, "could not get user", slog.Error(err))
226+
}
227+
return user.Deleted
228+
case database.ResourceTypeWorkspace:
229+
workspace, err := api.Database.GetWorkspaceByID(ctx, alog.ResourceID)
230+
if err != nil {
231+
api.Logger.Error(ctx, "could not get workspace", slog.Error(err))
232+
}
233+
return workspace.Deleted
234+
case database.ResourceTypeWorkspaceBuild:
235+
additionalFieldsBytes := []byte(alog.AdditionalFields)
236+
var additionalFields AdditionalFields
237+
err := json.Unmarshal(additionalFieldsBytes, &additionalFields)
238+
if err != nil {
239+
api.Logger.Error(ctx, "could not unmarshal workspace ID", slog.Error(err))
240+
}
241+
// if we don't have a WorkspaceID, we return true so as to hide the link in the UI
242+
if len(additionalFields.WorkspaceID) < 1 {
243+
return true
244+
}
245+
// We use workspace as a proxy for workspace build here
246+
workspace, err := api.Database.GetWorkspaceByID(ctx, uuid.MustParse(additionalFields.WorkspaceID))
247+
if err != nil {
248+
api.Logger.Error(ctx, "could not get workspace", slog.Error(err))
249+
}
250+
return workspace.Deleted
251+
default:
252+
return false
253+
}
254+
}
255+
213256
func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAuditLogsOffsetRow) string {
214257
switch alog.ResourceType {
215258
case database.ResourceTypeTemplate:
259+
if api.auditLogIsResourceDeleted(ctx, alog) {
260+
return ""
261+
}
216262
return fmt.Sprintf("/templates/%s",
217263
alog.ResourceTarget)
218264
case database.ResourceTypeUser:
265+
if api.auditLogIsResourceDeleted(ctx, alog) {
266+
return ""
267+
}
219268
return fmt.Sprintf("/users?filter=%s",
220269
alog.ResourceTarget)
221270
case database.ResourceTypeWorkspace:
271+
if api.auditLogIsResourceDeleted(ctx, alog) {
272+
return ""
273+
}
222274
return fmt.Sprintf("/@%s/%s",
223275
alog.UserUsername.String, alog.ResourceTarget)
224276
case database.ResourceTypeWorkspaceBuild:
277+
if api.auditLogIsResourceDeleted(ctx, alog) {
278+
return ""
279+
}
225280
additionalFieldsBytes := []byte(alog.AdditionalFields)
226281
var additionalFields AdditionalFields
227282
err := json.Unmarshal(additionalFieldsBytes, &additionalFields)
228283
if err != nil {
229-
api.Logger.Error(ctx, "could not unmarshal workspace name for friendly string", slog.Error(err))
284+
api.Logger.Error(ctx, "could not unmarshal workspace name", slog.Error(err))
230285
}
231286
return fmt.Sprintf("/@%s/%s/builds/%s",
232287
alog.UserUsername.String, additionalFields.WorkspaceName, additionalFields.BuildNumber)
233-
case database.ResourceTypeGroup:
234-
return fmt.Sprintf("/groups/%s",
235-
alog.ResourceID)
236288
default:
237289
return ""
238290
}
@@ -243,21 +295,27 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
243295
codersdk.AuditAction(alog.Action).FriendlyString(),
244296
)
245297

246-
// Strings for workspace_builds follow the below format:
247-
// "{user} started workspace build for {target}"
248-
// where target is a workspace instead of the workspace build,
298+
// Strings for starting/stopping workspace builds follow the below format:
299+
// "{user} started build for workspace {target}"
300+
// where target is a workspace instead of a workspace build
249301
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
250-
if alog.ResourceType != database.ResourceTypeWorkspaceBuild {
251-
str += fmt.Sprintf(" %s",
252-
codersdk.ResourceType(alog.ResourceType).FriendlyString())
302+
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete {
303+
str += " build for"
253304
}
254305

255-
// We don't display the name for git ssh keys. It's fairly long and doesn't
306+
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
256307
// make too much sense to display.
257-
if alog.ResourceType != database.ResourceTypeGitSshKey {
258-
str += " {target}"
308+
if alog.ResourceType == database.ResourceTypeGitSshKey {
309+
str += fmt.Sprintf(" the %s",
310+
codersdk.ResourceType(alog.ResourceType).FriendlyString())
311+
return str
259312
}
260313

314+
str += fmt.Sprintf(" %s",
315+
codersdk.ResourceType(alog.ResourceType).FriendlyString())
316+
317+
str += " {target}"
318+
261319
return str
262320
}
263321

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p
538538
if getWorkspaceErr != nil {
539539
server.Logger.Error(ctx, "failed to create audit log - get workspace err", slog.Error(err))
540540
} else {
541-
// We pass the workspace name to the Auditor so that it
542-
// can form a friendly string for the user.
541+
fmt.Println("Hello World! 1", workspace.ID.String())
542+
// We pass the below information to the Auditor so that it
543+
// can form a friendly string for the user to view in the UI.
543544
workspaceResourceInfo := map[string]string{
544545
"workspaceName": workspace.Name,
546+
"workspaceId": workspace.ID.String(),
545547
"buildNumber": strconv.FormatInt(int64(build.BuildNumber), 10),
546548
}
547549

@@ -753,11 +755,13 @@ func (server *Server) CompleteJob(ctx context.Context, completed *proto.Complete
753755
if getWorkspaceError == nil {
754756
auditor := server.Auditor.Load()
755757
auditAction := auditActionFromTransition(workspaceBuild.Transition)
758+
fmt.Println("Hello World! 2", workspace.ID.String())
756759

757-
// We pass the workspace name to the Auditor so that it
758-
// can form a friendly string for the user.
760+
// We pass the below information to the Auditor so that it
761+
// can form a friendly string for the user to view in the UI.
759762
workspaceResourceInfo := map[string]string{
760763
"workspaceName": workspace.Name,
764+
"workspaceId": workspace.ID.String(),
761765
"buildNumber": strconv.FormatInt(int64(workspaceBuild.BuildNumber), 10),
762766
}
763767

codersdk/audit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ func (r ResourceType) FriendlyString() string {
3838
case ResourceTypeWorkspace:
3939
return "workspace"
4040
case ResourceTypeWorkspaceBuild:
41-
return "workspace build"
41+
// workspace builds have a unique friendly string
42+
// see coderd/audit.go:298 for explanation
43+
return "workspace"
4244
case ResourceTypeGitSSHKey:
4345
return "git ssh key"
4446
case ResourceTypeAPIKey:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { FC } from "react"
2+
import { AuditLog } from "api/typesGenerated"
3+
import { Link as RouterLink } from "react-router-dom"
4+
import Link from "@material-ui/core/Link"
5+
6+
export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
7+
auditLog,
8+
}): JSX.Element => {
9+
let target = auditLog.resource_target.trim()
10+
11+
// audit logs with a resource_type of workspace build use workspace name as a target
12+
if (
13+
auditLog.resource_type === "workspace_build" &&
14+
auditLog.additional_fields.workspaceName
15+
) {
16+
target = auditLog.additional_fields.workspaceName.trim()
17+
}
18+
19+
// SSH key entries have no links
20+
if (auditLog.resource_type === "git_ssh_key") {
21+
return (
22+
<span>
23+
{auditLog.description
24+
.replace("{user}", `${auditLog.user?.username.trim()}`)
25+
.replace("{target}", `${target}`)}
26+
</span>
27+
)
28+
}
29+
30+
const truncatedDescription = auditLog.description
31+
.replace("{user}", `${auditLog.user?.username.trim()}`)
32+
.replace("{target}", "")
33+
34+
return (
35+
<span>
36+
{truncatedDescription}
37+
{auditLog.resource_link ? (
38+
<Link component={RouterLink} to={auditLog.resource_link}>
39+
<strong>{target}</strong>
40+
</Link>
41+
) : (
42+
<strong>{target}</strong>
43+
)}
44+
</span>
45+
)
46+
}

site/src/components/AuditLogRow/AuditLogRow.tsx

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,8 @@ import { PaletteIndex } from "theme/palettes"
1515
import userAgentParser from "ua-parser-js"
1616
import { combineClasses } from "util/combineClasses"
1717
import { AuditLogDiff } from "./AuditLogDiff"
18-
import { Link as RouterLink } from "react-router-dom"
1918
import i18next from "i18next"
20-
import Link from "@material-ui/core/Link"
21-
22-
const determineResourceLink = (auditLog: AuditLog): JSX.Element | undefined => {
23-
const { t } = i18next
24-
const linkTarget = auditLog.resource_target.trim()
25-
26-
if (auditLog.resource_type === "workspace_build") {
27-
return <>{t("auditLog:table.logRow.buildTarget")}</>
28-
} else if (auditLog.resource_type === "git_ssh_key") {
29-
return
30-
} else {
31-
return <strong>{linkTarget}</strong>
32-
}
33-
}
34-
35-
export const readableActionMessage = (auditLog: AuditLog): string => {
36-
return auditLog.description
37-
.replace("{user}", `${auditLog.user?.username.trim()}`)
38-
.replace("{target}", "")
39-
}
19+
import { AuditLogDescription } from "./AuditLogDescription"
4020

4121
const httpStatusColor = (httpStatus: number): PaletteIndex => {
4222
if (httpStatus >= 300 && httpStatus < 500) {
@@ -125,25 +105,7 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
125105
alignItems="baseline"
126106
spacing={1}
127107
>
128-
<span>
129-
{readableActionMessage(auditLog)}{" "}
130-
{auditLog.resource_link ? (
131-
<Link component={RouterLink} to={auditLog.resource_link}>
132-
{determineResourceLink(auditLog)}
133-
</Link>
134-
) : (
135-
<strong>{determineResourceLink(auditLog)}</strong>
136-
)}
137-
{auditLog.resource_type === "workspace_build" &&
138-
auditLog.additional_fields.workspaceName && (
139-
<>
140-
{t("auditLog:table.logRow.buildFriendlyString")}
141-
<strong>
142-
{auditLog.additional_fields.workspaceName}
143-
</strong>
144-
</>
145-
)}
146-
</span>
108+
<AuditLogDescription auditLog={auditLog} />
147109
<span className={styles.auditLogTime}>
148110
{new Date(auditLog.time).toLocaleTimeString()}
149111
</span>

site/src/i18n/en/auditLog.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
"emptyPage": "No audit logs available on this page",
99
"noLogs": "No audit logs available",
1010
"logRow": {
11-
"buildTarget": "build",
12-
"buildFriendlyString": " for workspace ",
1311
"ip": "IP: ",
1412
"os": "OS: ",
1513
"browser": "Browser: ",

0 commit comments

Comments
 (0)