Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita3bb906

Browse files
committed
feat(coderd): use task data model when fetching a task
1 parent47ba1a3 commita3bb906

File tree

3 files changed

+65
-77
lines changed

3 files changed

+65
-77
lines changed

‎cli/exp_task_status_test.go‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
funcTest_TaskStatus(t*testing.T) {
2525
t.Parallel()
2626

27-
t.Skip("TODO(mafredri): Remove, fixed down-stack!")
28-
2927
for_,tc:=range []struct {
3028
args []string
3129
expectOutputstring
@@ -90,6 +88,7 @@ func Test_TaskStatus(t *testing.T) {
9088
Healthy:true,
9189
},
9290
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
91+
Status:codersdk.TaskStatusActive,
9392
})
9493
default:
9594
t.Errorf("unexpected path: %s",r.URL.Path)
@@ -125,6 +124,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
125124
Healthy:true,
126125
},
127126
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
127+
Status:codersdk.TaskStatusPending,
128128
})
129129
case1:
130130
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -136,6 +136,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
136136
},
137137
WorkspaceAgentLifecycle:ptr.Ref(codersdk.WorkspaceAgentLifecycleReady),
138138
UpdatedAt:now.Add(-4*time.Second),
139+
Status:codersdk.TaskStatusActive,
139140
})
140141
case2:
141142
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -152,6 +153,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
152153
Timestamp:now.Add(-3*time.Second),
153154
Message:"Reticulating splines...",
154155
},
156+
Status:codersdk.TaskStatusActive,
155157
})
156158
case3:
157159
httpapi.Write(ctx,w,http.StatusOK, codersdk.Task{
@@ -168,6 +170,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
168170
Timestamp:now.Add(-2*time.Second),
169171
Message:"Splines reticulated successfully!",
170172
},
173+
Status:codersdk.TaskStatusActive,
171174
})
172175
default:
173176
httpapi.InternalServerError(w,xerrors.New("too many calls!"))
@@ -188,16 +191,18 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
188191
"owner_name": "",
189192
"name": "",
190193
"template_id": "00000000-0000-0000-0000-000000000000",
194+
"template_version_id": "00000000-0000-0000-0000-000000000000",
191195
"template_name": "",
192196
"template_display_name": "",
193197
"template_icon": "",
194198
"workspace_id": null,
199+
"workspace_status": "running",
195200
"workspace_agent_id": null,
196201
"workspace_agent_lifecycle": null,
197202
"workspace_agent_health": null,
198203
"workspace_app_id": null,
199204
"initial_prompt": "",
200-
"status": "running",
205+
"status": "active",
201206
"current_state": {
202207
"timestamp": "2025-08-26T12:34:57Z",
203208
"state": "working",
@@ -226,6 +231,7 @@ STATE CHANGED STATUS HEALTHY STATE MESSAGE
226231
Timestamp:ts.Add(time.Second),
227232
Message:"Thinking furiously...",
228233
},
234+
Status:codersdk.TaskStatusActive,
229235
})
230236
default:
231237
t.Errorf("unexpected path: %s",r.URL.Path)

‎coderd/aitasks.go‎

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/coder/coder/v2/coderd/rbac/policy"
2626
"github.com/coder/coder/v2/coderd/searchquery"
2727
"github.com/coder/coder/v2/coderd/taskname"
28-
"github.com/coder/coder/v2/coderd/util/slice"
2928
"github.com/coder/coder/v2/codersdk"
3029

