|
4 | 4 | "bytes"
|
5 | 5 | "context"
|
6 | 6 | "database/sql"
|
7 |
| -"encoding/json" |
8 | 7 | "errors"
|
9 | 8 | "fmt"
|
10 | 9 | "net/http"
|
@@ -447,6 +446,7 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
|
447 | 446 | // It creates a delete workspace build and returns 202 Accepted if the build was created.
|
448 | 447 | func (api*API)taskDelete(rw http.ResponseWriter,r*http.Request) {
|
449 | 448 | ctx:=r.Context()
|
| 449 | +apiKey:=httpmw.APIKey(r) |
450 | 450 |
|
451 | 451 | idStr:=chi.URLParam(r,"id")
|
452 | 452 | taskID,err:=uuid.Parse(idStr)
|
@@ -490,39 +490,15 @@ func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) {
|
490 | 490 | return
|
491 | 491 | }
|
492 | 492 |
|
493 |
| -// Construct a request to the workspace build creation handler to initiate deletion. |
| 493 | +// Construct a request to the workspace build creation handler to |
| 494 | +// initiate deletion. |
494 | 495 | buildReq:= codersdk.CreateWorkspaceBuildRequest{
|
495 | 496 | Transition:codersdk.WorkspaceTransitionDelete,
|
| 497 | +Reason:"Deleted via tasks API", |
496 | 498 | }
|
497 |
| -body,err:=json.Marshal(buildReq) |
498 |
| -iferr!=nil { |
499 |
| -httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{ |
500 |
| -Message:"Internal error marshaling delete request.", |
501 |
| -Detail:err.Error(), |
502 |
| -}) |
503 |
| -return |
504 |
| -} |
505 |
| - |
506 |
| -req,err:=http.NewRequestWithContext(ctx,http.MethodPost,fmt.Sprintf("/api/v2/workspaces/%s/builds",workspace.ID.String()),bytes.NewReader(body)) |
507 |
| -iferr!=nil { |
508 |
| -httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{ |
509 |
| -Message:"Internal error creating request.", |
510 |
| -Detail:err.Error(), |
511 |
| -}) |
512 |
| -return |
513 |
| -} |
514 |
| -req.Header.Set("Content-Type","application/json") |
515 |
| - |
516 |
| -// Inject the "workspace" URL param so ExtractWorkspaceParam can |
517 |
| -// resolve the workspace. |
518 |
| -rctx:=chi.NewRouteContext() |
519 |
| -rctx.URLParams.Add("workspace",workspace.ID.String()) |
520 |
| -req=req.WithContext(context.WithValue(req.Context(),chi.RouteCtxKey,rctx)) |
521 | 499 |
|
522 |
| -// Call the existing workspace build handler via middleware. |
523 | 500 | rc:=&responseWriterCapture{}
|
524 |
| -handler:=httpmw.ExtractWorkspaceParam(api.Database)(http.HandlerFunc(api.postWorkspaceBuilds)) |
525 |
| -handler.ServeHTTP(rc,req) |
| 501 | +api.postWorkspaceBuildsInternal(rc,r,apiKey,workspace,buildReq) |
526 | 502 |
|
527 | 503 | status:=rc.status
|
528 | 504 | ifstatus==0 {
|
|