Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
small refactor
  • Loading branch information
mafredri committed Aug 29, 2025
commit 9aaad7448069a02236a1e5fd227f42ef941b7406
34 changes: 5 additions & 29 deletions coderd/aitasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -447,6 +446,7 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
// It creates a delete workspace build and returns 202 Accepted if the build was created.
func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

idStr := chi.URLParam(r, "id")
taskID, err := uuid.Parse(idStr)
Expand Down Expand Up @@ -490,39 +490,15 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
return
}

// Construct a request to the workspace build creation handler to initiate deletion.
// Construct a request to the workspace build creation handler to
// initiate deletion.
buildReq := codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionDelete,
Reason: "Deleted via tasks API",
}
body, err := json.Marshal(buildReq)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error marshaling delete request.",
Detail: err.Error(),
})
return
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaces/%s/builds", workspace.ID.String()), bytes.NewReader(body))
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error creating request.",
Detail: err.Error(),
})
return
}
req.Header.Set("Content-Type", "application/json")

// Inject the "workspace" URL param so ExtractWorkspaceParam can
// resolve the workspace.
rctx := chi.NewRouteContext()
rctx.URLParams.Add("workspace", workspace.ID.String())
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

// Call the existing workspace build handler via middleware.
rc := &responseWriterCapture{}
handler := httpmw.ExtractWorkspaceParam(api.Database)(http.HandlerFunc(api.postWorkspaceBuilds))
handler.ServeHTTP(rc, req)
api.postWorkspaceBuildsInternal(rc, r, apiKey, workspace, buildReq)

status := rc.status
if status == 0 {
Expand Down
10 changes: 9 additions & 1 deletion coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,21 @@ func (api *API) workspaceBuildByBuildNumber(rw http.ResponseWriter, r *http.Requ
func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
apiKey := httpmw.APIKey(r)

workspace := httpmw.WorkspaceParam(r)
var createBuild codersdk.CreateWorkspaceBuildRequest
if !httpapi.Read(ctx, rw, r, &createBuild) {
return
}

api.postWorkspaceBuildsInternal(rw, r, apiKey, workspace, createBuild)
}

// postWorkspaceBuildsInternal handles the internal logic for creating
// workspace builds, can be called by other handlers and must not
// reference httpmw.
func (api *API) postWorkspaceBuildsInternal(rw http.ResponseWriter, r *http.Request, apiKey database.APIKey, workspace database.Workspace, createBuild codersdk.CreateWorkspaceBuildRequest) {
ctx := r.Context()

transition := database.WorkspaceTransition(createBuild.Transition)
builder := wsbuilder.New(workspace, transition, *api.BuildUsageChecker.Load()).
Initiator(apiKey.UserID).
Expand Down
Loading