-
Notifications
You must be signed in to change notification settings - Fork 1
feat(workspace): add workspace view command and surface ID in auth status #4
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
Open
obs-gh-virjramakrishnan
wants to merge
3
commits into
main
Choose a base branch
from
vramakrishnan/workspace-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3d9021e
feat(workspace): add workspace view command and surface ID in auth st…
sfc-gh-vramakrishnan 8637f3c
fix(workspace): fix mock return type and rebase onto main after #3 me…
sfc-gh-vramakrishnan a2e9191
docs: add workspace view to README commands table
sfc-gh-vramakrishnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { buildRouteMap } from "@stricli/core"; | ||
| import { viewCommand } from "./view"; | ||
|
|
||
| export const workspaceRoutes = buildRouteMap({ | ||
| routes: { | ||
| view: viewCommand, | ||
| }, | ||
| docs: { | ||
| brief: "View workspace information", | ||
| fullDescription: "View workspace information including the workspace ID.", | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { | ||
| afterAll, | ||
| beforeAll, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| mock, | ||
| test, | ||
| } from "bun:test"; | ||
| import { resolve } from "node:path"; | ||
| import type { LocalContext } from "../../context"; | ||
| import { createWriter } from "../../lib/writer"; | ||
|
|
||
| const repoRoot = resolve(import.meta.dir, "../../.."); | ||
| const gqlModulePath = resolve(repoRoot, "src/gql/workspace/view-workspace.ts"); | ||
|
|
||
| const loadConfigFn = mock(() => ({ | ||
| customerId: "test-customer", | ||
| token: "test-token", | ||
| domain: "observeinc.com", | ||
| })); | ||
|
|
||
| const viewWorkspaceFn = mock( | ||
| ( | ||
| _config: unknown, | ||
| ): Promise<{ | ||
| id: string; | ||
| label: string; | ||
| timezone: string; | ||
| locale: string; | ||
| createdDate: string; | ||
| } | null> => | ||
| Promise.resolve({ | ||
| id: "42587555", | ||
| label: "Default", | ||
| timezone: "America/Los_Angeles", | ||
| locale: "en_US", | ||
| createdDate: "2024-01-01T00:00:00Z", | ||
| }), | ||
| ); | ||
|
|
||
| let view: (typeof import("./view"))["view"]; | ||
|
|
||
| let previousNoColor: string | undefined; | ||
| let previousForceColor: string | undefined; | ||
|
|
||
| const deps = { | ||
| loadConfig: loadConfigFn, | ||
| viewWorkspace: viewWorkspaceFn, | ||
| } as Parameters<(typeof import("./view"))["view"]>[1]; | ||
|
|
||
| beforeAll(async () => { | ||
| previousNoColor = process.env.NO_COLOR; | ||
| previousForceColor = process.env.FORCE_COLOR; | ||
| process.env.NO_COLOR = "1"; | ||
| process.env.FORCE_COLOR = "0"; | ||
|
|
||
| void mock.module(gqlModulePath, () => ({ | ||
| viewWorkspace: viewWorkspaceFn, | ||
| })); | ||
|
|
||
| const mod = await import("./view.ts"); | ||
| view = mod.view; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| mock.restore(); | ||
| if (previousNoColor === undefined) { | ||
| delete process.env.NO_COLOR; | ||
| } else { | ||
| process.env.NO_COLOR = previousNoColor; | ||
| } | ||
| if (previousForceColor === undefined) { | ||
| delete process.env.FORCE_COLOR; | ||
| } else { | ||
| process.env.FORCE_COLOR = previousForceColor; | ||
| } | ||
| }); | ||
|
|
||
| function createMockContext() { | ||
| const stdout: string[] = []; | ||
| const stderr: string[] = []; | ||
| let exitCode: number | undefined; | ||
|
|
||
| const processMock = { | ||
| stdout: { | ||
| write: (msg: string) => { | ||
| stdout.push(msg); | ||
| return true; | ||
| }, | ||
| }, | ||
| stderr: { | ||
| write: (msg: string) => { | ||
| stderr.push(msg); | ||
| return true; | ||
| }, | ||
| }, | ||
| exit: (code?: number) => { | ||
| exitCode = code ?? 0; | ||
| throw new Error("process.exit"); | ||
| }, | ||
| }; | ||
|
|
||
| const context = { | ||
| process: processMock, | ||
| writer: createWriter({ process: processMock }), | ||
| } as unknown as LocalContext; | ||
|
|
||
| return { context, stdout, stderr, getExitCode: () => exitCode }; | ||
| } | ||
|
|
||
| describe("workspace view", () => { | ||
| beforeEach(() => { | ||
| loadConfigFn.mockClear(); | ||
| viewWorkspaceFn.mockClear(); | ||
| }); | ||
|
|
||
| test("outputs workspace id, label, and metadata", async () => { | ||
| const { context, stdout } = createMockContext(); | ||
| await view.call(context, {}, deps); | ||
|
|
||
| expect(viewWorkspaceFn).toHaveBeenCalledTimes(1); | ||
| const output = JSON.parse(stdout.join("")); | ||
| expect(output.id).toBe("42587555"); | ||
| expect(output.label).toBe("Default"); | ||
| expect(output.timezone).toBe("America/Los_Angeles"); | ||
| expect(output.locale).toBe("en_US"); | ||
| }); | ||
|
|
||
| test("exits with code 1 when no workspace is found", async () => { | ||
| viewWorkspaceFn.mockImplementationOnce(() => Promise.resolve(null)); | ||
|
|
||
| const { context, stderr, getExitCode } = createMockContext(); | ||
| try { | ||
| await view.call(context, {}, deps); | ||
| throw new Error("expected process.exit"); | ||
| } catch (error) { | ||
| expect((error as Error).message).toBe("process.exit"); | ||
| } | ||
| expect(getExitCode()).toBe(1); | ||
| expect(stderr.join("")).toContain("No workspace found"); | ||
| }); | ||
|
|
||
| test("exits with code 1 on API error", async () => { | ||
| viewWorkspaceFn.mockImplementationOnce(() => { | ||
| const err = new Error("Unauthorized"); | ||
| err.name = "GqlApiError"; | ||
| (err as unknown as { statusCode: number }).statusCode = 401; | ||
| throw err; | ||
| }); | ||
|
|
||
| const { context, stderr, getExitCode } = createMockContext(); | ||
| try { | ||
| await view.call(context, {}, deps); | ||
| throw new Error("expected process.exit"); | ||
| } catch (error) { | ||
| expect((error as Error).message).toBe("process.exit"); | ||
| } | ||
| expect(getExitCode()).toBe(1); | ||
| expect(stderr.join("")).toContain("Error"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { buildCommand } from "@stricli/core"; | ||
| import type { LocalContext } from "../../context"; | ||
| import { viewWorkspace } from "../../gql/workspace/view-workspace"; | ||
| import { GqlApiError } from "../../gql/gql-request"; | ||
| import { loadConfig } from "../../lib/config"; | ||
|
|
||
| export interface ViewWorkspaceDeps { | ||
| loadConfig?: typeof loadConfig; | ||
| viewWorkspace?: typeof viewWorkspace; | ||
| } | ||
|
|
||
| export async function view( | ||
| this: LocalContext, | ||
| _flags: Record<string, never>, | ||
| deps: ViewWorkspaceDeps = {}, | ||
| ): Promise<void> { | ||
| const { | ||
| loadConfig: loadConfigImpl = loadConfig, | ||
| viewWorkspace: viewWorkspaceImpl = viewWorkspace, | ||
| } = deps; | ||
| const { process, writer } = this; | ||
|
|
||
| try { | ||
| const config = loadConfigImpl(); | ||
| const workspace = await viewWorkspaceImpl(config); | ||
| if (!workspace) { | ||
| writer.error("No workspace found"); | ||
| process.exit(1); | ||
| return; | ||
| } | ||
| writer.write(JSON.stringify(workspace, null, 2)); | ||
| } catch (error) { | ||
| if (error instanceof GqlApiError) { | ||
| writer.error(`API Error (${error.statusCode}): ${error.message}`); | ||
| } else { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| writer.error(`Error: ${message}`); | ||
| } | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| export const viewCommand = buildCommand({ | ||
| loader: async () => view, | ||
| parameters: { | ||
| positional: { | ||
| kind: "tuple", | ||
| parameters: [], | ||
| }, | ||
| flags: {}, | ||
| }, | ||
| docs: { | ||
| brief: "View the current workspace", | ||
| fullDescription: | ||
| "Displays the current workspace ID, name, timezone, locale, and creation date.\n\n" + | ||
| "Examples:\n" + | ||
| " observe workspace view", | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| query ViewWorkspace { | ||
| currentUser { | ||
| workspaces { | ||
| id | ||
| label | ||
| timezone | ||
| locale | ||
| createdDate | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import type { Config } from "../../lib/config"; | ||
| import { | ||
| ViewWorkspaceDocument, | ||
| type ViewWorkspaceQuery, | ||
| } from "../generated/graphql"; | ||
| import { executeGraphQL } from "../gql-request"; | ||
|
|
||
| export type GqlWorkspaceDetail = NonNullable< | ||
| ViewWorkspaceQuery["currentUser"] | ||
| >["workspaces"][number]; | ||
|
|
||
| export async function viewWorkspace( | ||
| config: Config, | ||
| ): Promise<GqlWorkspaceDetail | null> { | ||
| const response = await executeGraphQL(config, ViewWorkspaceDocument, {}); | ||
| return response.data.currentUser?.workspaces[0] ?? null; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workspace is a concept thats being removed -- are we sure we need to add it to the cli?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need it for creating links to the customer's observe account, since the URL contains the workspaceID, e.g.
https://146206672945.observe-eng.com/workspace/42587555/service-explorer. If we dohttps://146206672945.observe-eng.com/service-explorerthe link does not work. I like having the agent provide this link, as the customer can directly see the data in observe that they set up via agent. I would remove this once we remove workspaces from the URLs. If we cannot have this API, I can also just not have the agent provide links at the end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having it in auth status is fine, perhaps marked
(deprecated), but I wouldn't addobserve workspace view.