@@ -160,6 +160,11 @@ func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAudit
160
160
return alogs
161
161
}
162
162
163
+ type AdditionalFields struct {
164
+ WorkspaceName string
165
+ BuildNumber string
166
+ }
167
+
163
168
func (api * API ) convertAuditLog (ctx context.Context , dblog database.GetAuditLogsOffsetRow ) codersdk.AuditLog {
164
169
ip , _ := netip .AddrFromSlice (dblog .Ip .IPNet .IP )
165
170
@@ -185,12 +190,29 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
185
190
}
186
191
}
187
192
188
- isDeleted := api .auditLogIsResourceDeleted (ctx , dblog )
189
- var resourceLink string
193
+ var (
194
+ additionalFieldsBytes = []byte (dblog .AdditionalFields )
195
+ additionalFields AdditionalFields
196
+ err = json .Unmarshal (additionalFieldsBytes , & additionalFields )
197
+ )
198
+ if err != nil {
199
+ api .Logger .Error (ctx , "unmarshal additional fields" , slog .Error (err ))
200
+ resourceInfo := map [string ]string {
201
+ "workspaceName" : "unknown" ,
202
+ "buildNumber" : "unknown" ,
203
+ }
204
+ dblog .AdditionalFields , err = json .Marshal (resourceInfo )
205
+ api .Logger .Error (ctx , "marshal additional fields" , slog .Error (err ))
206
+ }
207
+
208
+ var (
209
+ isDeleted = api .auditLogIsResourceDeleted (ctx , dblog )
210
+ resourceLink string
211
+ )
190
212
if isDeleted {
191
213
resourceLink = ""
192
214
} else {
193
- resourceLink = api . auditLogResourceLink (ctx , dblog )
215
+ resourceLink = auditLogResourceLink (dblog , additionalFields )
194
216
}
195
217
196
218
return codersdk.AuditLog {
@@ -209,23 +231,28 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
209
231
StatusCode : dblog .StatusCode ,
210
232
AdditionalFields : dblog .AdditionalFields ,
211
233
User : user ,
212
- Description : auditLogDescription (dblog ),
234
+ Description : auditLogDescription (dblog , additionalFields ),
213
235
ResourceLink : resourceLink ,
214
236
IsDeleted : isDeleted ,
215
237
}
216
238
}
217
239
218
- func auditLogDescription (alog database.GetAuditLogsOffsetRow ) string {
240
+ func auditLogDescription (alog database.GetAuditLogsOffsetRow , additionalFields AdditionalFields ) string {
219
241
str := fmt .Sprintf ("{user} %s" ,
220
242
codersdk .AuditAction (alog .Action ).FriendlyString (),
221
243
)
222
244
223
245
// Strings for starting/stopping workspace builds follow the below format:
224
- // "{user} started build for workspace {target}"
246
+ // "{user} started build #{build_number} for workspace {target}"
225
247
// where target is a workspace instead of a workspace build
226
248
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35
227
249
if alog .ResourceType == database .ResourceTypeWorkspaceBuild && alog .Action != database .AuditActionDelete {
228
- str += " build for"
250
+ if len (additionalFields .BuildNumber ) == 0 {
251
+ str += " build for"
252
+ } else {
253
+ str += fmt .Sprintf (" build #%s for" ,
254
+ additionalFields .BuildNumber )
255
+ }
229
256
}
230
257
231
258
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
@@ -295,12 +322,7 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get
295
322
}
296
323
}
297
324
298
- type AdditionalFields struct {
299
- WorkspaceName string
300
- BuildNumber string
301
- }
302
-
303
- func (api * API ) auditLogResourceLink (ctx context.Context , alog database.GetAuditLogsOffsetRow ) string {
325
+ func auditLogResourceLink (alog database.GetAuditLogsOffsetRow , additionalFields AdditionalFields ) string {
304
326
switch alog .ResourceType {
305
327
case database .ResourceTypeTemplate :
306
328
return fmt .Sprintf ("/templates/%s" ,
@@ -312,11 +334,8 @@ func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAudit
312
334
return fmt .Sprintf ("/@%s/%s" ,
313
335
alog .UserUsername .String , alog .ResourceTarget )
314
336
case database .ResourceTypeWorkspaceBuild :
315
- additionalFieldsBytes := []byte (alog .AdditionalFields )
316
- var additionalFields AdditionalFields
317
- err := json .Unmarshal (additionalFieldsBytes , & additionalFields )
318
- if err != nil {
319
- api .Logger .Error (ctx , "unmarshal workspace name" , slog .Error (err ))
337
+ if len (additionalFields .WorkspaceName ) == 0 || len (additionalFields .BuildNumber ) == 0 {
338
+ return ""
320
339
}
321
340
return fmt .Sprintf ("/@%s/%s/builds/%s" ,
322
341
alog .UserUsername .String , additionalFields .WorkspaceName , additionalFields .BuildNumber )
0 commit comments