@@ -180,9 +180,8 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit
180
180
}
181
181
182
182
type AdditionalFields struct {
183
- WorkspaceName string
184
- BuildNumber string
185
- WorkspaceOwner string
183
+ WorkspaceName string
184
+ BuildNumber string
186
185
}
187
186
188
187
func (api * API ) convertAuditLog (ctx context.Context , dblog database.GetAuditLogsOffsetRow ) codersdk.AuditLog {
@@ -218,9 +217,8 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
218
217
if err != nil {
219
218
api .Logger .Error (ctx , "unmarshal additional fields" , slog .Error (err ))
220
219
resourceInfo := map [string ]string {
221
- "workspaceName" : "unknown" ,
222
- "buildNumber" : "unknown" ,
223
- "workspaceOwner" : "unknown" ,
220
+ "workspaceName" : "unknown" ,
221
+ "buildNumber" : "unknown" ,
224
222
}
225
223
dblog .AdditionalFields , err = json .Marshal (resourceInfo )
226
224
api .Logger .Error (ctx , "marshal additional fields" , slog .Error (err ))
@@ -233,7 +231,7 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
233
231
if isDeleted {
234
232
resourceLink = ""
235
233
} else {
236
- resourceLink = auditLogResourceLink (dblog , additionalFields )
234
+ resourceLink = api . auditLogResourceLink (ctx , dblog , additionalFields )
237
235
}
238
236
239
237
return codersdk.AuditLog {
@@ -343,7 +341,7 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
343
341
}
344
342
}
345
343
346
- func auditLogResourceLink (alog database.GetAuditLogsOffsetRow , additionalFields AdditionalFields ) string {
344
+ func ( api * API ) auditLogResourceLink (ctx context. Context , alog database.GetAuditLogsOffsetRow , additionalFields AdditionalFields ) string {
347
345
switch alog .ResourceType {
348
346
case database .ResourceTypeTemplate :
349
347
return fmt .Sprintf ("/templates/%s" ,
@@ -352,18 +350,34 @@ func auditLogResourceLink(alog database.GetAuditLogsOffsetRow, additionalFields
352
350
return fmt .Sprintf ("/users?filter=%s" ,
353
351
alog .ResourceTarget )
354
352
case database .ResourceTypeWorkspace :
355
- workspaceOwner := alog .UserUsername .String
356
- if len (additionalFields .WorkspaceOwner ) != 0 && additionalFields .WorkspaceOwner != "unknown" {
357
- workspaceOwner = additionalFields .WorkspaceOwner
353
+ workspace , getWorkspaceErr := api .Database .GetWorkspaceByID (ctx , alog .ResourceID )
354
+ if getWorkspaceErr != nil {
355
+ return ""
356
+ }
357
+ workspaceOwner , getWorkspaceOwnerErr := api .Database .GetUserByID (ctx , workspace .OwnerID )
358
+ if getWorkspaceOwnerErr != nil {
359
+ return ""
358
360
}
359
361
return fmt .Sprintf ("/@%s/%s" ,
360
- workspaceOwner , alog .ResourceTarget )
362
+ workspaceOwner . Username , alog .ResourceTarget )
361
363
case database .ResourceTypeWorkspaceBuild :
362
364
if len (additionalFields .WorkspaceName ) == 0 || len (additionalFields .BuildNumber ) == 0 {
363
365
return ""
364
366
}
367
+ workspaceBuild , getWorkspaceBuildErr := api .Database .GetWorkspaceBuildByID (ctx , alog .ResourceID )
368
+ if getWorkspaceBuildErr != nil {
369
+ return ""
370
+ }
371
+ workspace , getWorkspaceErr := api .Database .GetWorkspaceByID (ctx , workspaceBuild .WorkspaceID )
372
+ if getWorkspaceErr != nil {
373
+ return ""
374
+ }
375
+ workspaceOwner , getWorkspaceOwnerErr := api .Database .GetUserByID (ctx , workspace .OwnerID )
376
+ if getWorkspaceOwnerErr != nil {
377
+ return ""
378
+ }
365
379
return fmt .Sprintf ("/@%s/%s/builds/%s" ,
366
- alog . UserUsername . String , additionalFields .WorkspaceName , additionalFields .BuildNumber )
380
+ workspaceOwner . Username , additionalFields .WorkspaceName , additionalFields .BuildNumber )
367
381
default :
368
382
return ""
369
383
}
0 commit comments