Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 591f5db

Browse files
authored
feat: add has-ai-task filters to the /workspaces and /templates endpoints (#18387)
This PR allows filtering templates and workspaces with the `has-ai-task` filter as described in the [Coder Tasks RFC](https://www.notion.so/coderhq/Coder-Tasks-207d579be5928053ab68c8d9a4b59eaa?source=copy_link#20ad579be59280e6a000eb0646d3c2df).
1 parent 56ff0fb commit 591f5db

File tree

16 files changed

+431
-51
lines changed

16 files changed

+431
-51
lines changed

coderd/apidoc/docs.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmem/dbmem.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,17 @@ func isDeprecated(template database.Template) bool {
13891389
return template.Deprecated != ""
13901390
}
13911391

1392+
func (q *FakeQuerier) getWorkspaceBuildParametersNoLock(workspaceBuildID uuid.UUID) ([]database.WorkspaceBuildParameter, error) {
1393+
params := make([]database.WorkspaceBuildParameter, 0)
1394+
for _, param := range q.workspaceBuildParameters {
1395+
if param.WorkspaceBuildID != workspaceBuildID {
1396+
continue
1397+
}
1398+
params = append(params, param)
1399+
}
1400+
return params, nil
1401+
}
1402+
13921403
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
13931404
return xerrors.New("AcquireLock must only be called within a transaction")
13941405
}
@@ -7898,14 +7909,7 @@ func (q *FakeQuerier) GetWorkspaceBuildParameters(_ context.Context, workspaceBu
78987909
q.mutex.RLock()
78997910
defer q.mutex.RUnlock()
79007911

7901-
params := make([]database.WorkspaceBuildParameter, 0)
7902-
for _, param := range q.workspaceBuildParameters {
7903-
if param.WorkspaceBuildID != workspaceBuildID {
7904-
continue
7905-
}
7906-
params = append(params, param)
7907-
}
7908-
return params, nil
7912+
return q.getWorkspaceBuildParametersNoLock(workspaceBuildID)
79097913
}
79107914

79117915
func (q *FakeQuerier) GetWorkspaceBuildStatsByTemplates(ctx context.Context, since time.Time) ([]database.GetWorkspaceBuildStatsByTemplatesRow, error) {
@@ -13233,6 +13237,18 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
1323313237
continue
1323413238
}
1323513239
}
13240+
13241+
if arg.HasAITask.Valid {
13242+
tv, err := q.getTemplateVersionByIDNoLock(ctx, template.ActiveVersionID)
13243+
if err != nil {
13244+
return nil, xerrors.Errorf("get template version: %w", err)
13245+
}
13246+
tvHasAITask := tv.HasAITask.Valid && tv.HasAITask.Bool
13247+
if tvHasAITask != arg.HasAITask.Bool {
13248+
continue
13249+
}
13250+
}
13251+
1323613252
templates = append(templates, template)
1323713253
}
1323813254
if len(templates) > 0 {
@@ -13562,6 +13578,43 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
1356213578
}
1356313579
}
1356413580

13581+
if arg.HasAITask.Valid {
13582+
hasAITask, err := func() (bool, error) {
13583+
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, workspace.ID)
13584+
if err != nil {
13585+
return false, xerrors.Errorf("get latest build: %w", err)
13586+
}
13587+
if build.HasAITask.Valid {
13588+
return build.HasAITask.Bool, nil
13589+
}
13590+
// If the build has a nil AI task, check if the job is in progress
13591+
// and if it has a non-empty AI Prompt parameter
13592+
job, err := q.getProvisionerJobByIDNoLock(ctx, build.JobID)
13593+
if err != nil {
13594+
return false, xerrors.Errorf("get provisioner job: %w", err)
13595+
}
13596+
if job.CompletedAt.Valid {
13597+
return false, nil
13598+
}
13599+
parameters, err := q.getWorkspaceBuildParametersNoLock(build.ID)
13600+
if err != nil {
13601+
return false, xerrors.Errorf("get workspace build parameters: %w", err)
13602+
}
13603+
for _, param := range parameters {
13604+
if param.Name == "AI Prompt" && param.Value != "" {
13605+
return true, nil
13606+
}
13607+
}
13608+
return false, nil
13609+
}()
13610+
if err != nil {
13611+
return nil, xerrors.Errorf("get hasAITask: %w", err)
13612+
}
13613+
if hasAITask != arg.HasAITask.Bool {
13614+
continue
13615+
}
13616+
}
13617+
1356513618
// If the filter exists, ensure the object is authorized.
1356613619
if prepared != nil && prepared.Authorize(ctx, workspace.RBACObject()) != nil {
1356713620
continue

coderd/database/modelqueries.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
8080
arg.FuzzyName,
8181
pq.Array(arg.IDs),
8282
arg.Deprecated,
83+
arg.HasAITask,
8384
)
8485
if err != nil {
8586
return nil, err
@@ -264,6 +265,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
264265
arg.LastUsedBefore,
265266
arg.LastUsedAfter,
266267
arg.UsingActive,
268+
arg.HasAITask,
267269
arg.RequesterID,
268270
arg.Offset,
269271
arg.Limit,
@@ -311,6 +313,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
311313
&i.LatestBuildError,
312314
&i.LatestBuildTransition,
313315
&i.LatestBuildStatus,
316+
&i.LatestBuildHasAITask,
314317
&i.Count,
315318
); err != nil {
316319
return nil, err

coderd/database/queries.sql.go

Lines changed: 58 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)