-
Notifications
You must be signed in to change notification settings - Fork 961
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
30b4008
16eee74
0cd6fde
d9e7e08
e4695ff
0ad6ce3
66a4fd7
6e14061
ae212d0
5a671e8
08cab33
27b5823
9b5a99c
e1d9fd4
dc041de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
package coderd | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"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 +67,79 @@ 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) aiTasksCreate(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
ctx = r.Context() | ||
apiKey = httpmw.APIKey(r) | ||
auditor = api.Auditor.Load() | ||
) | ||
|
||
var req codersdk.CreateAITasksRequest | ||
if !httpapi.Read(ctx, rw, r, &req) { | ||
return | ||
} | ||
|
||
user, err := api.Database.GetUserByID(ctx, apiKey.UserID) | ||
if httpapi.Is404Error(err) { | ||
httpapi.ResourceNotFound(rw) | ||
return | ||
} | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error fetching user.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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: `Template does not have required parameter "` + codersdk.AITaskPromptParameterName + `"`, | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
return | ||
} | ||
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. suggestion: This might be better moved inside |
||
|
||
createReq := codersdk.CreateWorkspaceRequest{ | ||
Name: req.Name, | ||
TemplateVersionID: req.TemplateVersionID, | ||
TemplateVersionPresetID: req.TemplateVersionPresetID, | ||
RichParameterValues: []codersdk.WorkspaceBuildParameter{ | ||
{Name: codersdk.AITaskPromptParameterName, Value: req.Prompt}, | ||
}, | ||
} | ||
|
||
owner := workspaceOwner{ | ||
ID: user.ID, | ||
Username: user.Username, | ||
AvatarURL: user.AvatarURL, | ||
} | ||
|
||
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 about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.