@@ -199,10 +199,40 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
199
199
Diff : diff ,
200
200
StatusCode : dblog .StatusCode ,
201
201
AdditionalFields : dblog .AdditionalFields ,
202
- Description : auditLogDescription (dblog ),
203
202
User : user ,
203
+ Description : auditLogDescription (dblog ),
204
204
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
205
228
}
229
+
230
+ str += fmt .Sprintf (" %s" ,
231
+ codersdk .ResourceType (alog .ResourceType ).FriendlyString ())
232
+
233
+ str += " {target}"
234
+
235
+ return str
206
236
}
207
237
208
238
type AdditionalFields struct {
@@ -216,19 +246,19 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
216
246
case database .ResourceTypeTemplate :
217
247
template , err := api .Database .GetTemplateByID (ctx , alog .ResourceID )
218
248
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 ))
220
250
}
221
251
return template .Deleted
222
252
case database .ResourceTypeUser :
223
253
user , err := api .Database .GetUserByID (ctx , alog .ResourceID )
224
254
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 ))
226
256
}
227
257
return user .Deleted
228
258
case database .ResourceTypeWorkspace :
229
259
workspace , err := api .Database .GetWorkspaceByID (ctx , alog .ResourceID )
230
260
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 ))
232
262
}
233
263
return workspace .Deleted
234
264
case database .ResourceTypeWorkspaceBuild :
@@ -245,7 +275,7 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
245
275
// We use workspace as a proxy for workspace build here
246
276
workspace , err := api .Database .GetWorkspaceByID (ctx , uuid .MustParse (additionalFields .WorkspaceID ))
247
277
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 ))
249
279
}
250
280
return workspace .Deleted
251
281
default :
@@ -254,29 +284,21 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
254
284
}
255
285
256
286
func (api * API ) auditLogResourceLink (ctx context.Context , alog database.GetAuditLogsOffsetRow ) string {
287
+ if api .auditLogIsResourceDeleted (ctx , alog ) {
288
+ return ""
289
+ }
290
+
257
291
switch alog .ResourceType {
258
292
case database .ResourceTypeTemplate :
259
- if api .auditLogIsResourceDeleted (ctx , alog ) {
260
- return ""
261
- }
262
293
return fmt .Sprintf ("/templates/%s" ,
263
294
alog .ResourceTarget )
264
295
case database .ResourceTypeUser :
265
- if api .auditLogIsResourceDeleted (ctx , alog ) {
266
- return ""
267
- }
268
296
return fmt .Sprintf ("/users?filter=%s" ,
269
297
alog .ResourceTarget )
270
298
case database .ResourceTypeWorkspace :
271
- if api .auditLogIsResourceDeleted (ctx , alog ) {
272
- return ""
273
- }
274
299
return fmt .Sprintf ("/@%s/%s" ,
275
300
alog .UserUsername .String , alog .ResourceTarget )
276
301
case database .ResourceTypeWorkspaceBuild :
277
- if api .auditLogIsResourceDeleted (ctx , alog ) {
278
- return ""
279
- }
280
302
additionalFieldsBytes := []byte (alog .AdditionalFields )
281
303
var additionalFields AdditionalFields
282
304
err := json .Unmarshal (additionalFieldsBytes , & additionalFields )
@@ -290,35 +312,6 @@ func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAudit
290
312
}
291
313
}
292
314
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
-
322
315
// auditSearchQuery takes a query string and returns the auditLog filter.
323
316
// It also can return the list of validation errors to return to the api.
324
317
func auditSearchQuery (query string ) (database.GetAuditLogsOffsetParams , []codersdk.ValidationError ) {
0 commit comments