From 9fe52826dad84597e809eb186c35760af2dbdd5a Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 26 Oct 2026 12:59:53 +0100 Subject: [PATCH] feat: send include_related from WorkspaceByOwnerAndName options (#29908) Stacked on #29855. Wires `WorkspaceOptions.IncludeRelated` through `codersdk.Client.WorkspaceByOwnerAndName`. The method now applies `params.asRequestOption()` (matching `Client.Workspace`), so the `include_related` query parameter is sent to the server. Previously it only forwarded `include_deleted` and silently dropped `IncludeRelated`. - `include_deleted` behavior is unchanged: `asRequestOption` sets it when true; the server treats an omitted value the same as `false`. - Updated the `TemplateOnly` and `LatestBuildOnly` subtests of `TestWorkspaceByOwnerAndNameIncludeRelated` to drive narrowing through the SDK method and its options, exercising the wiring end to end. Option-level encoding is already covered by `TestWorkspaceOptionsAsRequestOption`. _This PR was created by Coder Agents on behalf of @spikecurtis._ --- coderd/workspaces_test.go | 13 +++++++++---- codersdk/workspaces.go | 13 +++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/coderd/workspaces_test.go b/coderd/workspaces_test.go index 92cff9b6713..9d1f7609a5e 100644 --- a/coderd/workspaces_test.go +++ b/coderd/workspaces_test.go @@ -48,6 +48,7 @@ import ( "github.com/coder/coder/v2/coderd/workspacestats" "github.com/coder/coder/v2/coderd/wsbuilder" "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/codersdk/wsrelated" "github.com/coder/coder/v2/cryptorand" "github.com/coder/coder/v2/provisioner/echo" "github.com/coder/coder/v2/provisionersdk/proto" @@ -6620,8 +6621,10 @@ func TestWorkspaceByOwnerAndNameIncludeRelated(t *testing.T) { t.Run("TemplateOnly", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - w, code := get(ctx, t, "template") - require.Equal(t, http.StatusOK, code) + cfg, err := wsrelated.Parse("template") + require.NoError(t, err) + w, err := client.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{IncludeRelated: &cfg}) + require.NoError(t, err) require.Equal(t, workspace.ID, w.ID) // The build was not requested, so it is omitted (zero value). require.Equal(t, uuid.Nil, w.LatestBuild.ID) @@ -6632,8 +6635,10 @@ func TestWorkspaceByOwnerAndNameIncludeRelated(t *testing.T) { t.Run("LatestBuildOnly", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) - w, code := get(ctx, t, "latest_build.*") - require.Equal(t, http.StatusOK, code) + cfg, err := wsrelated.Parse("latest_build.*") + require.NoError(t, err) + w, err := client.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{IncludeRelated: &cfg}) + require.NoError(t, err) require.Equal(t, workspace.ID, w.ID) // The build was requested, so it is populated. require.Equal(t, workspace.LatestBuild.ID, w.LatestBuild.ID) diff --git a/codersdk/workspaces.go b/codersdk/workspaces.go index 634ddb7511e..db1dbad03fd 100644 --- a/codersdk/workspaces.go +++ b/codersdk/workspaces.go @@ -654,7 +654,7 @@ func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) (Worksp } // WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name. -func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceOptions) (Workspace, error) { +func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, opts ...WorkspaceOptions) (Workspace, error) { // These identifiers must remain individual path segments during URL resolution. if err := validateWorkspacePathSegment(owner); err != nil { return Workspace{}, xerrors.Errorf("invalid workspace owner: %w", err) @@ -662,11 +662,12 @@ func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name if err := validateWorkspacePathSegment(name); err != nil { return Workspace{}, xerrors.Errorf("invalid workspace name: %w", err) } - res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, func(r *http.Request) { - q := r.URL.Query() - q.Set("include_deleted", fmt.Sprintf("%t", params.IncludeDeleted)) - r.URL.RawQuery = q.Encode() - }) + reqOpts := make([]RequestOption, 0, len(opts)+1) + reqOpts = append(reqOpts, WithQueryParam("include_deleted", "false")) + for _, o := range opts { + reqOpts = append(reqOpts, o.asRequestOption()) + } + res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, reqOpts...) if err != nil { return Workspace{}, err }