Skip to content

Commit d5eb06a

Browse files
committed
added deleted label
1 parent 709e42e commit d5eb06a

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

coderd/audit.go

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,40 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
199199
Diff: diff,
200200
StatusCode: dblog.StatusCode,
201201
AdditionalFields: dblog.AdditionalFields,
202-
Description: auditLogDescription(dblog),
203202
User: user,
203+
Description: auditLogDescription(dblog),
204204
ResourceLink: api.auditLogResourceLink(ctx, dblog),
205+
IsDeleted: api.auditLogIsResourceDeleted(ctx, dblog),
206+
}
207+
}
208+
209+
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
210+
str := fmt.Sprintf("{user} %s",
211+
codersdk.AuditAction(alog.Action).FriendlyString(),
212+
)
213+
214+
// Strings for starting/stopping workspace builds follow the below format:
215+
// "{user} started build for workspace {target}"
216+
// where target is a workspace instead of a workspace build
217+
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
218+
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete {
219+
str += " build for"
220+
}
221+
222+
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
223+
// make too much sense to display.
224+
if alog.ResourceType == database.ResourceTypeGitSshKey {
225+
str += fmt.Sprintf(" the %s",
226+
codersdk.ResourceType(alog.ResourceType).FriendlyString())
227+
return str
205228
}
229+
230+
str += fmt.Sprintf(" %s",
231+
codersdk.ResourceType(alog.ResourceType).FriendlyString())
232+
233+
str += " {target}"
234+
235+
return str
206236
}
207237

208238
type AdditionalFields struct {
@@ -216,19 +246,19 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
216246
case database.ResourceTypeTemplate:
217247
template, err := api.Database.GetTemplateByID(ctx, alog.ResourceID)
218248
if err != nil {
219-
api.Logger.Error(ctx, "could not get template", slog.Error(err))
249+
api.Logger.Error(ctx, "could not fetch template", slog.Error(err))
220250
}
221251
return template.Deleted
222252
case database.ResourceTypeUser:
223253
user, err := api.Database.GetUserByID(ctx, alog.ResourceID)
224254
if err != nil {
225-
api.Logger.Error(ctx, "could not get user", slog.Error(err))
255+
api.Logger.Error(ctx, "could not fetch user", slog.Error(err))
226256
}
227257
return user.Deleted
228258
case database.ResourceTypeWorkspace:
229259
workspace, err := api.Database.GetWorkspaceByID(ctx, alog.ResourceID)
230260
if err != nil {
231-
api.Logger.Error(ctx, "could not get workspace", slog.Error(err))
261+
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err))
232262
}
233263
return workspace.Deleted
234264
case database.ResourceTypeWorkspaceBuild:
@@ -245,7 +275,7 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
245275
// We use workspace as a proxy for workspace build here
246276
workspace, err := api.Database.GetWorkspaceByID(ctx, uuid.MustParse(additionalFields.WorkspaceID))
247277
if err != nil {
248-
api.Logger.Error(ctx, "could not get workspace", slog.Error(err))
278+
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err))
249279
}
250280
return workspace.Deleted
251281
default:
@@ -254,29 +284,21 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
254284
}
255285

256286
func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAuditLogsOffsetRow) string {
287+
if api.auditLogIsResourceDeleted(ctx, alog) {
288+
return ""
289+
}
290+
257291
switch alog.ResourceType {
258292
case database.ResourceTypeTemplate:
259-
if api.auditLogIsResourceDeleted(ctx, alog) {
260-
return ""
261-
}
262293
return fmt.Sprintf("/templates/%s",
263294
alog.ResourceTarget)
264295
case database.ResourceTypeUser:
265-
if api.auditLogIsResourceDeleted(ctx, alog) {
266-
return ""
267-
}
268296
return fmt.Sprintf("/users?filter=%s",
269297
alog.ResourceTarget)
270298
case database.ResourceTypeWorkspace:
271-
if api.auditLogIsResourceDeleted(ctx, alog) {
272-
return ""
273-
}
274299
return fmt.Sprintf("/@%s/%s",
275300
alog.UserUsername.String, alog.ResourceTarget)
276301
case database.ResourceTypeWorkspaceBuild:
277-
if api.auditLogIsResourceDeleted(ctx, alog) {
278-
return ""
279-
}
280302
additionalFieldsBytes := []byte(alog.AdditionalFields)
281303
var additionalFields AdditionalFields
282304
err := json.Unmarshal(additionalFieldsBytes, &additionalFields)
@@ -290,35 +312,6 @@ func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAudit
290312
}
291313
}
292314

293-
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
294-
str := fmt.Sprintf("{user} %s",
295-
codersdk.AuditAction(alog.Action).FriendlyString(),
296-
)
297-
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
301-
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
302-
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete {
303-
str += " build for"
304-
}
305-
306-
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
307-
// make too much sense to display.
308-
if alog.ResourceType == database.ResourceTypeGitSshKey {
309-
str += fmt.Sprintf(" the %s",
310-
codersdk.ResourceType(alog.ResourceType).FriendlyString())
311-
return str
312-
}
313-
314-
str += fmt.Sprintf(" %s",
315-
codersdk.ResourceType(alog.ResourceType).FriendlyString())
316-
317-
str += " {target}"
318-
319-
return str
320-
}
321-
322315
// auditSearchQuery takes a query string and returns the auditLog filter.
323316
// It also can return the list of validation errors to return to the api.
324317
func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []codersdk.ValidationError) {

codersdk/audit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type AuditLog struct {
105105
AdditionalFields json.RawMessage `json:"additional_fields"`
106106
Description string `json:"description"`
107107
ResourceLink string `json:"resource_link"`
108+
IsDeleted bool `json:"is_deleted"`
108109

109110
User *User `json:"user"`
110111
}

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export interface AuditLog {
6868
readonly additional_fields: Record<string, string>
6969
readonly description: string
7070
readonly resource_link: string
71+
readonly is_deleted: boolean
7172
readonly user?: User
7273
}
7374

site/src/components/AuditLogRow/AuditLogDescription.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ import { FC } from "react"
22
import { AuditLog } from "api/typesGenerated"
33
import { Link as RouterLink } from "react-router-dom"
44
import Link from "@material-ui/core/Link"
5+
import { makeStyles } from "@material-ui/core/styles"
6+
import i18next from "i18next"
57

68
export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
79
auditLog,
810
}): JSX.Element => {
11+
const classes = useStyles()
12+
const { t } = i18next
13+
914
let target = auditLog.resource_target.trim()
1015

1116
// audit logs with a resource_type of workspace build use workspace name as a target
@@ -41,6 +46,18 @@ export const AuditLogDescription: FC<{ auditLog: AuditLog }> = ({
4146
) : (
4247
<strong>{target}</strong>
4348
)}
49+
{auditLog.is_deleted && (
50+
<span className={classes.deletedLabel}>
51+
<> {t("auditLog:table.logRow.deletedLabel")}</>
52+
</span>
53+
)}
4454
</span>
4555
)
4656
}
57+
58+
const useStyles = makeStyles((theme) => ({
59+
deletedLabel: {
60+
...theme.typography.caption,
61+
color: theme.palette.text.secondary,
62+
},
63+
}))

site/src/i18n/en/auditLog.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"emptyPage": "No audit logs available on this page",
99
"noLogs": "No audit logs available",
1010
"logRow": {
11+
"deletedLabel": "(deleted)",
1112
"ip": "IP: ",
1213
"os": "OS: ",
1314
"browser": "Browser: ",

0 commit comments

Comments
 (0)