mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: fall back to name lookup for UUID-shaped workspace names (#24340)
`namedWorkspace` in `cli/root.go` parsed workspace identifiers with `uuid.Parse` first and returned immediately on success, even when no workspace had that UUID as its actual ID. This caused 404 errors for any workspace whose name was a valid 32-char hex string (dashless UUID). - Add `codersdk.ResolveWorkspace`: tries UUID lookup first, falls back to name lookup on 404. `NameValid` guard skips the fallback for standard dashed UUIDs (36 chars > 32-char name limit). - Export `codersdk.SplitWorkspaceIdentifier`, replacing the duplicate `splitNamedWorkspace` in `cli/root.go` (uses `strings.Cut`). - Delete `namedWorkspace` from `cli/root.go`; all 28 call sites now use `client.ResolveWorkspace` directly. - Delete `namedWorkspace` and `splitNameAndOwner` from `codersdk/toolsdk/bash.go`; inline `client.ResolveWorkspace`. - Simplify `GetWorkspace` tool handler to a single `ResolveWorkspace` call. - Unit tests via httptest mock cover UUID, name, owner/name, UUID-like fallback, not-found, server error, transport error, and invalid identifier paths. - Integration tests in `cli/show_test.go` and `codersdk/toolsdk` for workspaces with UUID-like names. > Generated with Coder Agents
This commit is contained in:
@@ -3,6 +3,7 @@ package codersdk
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
@@ -612,6 +613,53 @@ func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name
|
||||
return workspace, json.NewDecoder(res.Body).Decode(&workspace)
|
||||
}
|
||||
|
||||
// SplitWorkspaceIdentifier splits an identifier into owner and
|
||||
// workspace name. A bare name defaults the owner to Me ("me"). An
|
||||
// "owner/name" pair is accepted, and identifiers with more than one
|
||||
// "/" are rejected.
|
||||
func SplitWorkspaceIdentifier(identifier string) (owner, name string, err error) {
|
||||
owner, name, ok := strings.Cut(identifier, "/")
|
||||
if !ok {
|
||||
return Me, identifier, nil
|
||||
}
|
||||
if strings.Contains(name, "/") {
|
||||
return "", "", xerrors.Errorf("invalid workspace identifier: %q", identifier)
|
||||
}
|
||||
return owner, name, nil
|
||||
}
|
||||
|
||||
// ResolveWorkspace fetches a workspace by identifier, which may be a
|
||||
// UUID, a bare name (owned by the current user), or an "owner/name"
|
||||
// pair. When the identifier parses as a valid UUID but no workspace
|
||||
// exists with that ID, the function falls back to a name-based
|
||||
// lookup because workspace names can be valid UUID strings.
|
||||
func (c *Client) ResolveWorkspace(ctx context.Context, identifier string) (Workspace, error) {
|
||||
if uid, err := uuid.Parse(identifier); err == nil {
|
||||
ws, err := c.Workspace(ctx, uid)
|
||||
if err == nil {
|
||||
return ws, nil
|
||||
}
|
||||
// A workspace name might be a valid UUID string. If the
|
||||
// ID-based lookup returned 404, fall through to name-based
|
||||
// lookup below.
|
||||
var sdkErr *Error
|
||||
if !errors.As(err, &sdkErr) || sdkErr.StatusCode() != http.StatusNotFound {
|
||||
return Workspace{}, err
|
||||
}
|
||||
// A standard dashed UUID (36 chars) cannot be a valid
|
||||
// workspace name (max 32 chars). Skip the wasted
|
||||
// name-based round-trip.
|
||||
if err := NameValid(identifier); err != nil {
|
||||
return Workspace{}, sdkErr
|
||||
}
|
||||
}
|
||||
owner, name, err := SplitWorkspaceIdentifier(identifier)
|
||||
if err != nil {
|
||||
return Workspace{}, err
|
||||
}
|
||||
return c.WorkspaceByOwnerAndName(ctx, owner, name, WorkspaceOptions{})
|
||||
}
|
||||
|
||||
type WorkspaceQuota struct {
|
||||
CreditsConsumed int `json:"credits_consumed"`
|
||||
Budget int `json:"budget"`
|
||||
|
||||
Reference in New Issue
Block a user