Skip to content

Commit 9895b88

Browse files
committed
remove duplicate workspace build entry
1 parent ab1fec0 commit 9895b88

File tree

2 files changed

+3
-54
lines changed

2 files changed

+3
-54
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ func (server *Server) FailJob(ctx context.Context, failJob *proto.FailedJob) (*p
520520
case *proto.FailedJob_TemplateImport_:
521521
}
522522

523+
// if failed job is a workspace build, audit the outcome
523524
if job.Type == database.ProvisionerJobTypeWorkspaceBuild {
524525
auditor := server.Auditor.Load()
525526
build, getBuildErr := server.Database.GetWorkspaceBuildByJobID(ctx, job.ID)
@@ -695,9 +696,9 @@ func (server *Server) CompleteJob(ctx context.Context, completed *proto.Complete
695696
return nil, xerrors.Errorf("complete job: %w", err)
696697
}
697698

699+
// audit the outcome of the workspace build
698700
if getWorkspaceError == nil {
699701
auditor := server.Auditor.Load()
700-
701702
auditAction := determineAuditAction(workspaceBuild.Transition)
702703

703704
// We pass the workspace name to the Auditor so that it

coderd/workspacebuilds.go

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"golang.org/x/exp/slices"
1616
"golang.org/x/xerrors"
1717

18-
"cdr.dev/slog"
19-
"github.com/coder/coder/coderd/audit"
2018
"github.com/coder/coder/coderd/database"
2119
"github.com/coder/coder/coderd/httpapi"
2220
"github.com/coder/coder/coderd/httpmw"
@@ -280,58 +278,8 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
280278
return
281279
}
282280

283-
auditor := api.Auditor.Load()
284-
285-
// if user deletes a workspace, audit the workspace
286-
if action == rbac.ActionDelete {
287-
aReq, commitAudit := audit.InitRequest[database.Workspace](rw, &audit.RequestParams{
288-
Audit: *auditor,
289-
Log: api.Logger,
290-
Request: r,
291-
Action: database.AuditActionDelete,
292-
})
293-
294-
defer commitAudit()
295-
aReq.Old = workspace
296-
}
297-
298-
latestBuild, latestBuildErr := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
299-
300-
// if a user starts/stops a workspace, audit the workspace build
301-
if action == rbac.ActionUpdate {
302-
var auditAction database.AuditAction
303-
if createBuild.Transition == codersdk.WorkspaceTransitionStart {
304-
auditAction = database.AuditActionStart
305-
} else if createBuild.Transition == codersdk.WorkspaceTransitionStop {
306-
auditAction = database.AuditActionStop
307-
} else {
308-
auditAction = database.AuditActionWrite
309-
}
310-
311-
// We pass the workspace name to the Auditor so that it
312-
// can form a friendly string for the user.
313-
workspaceResourceInfo := map[string]string{
314-
"workspaceName": workspace.Name,
315-
}
316-
317-
wriBytes, err := json.Marshal(workspaceResourceInfo)
318-
if err != nil {
319-
api.Logger.Error(ctx, "could not marshal workspace name", slog.Error(err))
320-
}
321-
322-
aReq, commitAudit := audit.InitRequest[database.WorkspaceBuild](rw, &audit.RequestParams{
323-
Audit: *auditor,
324-
Log: api.Logger,
325-
Request: r,
326-
Action: auditAction,
327-
AdditionalFields: wriBytes,
328-
})
329-
330-
defer commitAudit()
331-
aReq.Old = latestBuild
332-
}
333-
334281
if createBuild.TemplateVersionID == uuid.Nil {
282+
latestBuild, latestBuildErr := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
335283
if latestBuildErr != nil {
336284
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
337285
Message: "Internal error fetching the latest workspace build.",

0 commit comments

Comments
 (0)