Skip to content

add owner information to the template context #15

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

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions preview/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
OptionTypeListString OptionType = "list(string)"
)

type WorkspaceOwner = types.WorkspaceOwner

type PreviewOutput struct {
Output *Output `json:"output"`
Diags types.Diagnostics `json:"diags"`
Expand Down
22 changes: 18 additions & 4 deletions preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/coder/preview"
"github.com/coder/preview/types"

"github.com/coder/parameters-playground/preview/apitypes"
)

Expand Down Expand Up @@ -61,12 +60,17 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
return err
}

owner, err := workspaceOwner(p[1])
if err != nil {
return err
}

handler := slog.NewJSONHandler(l, nil)
logger := slog.New(handler)

var parameters map[string]string
if len(p) >= 2 {
params, err := jsValueToStringMap(p[1])
if len(p) >= 3 {
params, err := jsValueToStringMap(p[2])
if err != nil {
logger.Error("Unable to convert second prameter into map[string]string", "err", err)
}
Expand All @@ -81,7 +85,7 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
PlanJSONPath: "",
PlanJSON: nil,
ParameterValues: parameters,
Owner: types.WorkspaceOwner{},
Owner: owner,
Logger: logger,
}, tf)

Expand Down Expand Up @@ -110,6 +114,16 @@ func fileTreeFS(value js.Value) (fs.FS, error) {
return afero.NewIOFS(mem), nil
}

func workspaceOwner(value js.Value) (apitypes.WorkspaceOwner, error) {
data := js.Global().Get("JSON").Call("stringify", value).String()
var owner apitypes.WorkspaceOwner
if err := json.Unmarshal([]byte(data), &owner); err != nil {
return apitypes.WorkspaceOwner(types.WorkspaceOwner{}), err
}

return owner, nil
}

func loadTree(mem afero.Fs, fileTree map[string]any, path ...string) {
dir := filepath.Join(path...)
err := mem.MkdirAll(dir, 0755)
Expand Down
Binary file modified public/build/preview.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import {
} from "@/client/components/Tooltip";
import { rpc } from "@/utils/rpc";
import { useLoaderData, type LoaderFunctionArgs } from "react-router";
import type {WorkspaceOwner} from "@/gen/types.ts";

type GoPreviewDef = (
v: Record<string, string>,
owner: WorkspaceOwner,
params: Record<string, string>,
) => Promise<string>;

Expand Down
12 changes: 11 additions & 1 deletion src/client/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "@/client/diagnostics";
import { useDebouncedValue } from "@/client/hooks/debounce";
import { useStore } from "@/client/store";
import type { Parameter, ParserLog, PreviewOutput } from "@/gen/types";
import type {Parameter, ParserLog, PreviewOutput} from "@/gen/types";
import { cn } from "@/utils/cn";
import ReactJsonView from "@microlink/react-json-view";
import * as Dialog from "@radix-ui/react-dialog";
Expand Down Expand Up @@ -83,6 +83,16 @@ export const Preview: FC = () => {
{
"main.tf": debouncedCode,
},
{
id: "8d36e355-e775-4c49-9b8d-ac042ed50440",
name: "coder",
full_name: "Coder",
email: "coder@coder.com",
ssh_public_key: "",
groups: ["Everyone"],
login_type: "password",
rbac_roles: [{name:"member", org_id:""}, {name:"organization-member",org_id:"09942665-ba1b-4661-be9f-36bf9f738c83"}]
},
$form,
);

Expand Down
18 changes: 18 additions & 0 deletions src/gen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,22 @@ export interface Range {
End: Pos;
}

// From apitypes/apitypes.go
export interface WorkspaceOwner {
id: string;
name: string;
full_name: string;
email: string;
ssh_public_key: string;
groups: string[];
login_type: string;
rbac_roles: WorkspaceOwnerRBACRole[];
}

// From types/owner.go
export interface WorkspaceOwnerRBACRole {
name: string;
org_id: string;
}