mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(coderd): add lookup task by name in httpmw.TaskParam (#20647)
* Adds a `GetTaskByOwnerIDAndName` query * Updates `httpmw.TaskParam` to fall back to task name if no task by UUID found. * Updates the `TaskByIdentifier` used in `cli/` to use direct lookup instead of searching.
This commit is contained in:
+26
-28
@@ -231,6 +231,7 @@ func (c *ExperimentalClient) Tasks(ctx context.Context, filter *TasksFilter) ([]
|
||||
}
|
||||
|
||||
// TaskByID fetches a single experimental task by its ID.
|
||||
// Only tasks owned by codersdk.Me are supported.
|
||||
//
|
||||
// Experimental: This method is experimental and may change in the future.
|
||||
func (c *ExperimentalClient) TaskByID(ctx context.Context, id uuid.UUID) (Task, error) {
|
||||
@@ -251,6 +252,30 @@ func (c *ExperimentalClient) TaskByID(ctx context.Context, id uuid.UUID) (Task,
|
||||
return task, nil
|
||||
}
|
||||
|
||||
// TaskByOwnerAndName fetches a single experimental task by its owner and name.
|
||||
//
|
||||
// Experimental: This method is experimental and may change in the future.
|
||||
func (c *ExperimentalClient) TaskByOwnerAndName(ctx context.Context, owner, ident string) (Task, error) {
|
||||
if owner == "" {
|
||||
owner = Me
|
||||
}
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/experimental/tasks/%s/%s", owner, ident), nil)
|
||||
if err != nil {
|
||||
return Task{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return Task{}, ReadBodyAsError(res)
|
||||
}
|
||||
|
||||
var task Task
|
||||
if err := json.NewDecoder(res.Body).Decode(&task); err != nil {
|
||||
return Task{}, err
|
||||
}
|
||||
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func splitTaskIdentifier(identifier string) (owner string, taskName string, err error) {
|
||||
parts := strings.Split(identifier, "/")
|
||||
|
||||
@@ -287,34 +312,7 @@ func (c *ExperimentalClient) TaskByIdentifier(ctx context.Context, identifier st
|
||||
return Task{}, err
|
||||
}
|
||||
|
||||
tasks, err := c.Tasks(ctx, &TasksFilter{
|
||||
Owner: owner,
|
||||
})
|
||||
if err != nil {
|
||||
return Task{}, xerrors.Errorf("list tasks for owner %q: %w", owner, err)
|
||||
}
|
||||
|
||||
if taskID, err := uuid.Parse(taskName); err == nil {
|
||||
// Find task by ID.
|
||||
for _, task := range tasks {
|
||||
if task.ID == taskID {
|
||||
return task, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Find task by name.
|
||||
for _, task := range tasks {
|
||||
if task.Name == taskName {
|
||||
return task, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mimic resource not found from API.
|
||||
var notFoundErr error = &Error{
|
||||
Response: Response{Message: "Resource not found or you do not have access to this resource"},
|
||||
}
|
||||
return Task{}, xerrors.Errorf("task %q not found for owner %q: %w", taskName, owner, notFoundErr)
|
||||
return c.TaskByOwnerAndName(ctx, owner, taskName)
|
||||
}
|
||||
|
||||
// DeleteTask deletes a task by its ID.
|
||||
|
||||
Reference in New Issue
Block a user