-
Notifications
You must be signed in to change notification settings - Fork 252
feat: add proposal status command #3164
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
alepane21
wants to merge
5
commits into
main
Choose a base branch
from
ale/cosmo-78-wgc-add-proposal-status-command
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
5 commits
Select commit
Hold shift + click to select a range
14b1716
feat: add proposal status command
alepane21 f7b717e
fix: if not specified, use the default namespace
alepane21 90e6951
fix: remove bold formatting
alepane21 d2dc1cd
feat: add filter by proposal name
alepane21 5f04b76
Merge branch 'main' into ale/cosmo-78-wgc-add-proposal-status-command
alepane21 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; | ||
| import type { Proposal } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb'; | ||
| import { Command } from 'commander'; | ||
| import pc from 'picocolors'; | ||
| import { BaseCommandOptions } from '../../../core/types/types.js'; | ||
| import { getProposalsByFederatedGraph } from '../utils.js'; | ||
|
|
||
| type FindProposalResult = { success: true; proposal: Proposal } | { success: false; details: string }; | ||
|
|
||
| const findProposal = async ( | ||
| client: BaseCommandOptions['client'], | ||
| { | ||
| name, | ||
| federatedGraphName, | ||
| namespace, | ||
| }: { | ||
| name: string; | ||
| federatedGraphName: string; | ||
| namespace?: string; | ||
| }, | ||
| ): Promise<FindProposalResult> => { | ||
| const resp = await getProposalsByFederatedGraph({ | ||
| client, | ||
| federatedGraphName, | ||
| namespace, | ||
| proposalName: name, | ||
| limit: 1, | ||
| offset: 0, | ||
| }); | ||
|
|
||
| if (resp.response?.code !== EnumStatusCode.OK) { | ||
| return { | ||
| success: false, | ||
| details: resp.response?.details || `Failed to fetch proposal '${name}'.`, | ||
| }; | ||
| } | ||
|
|
||
| const proposal = resp.proposals.find((item) => item.name === name); | ||
| if (proposal) { | ||
| return { success: true, proposal }; | ||
| } | ||
|
|
||
| return { | ||
| success: false, | ||
| details: `Proposal '${name}' not found.`, | ||
| }; | ||
| }; | ||
|
|
||
| export default (opts: BaseCommandOptions) => { | ||
| const command = new Command('status'); | ||
| command.description('Checks the status of an existing proposal for a federated graph.'); | ||
| command.argument('<name>', 'The name of the proposal to check.'); | ||
| command.requiredOption( | ||
| '-f, --federation-graph <federatedGraphName>', | ||
| 'The name of the federated graph this proposal is for.', | ||
| ); | ||
| command.option('-n, --namespace [string]', 'The namespace of the federated graph.', 'default'); | ||
| command.option('-j, --json', 'Prints to the console in json format instead of text.'); | ||
|
|
||
| command.action(async (name, options) => { | ||
| let result: FindProposalResult; | ||
|
|
||
| try { | ||
| result = await findProposal(opts.client, { | ||
| name, | ||
| federatedGraphName: options.federationGraph, | ||
| namespace: options.namespace, | ||
| }); | ||
| } catch (error) { | ||
| result = { | ||
| success: false, | ||
| details: error instanceof Error ? error.message : 'An unknown error occurred.', | ||
| }; | ||
| } | ||
|
|
||
| if (!result.success) { | ||
| if (options.json) { | ||
| console.log( | ||
| JSON.stringify({ | ||
| status: 'error', | ||
| details: result.details, | ||
| }), | ||
| ); | ||
| } else { | ||
| console.error(pc.red(result.details)); | ||
| } | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| if (options.json) { | ||
| console.log( | ||
| JSON.stringify({ | ||
| status: result.proposal.state, | ||
| }), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`Proposal '${name}' status is ${result.proposal.state}.`); | ||
| }); | ||
|
|
||
| return command; | ||
| }; | ||
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,150 @@ | ||
| import { type MessageInitShape } from '@bufbuild/protobuf'; | ||
| import { createClient, createRouterTransport } from '@connectrpc/connect'; | ||
| import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; | ||
| import { | ||
| type GetProposalsByFederatedGraphRequest, | ||
| GetProposalsByFederatedGraphResponseSchema, | ||
| PlatformService, | ||
| } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb'; | ||
| import { Command } from 'commander'; | ||
| import { afterEach, beforeEach, describe, expect, test, vi, type MockInstance } from 'vitest'; | ||
| import StatusProposalCommand from '../src/commands/proposal/commands/status.js'; | ||
| import { Client } from '../src/core/client/client.js'; | ||
|
|
||
| type GetProposalsResponse = MessageInitShape<typeof GetProposalsByFederatedGraphResponseSchema>; | ||
|
|
||
| function createMockTransport( | ||
| response: GetProposalsResponse, | ||
| onGetProposals?: (req: GetProposalsByFederatedGraphRequest) => void, | ||
| ) { | ||
| return createRouterTransport(({ service }) => { | ||
| service(PlatformService, { | ||
| getProposalsByFederatedGraph: (req) => { | ||
| onGetProposals?.(req); | ||
| return response; | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function runStatus( | ||
| response: GetProposalsResponse, | ||
| args: string[] = [], | ||
| onGetProposals?: (req: GetProposalsByFederatedGraphRequest) => void, | ||
| ): Promise<void> { | ||
| const client: Client = { | ||
| platform: createClient(PlatformService, createMockTransport(response, onGetProposals)), | ||
| }; | ||
| const program = new Command(); | ||
| program.exitOverride(); | ||
| program.addCommand(StatusProposalCommand({ client })); | ||
| await program.parseAsync(['status', 'my-proposal', '--federation-graph', 'my-graph', ...args], { from: 'user' }); | ||
| } | ||
|
|
||
| describe('proposal status', () => { | ||
| let logSpy: MockInstance<typeof console.log>; | ||
| let errorSpy: MockInstance<typeof console.error>; | ||
|
|
||
| beforeEach(() => { | ||
| logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); | ||
| errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.exitCode = undefined; | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('prints the proposal status as text', async () => { | ||
| let request: GetProposalsByFederatedGraphRequest | undefined; | ||
|
|
||
| await runStatus( | ||
| { | ||
| response: { code: EnumStatusCode.OK }, | ||
| proposals: [{ name: 'my-proposal', state: 'DRAFT' }], | ||
| totalCount: 1, | ||
| }, | ||
| ['--namespace', 'production'], | ||
| (req) => { | ||
| request = req; | ||
| }, | ||
| ); | ||
|
|
||
| expect(request).toMatchObject({ | ||
| federatedGraphName: 'my-graph', | ||
| namespace: 'production', | ||
| proposalName: 'my-proposal', | ||
| limit: 1, | ||
| offset: 0, | ||
| }); | ||
| expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Proposal 'my-proposal' status is DRAFT.")); | ||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('prints the proposal status as json', async () => { | ||
| await runStatus( | ||
| { | ||
| response: { code: EnumStatusCode.OK }, | ||
| proposals: [{ name: 'my-proposal', state: 'APPROVED' }], | ||
| totalCount: 1, | ||
| }, | ||
| ['--json'], | ||
| ); | ||
|
|
||
| expect(logSpy).toHaveBeenCalledWith(JSON.stringify({ status: 'APPROVED' })); | ||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('uses the default namespace when none is specified', async () => { | ||
| let request: GetProposalsByFederatedGraphRequest | undefined; | ||
|
|
||
| await runStatus( | ||
| { | ||
| response: { code: EnumStatusCode.OK }, | ||
| proposals: [{ name: 'my-proposal', state: 'DRAFT' }], | ||
| totalCount: 1, | ||
| }, | ||
| [], | ||
| (req) => { | ||
| request = req; | ||
| }, | ||
| ); | ||
|
|
||
| expect(request?.namespace).toBe('default'); | ||
| }); | ||
|
|
||
| test('prints a json error and sets a non-zero exit code when the proposal is not found', async () => { | ||
| await runStatus( | ||
| { | ||
| response: { code: EnumStatusCode.OK }, | ||
| proposals: [], | ||
| totalCount: 0, | ||
| }, | ||
| ['--json'], | ||
| ); | ||
|
|
||
| expect(JSON.parse(String(logSpy.mock.calls[0][0]))).toEqual({ | ||
| status: 'error', | ||
| details: "Proposal 'my-proposal' not found.", | ||
| }); | ||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test('prints the control-plane error and sets a non-zero exit code', async () => { | ||
| await runStatus({ | ||
| response: { | ||
| code: EnumStatusCode.ERR_NOT_FOUND, | ||
| details: 'Federated graph my-graph not found', | ||
| }, | ||
| proposals: [], | ||
| totalCount: 0, | ||
| }); | ||
|
|
||
| expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Federated graph my-graph not found')); | ||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBe(1); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
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.
Don't add
try/catchblock here even if agent tells you to. I'll try and document when this is required or not.We don't really catch exceptions for code that calls RPC because that would make the codebase a bit tangled. Instead the CLI application has a top-level error handling in
cli/src/index.tsthat we use for catching unexpected errors.But in most cases, network failures are handled by checking
resp.response?.code !== EnumStatusCode.OK. The functions such asfindProposalare then wrappers around RPC code and they model a result object withsuccessproperty.