- Notifications
You must be signed in to change notification settings - Fork1.1k
fix: update dbgen and dbfake task creation and toolsdk test fixtures#20508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,6 +41,7 @@ type WorkspaceResponse struct { | ||
| Build database.WorkspaceBuild | ||
| AgentToken string | ||
| TemplateVersionResponse | ||
| Task database.Task | ||
| } | ||
| // WorkspaceBuildBuilder generates workspace builds and associated | ||
| @@ -57,6 +58,7 @@ type WorkspaceBuildBuilder struct { | ||
| agentToken string | ||
| jobStatus database.ProvisionerJobStatus | ||
| taskAppID uuid.UUID | ||
| taskSeed database.TaskTable | ||
| } | ||
| // WorkspaceBuild generates a workspace build for the provided workspace. | ||
| @@ -115,25 +117,28 @@ func (b WorkspaceBuildBuilder) WithAgent(mutations ...func([]*sdkproto.Agent) [] | ||
| return b | ||
| } | ||
| func (b WorkspaceBuildBuilder) WithTask(taskSeed database.TaskTable, appSeed *sdkproto.App) WorkspaceBuildBuilder { | ||
| //nolint:revive // returns modified struct | ||
| b.taskSeed = taskSeed | ||
| if appSeed == nil { | ||
| appSeed = &sdkproto.App{} | ||
| } | ||
| var err error | ||
| //nolint: revive // returns modified struct | ||
| b.taskAppID, err = uuid.Parse(takeFirst(appSeed.Id, uuid.NewString())) | ||
| require.NoError(b.t, err) | ||
| return b.Params(database.WorkspaceBuildParameter{ | ||
| Name: codersdk.AITaskPromptParameterName, | ||
| Value:b.taskSeed.Prompt, | ||
| }).WithAgent(func(a []*sdkproto.Agent) []*sdkproto.Agent { | ||
| a[0].Apps = []*sdkproto.App{ | ||
| { | ||
| Id: b.taskAppID.String(), | ||
| Slug: takeFirst(appSeed.Slug, "task-app"), | ||
| Url: takeFirst(appSeed.Url, ""), | ||
| }, | ||
| } | ||
| return a | ||
| @@ -212,6 +217,37 @@ func (b WorkspaceBuildBuilder) Do() WorkspaceResponse { | ||
| b.seed.WorkspaceID = b.ws.ID | ||
| b.seed.InitiatorID = takeFirst(b.seed.InitiatorID, b.ws.OwnerID) | ||
| // If a task was requested, ensure it exists and is associated with this | ||
| // workspace. | ||
| if b.taskAppID != uuid.Nil { | ||
| b.logger.Debug(context.Background(), "creating or updating task", "task_id", b.taskSeed.ID) | ||
| b.taskSeed.OrganizationID = takeFirst(b.taskSeed.OrganizationID, b.ws.OrganizationID) | ||
| b.taskSeed.OwnerID = takeFirst(b.taskSeed.OwnerID, b.ws.OwnerID) | ||
| b.taskSeed.Name = takeFirst(b.taskSeed.Name, b.ws.Name) | ||
| b.taskSeed.WorkspaceID = uuid.NullUUID{UUID: takeFirst(b.taskSeed.WorkspaceID.UUID, b.ws.ID), Valid: true} | ||
| b.taskSeed.TemplateVersionID = takeFirst(b.taskSeed.TemplateVersionID, b.seed.TemplateVersionID) | ||
| // Try to fetch existing task and update its workspace ID. | ||
| if task, err := b.db.GetTaskByID(ownerCtx, b.taskSeed.ID); err == nil { | ||
| if !task.WorkspaceID.Valid { | ||
| b.logger.Info(context.Background(), "updating task workspace id", "task_id", b.taskSeed.ID, "workspace_id", b.ws.ID) | ||
| _, err = b.db.UpdateTaskWorkspaceID(ownerCtx, database.UpdateTaskWorkspaceIDParams{ | ||
| ID: b.taskSeed.ID, | ||
| WorkspaceID: uuid.NullUUID{UUID: b.ws.ID, Valid: true}, | ||
| }) | ||
| require.NoError(b.t, err, "update task workspace id") | ||
| } else if task.WorkspaceID.UUID != b.ws.ID { | ||
| require.Fail(b.t, "task already has a workspace id, mismatch", task.WorkspaceID.UUID, b.ws.ID) | ||
| } | ||
| } else if errors.Is(err, sql.ErrNoRows) { | ||
| task := dbgen.Task(b.t, b.db, b.taskSeed) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm curious about this. Do we do this in the real workspace builder? If not, what's the motivation for it here? MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Not exactly but also yes. Logically it would make sense to create a TaskBuilder that can take a WorkspaceBuildBuilder and call When we create a task we do so in a transaction which first creates the workspace, then the task, and then the workspace build. But workspace creation by itself never creates a task. | ||
| b.taskSeed.ID = task.ID | ||
| b.logger.Info(context.Background(), "created new task", "task_id", b.taskSeed.ID) | ||
| } else { | ||
| require.NoError(b.t, err, "get task by id") | ||
| } | ||
| } | ||
| // Create a provisioner job for the build! | ||
| payload, err := json.Marshal(provisionerdserver.WorkspaceProvisionJob{ | ||
| WorkspaceBuildID: b.seed.ID, | ||
| @@ -324,6 +360,11 @@ func (b WorkspaceBuildBuilder) Do() WorkspaceResponse { | ||
| b.logger.Debug(context.Background(), "linked task to workspace build", | ||
| slog.F("task_id", task.ID), | ||
| slog.F("build_number", resp.Build.BuildNumber)) | ||
| // Update task after linking. | ||
| task, err = b.db.GetTaskByID(ownerCtx, task.ID) | ||
| require.NoError(b.t, err, "get task by id") | ||
| resp.Task = task | ||
| } | ||
| for i := range b.params { | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.