3130
aiagentapi"github.com/coder/agentapi-sdk-go"
@@ -604,25 +603,37 @@ func (api *API) tasksList(rw http.ResponseWriter, r *http.Request) {
604603
func (api*API)taskGet(rw http.ResponseWriter,r*http.Request) {
605604
ctx:=r.Context()
606605
apiKey:=httpmw.APIKey(r)
606+
task:=httpmw.TaskParam(r)
607607

608-
idStr:=chi.URLParam(r,"id")
609-
taskID,err:=uuid.Parse(idStr)
610-
iferr!=nil {
611-
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
612-
Message:fmt.Sprintf("Invalid UUID %q for task ID.",idStr),
613-
})
608+
// If task doesn't have a workspace, return task data with template info.
609+
if!task.WorkspaceID.Valid {
610+
// TODO(mafredri): Update the view to include missing fields?
611+
taskResp:= codersdk.Task{
612+
ID:task.ID,
613+
OrganizationID:task.OrganizationID,
614+
OwnerID:task.OwnerID,
615+
// OwnerName: user.Username,
616+
Name:task.Name,
617+
// TemplateID: template.ID,
618+
TemplateVersionID:task.TemplateVersionID,
619+
// TemplateName: template.Name,
620+
// TemplateDisplayName: template.DisplayName,
621+
// TemplateIcon: template.Icon,
622+
InitialPrompt:task.Prompt,
623+
Status:codersdk.TaskStatus(task.Status),
624+
CreatedAt:task.CreatedAt,
625+
UpdatedAt:task.CreatedAt,
626+
}
627+
httpapi.Write(ctx,rw,http.StatusOK,taskResp)
614628
return
615629
}
616630

617-
// For now, taskID = workspaceID, once we have a task data model in
618-
// the DB, we can change this lookup.
619-
workspaceID:=taskID
620-
workspace,err:=api.Database.GetWorkspaceByID(ctx,workspaceID)
621-
ifhttpapi.Is404Error(err) {
622-
httpapi.ResourceNotFound(rw)
623-
return
624-
}
631+
workspace,err:=api.Database.GetWorkspaceByID(ctx,task.WorkspaceID.UUID)
625632
iferr!=nil {
633+
ifhttpapi.Is404Error(err) {
634+
httpapi.ResourceNotFound(rw)
635+
return
636+
}
626637
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
627638
Message:"Internal error fetching workspace.",
628639
Detail:err.Error(),
@@ -642,34 +653,6 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
642653
httpapi.ResourceNotFound(rw)
643654
return
644655
}
645-
ifdata.builds[0].HasAITask==nil||!*data.builds[0].HasAITask {
646-
// TODO(DanielleMaywood):
647-
// This is a temporary workaround. When a task has just been created, but
648-
// not yet provisioned, the workspace build will not have `HasAITask` set.
649-
//
650-
// When we reach this code flow, it is _either_ because the workspace is
651-
// not a task, or it is a task that has not yet been provisioned. This
652-
// endpoint should rarely be called with a non-task workspace so we
653-
// should be fine with this extra database call to check if it has the
654-
// special "AI Task" parameter.
655-
parameters,err:=api.Database.GetWorkspaceBuildParameters(ctx,data.builds[0].ID)
656-
iferr!=nil {
657-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
658-
Message:"Internal error fetching workspace build parameters.",
659-
Detail:err.Error(),
660-
})
661-
return
662-
}
663-
664-
_,hasAITask:=slice.Find(parameters,func(t database.WorkspaceBuildParameter)bool {
665-
returnt.Name==codersdk.AITaskPromptParameterName
666-
})
667-
668-
if!hasAITask {
669-
httpapi.ResourceNotFound(rw)
670-
return
671-
}
672-
}
673656

674657
appStatus:= codersdk.WorkspaceAppStatus{}
675658
iflen(data.appStatuses)>0 {
@@ -692,16 +675,8 @@ func (api *API) taskGet(rw http.ResponseWriter, r *http.Request) {
692675
return
693676
}
694677

695-
tasks,err:=api.tasksFromWorkspaces(ctx, []codersdk.Workspace{ws})
696-
iferr!=nil {
697-
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
698-
Message:"Internal error fetching task prompt and state.",
699-
Detail:err.Error(),
700-
})
701-
return
702-
}
703-
704-
httpapi.Write(ctx,rw,http.StatusOK,tasks[0])
678+
taskResp:=taskFromDBTaskAndWorkspace(task,ws)
679+
httpapi.Write(ctx,rw,http.StatusOK,taskResp)
705680
}
706681

707682
// @Summary Delete AI task by ID

‎coderd/aitasks_test.go‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -282,27 +282,32 @@ func TestTasks(t *testing.T) {
282282
ctx=testutil.Context(t,testutil.WaitLong)
283283
user=coderdtest.CreateFirstUser(t,client)
284284
template=createAITemplate(t,client,user)
285-
// Create a workspace (task) with a specific prompt.
286285
wantPrompt="review my code"
287-
workspace=coderdtest.CreateWorkspace(t,client,template.ID,func(req*codersdk.CreateWorkspaceRequest) {
288-
req.RichParameterValues= []codersdk.WorkspaceBuildParameter{
289-
{Name:codersdk.AITaskPromptParameterName,Value:wantPrompt},
290-
}
291-
})
286+
exp=codersdk.NewExperimentalClient(client)
292287
)
293288

