- Notifications
You must be signed in to change notification settings - Fork1k
refactor: create tasks in coderd instead of frontend#19280
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
30b4008
16eee74
0cd6fde
d9e7e08
e4695ff
0ad6ce3
66a4fd7
6e14061
ae212d0
5a671e8
08cab33
27b5823
9b5a99c
e1d9fd4
dc041de
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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
package coderd | ||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"slices" | ||
"strings" | ||
"github.com/google/uuid" | ||
"github.com/coder/coder/v2/coderd/audit" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/coderd/rbac" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
@@ -61,3 +68,106 @@ func (api *API) aiTasksPrompts(rw http.ResponseWriter, r *http.Request) { | ||
Prompts: promptsByBuildID, | ||
}) | ||
} | ||
// This endpoint is experimental and not guaranteed to be stable, so we're not | ||
// generating public-facing documentation for it. | ||
func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
ctx = r.Context() | ||
apiKey = httpmw.APIKey(r) | ||
auditor = api.Auditor.Load() | ||
mems = httpmw.OrganizationMembersParam(r) | ||
) | ||
var req codersdk.CreateTaskRequest | ||
if !httpapi.Read(ctx, rw, r, &req) { | ||
return | ||
} | ||
hasAITask, err := api.Database.GetTemplateVersionHasAITask(ctx, req.TemplateVersionID) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) || rbac.IsUnauthorizedError(err) { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
} | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching whether the template version has an AI task.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
if !hasAITask { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: fmt.Sprintf(`Template does not have required parameter %q`, codersdk.AITaskPromptParameterName), | ||
}) | ||
return | ||
} | ||
createReq := codersdk.CreateWorkspaceRequest{ | ||
Name: req.Name, | ||
TemplateVersionID: req.TemplateVersionID, | ||
TemplateVersionPresetID: req.TemplateVersionPresetID, | ||
RichParameterValues: []codersdk.WorkspaceBuildParameter{ | ||
{Name: codersdk.AITaskPromptParameterName, Value: req.Prompt}, | ||
}, | ||
} | ||
var owner workspaceOwner | ||
if mems.User != nil { | ||
// This user fetch is an optimization path for the most common case of creating a | ||
// task for 'Me'. | ||
// | ||
// This is also required to allow `owners` to create workspaces for users | ||
// that are not in an organization. | ||
owner = workspaceOwner{ | ||
ID: mems.User.ID, | ||
Username: mems.User.Username, | ||
AvatarURL: mems.User.AvatarURL, | ||
} | ||
} else { | ||
// A task can still be created if the caller can read the organization | ||
// member. The organization is required, which can be sourced from the | ||
// template. | ||
// | ||
// TODO: This code gets called twice for each workspace build request. | ||
// This is inefficient and costs at most 2 extra RTTs to the DB. | ||
// This can be optimized. It exists as it is now for code simplicity. | ||
// The most common case is to create a workspace for 'Me'. Which does | ||
// not enter this code branch. | ||
template, ok := requestTemplate(ctx, rw, createReq, api.Database) | ||
if !ok { | ||
return | ||
} | ||
// If the caller can find the organization membership in the same org | ||
// as the template, then they can continue. | ||
orgIndex := slices.IndexFunc(mems.Memberships, func(mem httpmw.OrganizationMember) bool { | ||
return mem.OrganizationID == template.OrganizationID | ||
}) | ||
if orgIndex == -1 { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
} | ||
member := mems.Memberships[orgIndex] | ||
owner = workspaceOwner{ | ||
ID: member.UserID, | ||
Username: member.Username, | ||
AvatarURL: member.AvatarURL, | ||
} | ||
} | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
aReq, commitAudit := audit.InitRequest[database.WorkspaceTable](rw, &audit.RequestParams{ | ||
Audit: *auditor, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionCreate, | ||
AdditionalFields: audit.AdditionalFields{ | ||
WorkspaceOwner: owner.Username, | ||
}, | ||
}) | ||
defer commitAudit() | ||
createWorkspace(ctx, aReq, apiKey.UserID, api, owner, createReq, rw, r) | ||
} |
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.
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.
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.
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.