294-
coderdtest.AwaitWorkspaceBuildJobCompleted(t,client,workspace.LatestBuild.ID)
295-
ws:=coderdtest.MustWorkspace(t,client,workspace.ID)
289+
task,err:=exp.CreateTask(ctx,"me", codersdk.CreateTaskRequest{
290+
TemplateVersionID:template.ActiveVersionID,
291+
Input:wantPrompt,
292+
})
293+
require.NoError(t,err)
294+
require.True(t,task.WorkspaceID.Valid)
295+
296+
// Get the workspace and wait for it to be ready.
297+
ws,err:=client.Workspace(ctx,task.WorkspaceID.UUID)
298+
require.NoError(t,err)
299+
coderdtest.AwaitWorkspaceBuildJobCompleted(t,client,ws.LatestBuild.ID)
300+
ws=coderdtest.MustWorkspace(t,client,task.WorkspaceID.UUID)
296301
// Assert invariant: the workspace has exactly one resource with one agent with one app.
297302
require.Len(t,ws.LatestBuild.Resources,1)
298303
require.Len(t,ws.LatestBuild.Resources[0].Agents,1)
299304
agentID:=ws.LatestBuild.Resources[0].Agents[0].ID
300305
taskAppID:=ws.LatestBuild.Resources[0].Agents[0].Apps[0].ID
301306

302307
// Insert an app status for the workspace
303-
_,err:=db.InsertWorkspaceAppStatus(dbauthz.AsSystemRestricted(ctx), database.InsertWorkspaceAppStatusParams{
308+
_,err=db.InsertWorkspaceAppStatus(dbauthz.AsSystemRestricted(ctx), database.InsertWorkspaceAppStatusParams{
304309
ID:uuid.New(),
305-
WorkspaceID:workspace.ID,
310+
WorkspaceID:task.WorkspaceID.UUID,
306311
CreatedAt:dbtime.Now(),
307312
AgentID:agentID,
308313
AppID:taskAppID,
@@ -312,31 +317,33 @@ func TestTasks(t *testing.T) {
312317
require.NoError(t,err)
313318

314319
// Fetch the task by ID via experimental API and verify fields.
315-
exp:=codersdk.NewExperimentalClient(client)
316-
task,err:=exp.TaskByID(ctx,workspace.ID)
320+
updated,err:=exp.TaskByID(ctx,task.ID)
317321
require.NoError(t,err)
318322

319-
assert.Equal(t,workspace.ID,task.ID,"task ID should match workspace ID")
320-
assert.Equal(t,workspace.Name,task.Name,"task name should map from workspace name")
321-
assert.Equal(t,wantPrompt,task.InitialPrompt,"task prompt should match the AI Prompt parameter")
322-
assert.Equal(t,workspace.ID,task.WorkspaceID.UUID,"workspace id should match")
323-
assert.NotEmpty(t,task.WorkspaceStatus,"task status should not be empty")
323+
assert.Equal(t,task.ID,updated.ID,"task ID should match")
324+
assert.Equal(t,task.Name,updated.Name,"task name should match")
325+
assert.Equal(t,wantPrompt,updated.InitialPrompt,"task prompt should match the AI Prompt parameter")
326+
assert.Equal(t,task.WorkspaceID.UUID,updated.WorkspaceID.UUID,"workspace id should match")
327+
assert.Equal(t,ws.LatestBuild.BuildNumber,updated.WorkspaceBuildNumber,"workspace build number should match")
328+
assert.Equal(t,agentID,updated.WorkspaceAgentID.UUID,"workspace agent id should match")
329+
assert.Equal(t,taskAppID,updated.WorkspaceAppID.UUID,"workspace app id should match")
330+
assert.NotEmpty(t,updated.WorkspaceStatus,"task status should not be empty")
324331

325332
// Stop the workspace
326-
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStart,codersdk.WorkspaceTransitionStop)
333+
coderdtest.MustTransitionWorkspace(t,client,task.WorkspaceID.UUID,codersdk.WorkspaceTransitionStart,codersdk.WorkspaceTransitionStop)
327334

328335
// Verify that the previous status still remains
329-
updated,err:=exp.TaskByID(ctx,workspace.ID)
336+
updated,err=exp.TaskByID(ctx,task.ID)
330337
require.NoError(t,err)
331338
assert.NotNil(t,updated.CurrentState,"current state should not be nil")
332339
assert.Equal(t,"all done",updated.CurrentState.Message)
333340
assert.Equal(t,codersdk.TaskStateComplete,updated.CurrentState.State)
334341

335342
// Start the workspace again
336-
coderdtest.MustTransitionWorkspace(t,client,workspace.ID,codersdk.WorkspaceTransitionStop,codersdk.WorkspaceTransitionStart)
343+
coderdtest.MustTransitionWorkspace(t,client,task.WorkspaceID.UUID,codersdk.WorkspaceTransitionStop,codersdk.WorkspaceTransitionStart)
337344

338345
// Verify that the status from the previous build is no longer present
339-
updated,err=exp.TaskByID(ctx,workspace.ID)
346+
updated,err=exp.TaskByID(ctx,task.ID)
340347
require.NoError(t,err)
341348
assert.Nil(t,updated.CurrentState,"current state should be nil")
342349
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp