-
Notifications
You must be signed in to change notification settings - Fork 250
feat: add approve command #3145
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
Draft
alepane21
wants to merge
2
commits into
main
Choose a base branch
from
ale/cosmo-389-cli-approve-schema-proposals-via-wgc
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,59 @@ | ||
| import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; | ||
| import { Command, InvalidArgumentError } from 'commander'; | ||
| import pc from 'picocolors'; | ||
| import { getBaseHeaders } from '../../../core/config.js'; | ||
| import { BaseCommandOptions } from '../../../core/types/types.js'; | ||
|
|
||
| const proposalStatuses = ['APPROVED', 'CLOSED'] as const; | ||
| type ProposalStatus = (typeof proposalStatuses)[number]; | ||
|
|
||
| const parseProposalStatus = (value: string): ProposalStatus => { | ||
| const status = value.toUpperCase(); | ||
| if (!proposalStatuses.includes(status as ProposalStatus)) { | ||
| throw new InvalidArgumentError('Allowed values are approved and closed.'); | ||
| } | ||
| return status as ProposalStatus; | ||
| }; | ||
|
|
||
| export default (opts: BaseCommandOptions) => { | ||
| const command = new Command('update-status'); | ||
| command.description('Updates the status of an existing proposal for a federated graph.'); | ||
| command.argument('<name>', 'The name of the proposal to update.'); | ||
| command.requiredOption( | ||
| '-f, --federation-graph <federatedGraphName>', | ||
| 'The name of the federated graph this proposal is for.', | ||
| ); | ||
| command.requiredOption( | ||
| '-s, --status <status>', | ||
| 'The status to set. Allowed values: approved, closed.', | ||
| parseProposalStatus, | ||
| ); | ||
| command.option('-n, --namespace [string]', 'The namespace of the federated graph.', 'default'); | ||
|
|
||
| command.action(async (name, options) => { | ||
| const resp = await opts.client.platform.updateProposal( | ||
| { | ||
| proposalName: name, | ||
| federatedGraphName: options.federationGraph, | ||
| namespace: options.namespace, | ||
| updateAction: { | ||
| case: 'state', | ||
| value: options.status, | ||
| }, | ||
| }, | ||
| { | ||
| headers: getBaseHeaders(), | ||
| }, | ||
| ); | ||
|
|
||
| if (resp.response?.code === EnumStatusCode.OK) { | ||
| console.log(pc.green(`Proposal '${name}' status was updated to ${options.status} successfully.`)); | ||
| return; | ||
| } | ||
|
|
||
| console.error(pc.red(resp.response?.details || `Failed to update status for proposal '${name}'.`)); | ||
| process.exitCode = 1; | ||
| }); | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { type MessageInitShape } from '@bufbuild/protobuf'; | ||
| import { createClient, createRouterTransport } from '@connectrpc/connect'; | ||
| import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; | ||
| import { | ||
| PlatformService, | ||
| type UpdateProposalRequest, | ||
| UpdateProposalResponseSchema, | ||
| } 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 UpdateProposalStatusCommand from '../src/commands/proposal/commands/update-status.js'; | ||
| import { Client } from '../src/core/client/client.js'; | ||
|
|
||
| function createMockTransport( | ||
| response: MessageInitShape<typeof UpdateProposalResponseSchema>, | ||
| onUpdateProposal?: (req: UpdateProposalRequest) => void, | ||
| ) { | ||
| return createRouterTransport(({ service }) => { | ||
| service(PlatformService, { | ||
| updateProposal: (req) => { | ||
| onUpdateProposal?.(req); | ||
| return response; | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function runUpdateStatus( | ||
| response: MessageInitShape<typeof UpdateProposalResponseSchema>, | ||
| args: string[] = [], | ||
| onUpdateProposal?: (req: UpdateProposalRequest) => void, | ||
| ): Promise<void> { | ||
| const client: Client = { | ||
| platform: createClient(PlatformService, createMockTransport(response, onUpdateProposal)), | ||
| }; | ||
| const program = new Command(); | ||
| program.exitOverride(); | ||
| const command = UpdateProposalStatusCommand({ client }); | ||
| command.exitOverride(); | ||
| program.addCommand(command); | ||
| await program.parseAsync( | ||
| ['update-status', 'my-proposal', '--federation-graph', 'my-graph', '--status', 'approved', ...args], | ||
| { from: 'user' }, | ||
| ); | ||
| } | ||
|
|
||
| describe('update 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('approves a proposal in the default namespace', async () => { | ||
| let request: UpdateProposalRequest | undefined; | ||
|
|
||
| await runUpdateStatus({ response: { code: EnumStatusCode.OK } }, [], (req) => { | ||
| request = req; | ||
| }); | ||
|
|
||
| expect(request).toMatchObject({ | ||
| proposalName: 'my-proposal', | ||
| federatedGraphName: 'my-graph', | ||
| namespace: 'default', | ||
| updateAction: { | ||
| case: 'state', | ||
| value: 'APPROVED', | ||
| }, | ||
| }); | ||
| expect(logSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining("Proposal 'my-proposal' status was updated to APPROVED successfully."), | ||
| ); | ||
| expect(errorSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('closes a proposal in an explicitly provided namespace', async () => { | ||
| let request: UpdateProposalRequest | undefined; | ||
|
|
||
| await runUpdateStatus( | ||
| { response: { code: EnumStatusCode.OK } }, | ||
| ['--status', 'CLOSED', '--namespace', 'production'], | ||
| (req) => { | ||
| request = req; | ||
| }, | ||
| ); | ||
|
|
||
| expect(request).toMatchObject({ | ||
| namespace: 'production', | ||
| updateAction: { | ||
| case: 'state', | ||
| value: 'CLOSED', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test('rejects statuses that cannot be set manually', async () => { | ||
| let updateCalled = false; | ||
|
|
||
| await expect( | ||
| runUpdateStatus({ response: { code: EnumStatusCode.OK } }, ['--status', 'published'], () => { | ||
| updateCalled = true; | ||
| }), | ||
| ).rejects.toThrow('Allowed values are approved and closed.'); | ||
|
|
||
| expect(updateCalled).toBe(false); | ||
| }); | ||
|
|
||
| test('prints the control-plane error and sets a non-zero exit code', async () => { | ||
| await runUpdateStatus({ | ||
| response: { | ||
| code: EnumStatusCode.ERR_NOT_FOUND, | ||
| details: 'Proposal my-proposal not found', | ||
| }, | ||
| }); | ||
|
|
||
| expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Proposal my-proposal not found')); | ||
| expect(logSpy).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBe(1); | ||
| }); | ||
| }); |
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,52 @@ | ||
| --- | ||
| title: "Update Status" | ||
| description: "Updates the status of an existing proposal for a federated graph." | ||
| icon: arrows-rotate | ||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| npx wgc proposal update-status <name> --federation-graph <federated-graph-name> --status <status> [options] | ||
| ``` | ||
|
|
||
| ## Description | ||
|
|
||
| The `npx wgc proposal update-status` command updates an existing proposal's review status. The command is non-interactive, which makes it suitable for CI workflows. | ||
|
|
||
| The API key used by the command must have write access to the federated graph. | ||
|
|
||
| ## **Parameters** | ||
|
|
||
| - `[name]`: The name of the proposal to update. | ||
|
|
||
| ## **Options** | ||
|
|
||
| - `-f, --federation-graph <federatedGraphName>` (required): The name of the federated graph this proposal is for. | ||
| - `-s, --status <status>` (required): The status to set. Accepted values are `approved` and `closed`. Values are case-insensitive. | ||
| - `-n, --namespace [namespace]`: The namespace of the federated graph (Default: `default`). | ||
|
|
||
| ## **CI examples** | ||
|
|
||
| Configure an API key as a secret in your CI system and expose it as `COSMO_API_KEY` before running the command. | ||
|
|
||
| Approve a proposal: | ||
|
|
||
| ```bash | ||
| export COSMO_API_KEY=<API_KEY> | ||
| npx wgc proposal update-status product-changes \ | ||
| --federation-graph my-graph \ | ||
| --namespace production \ | ||
| --status approved | ||
| ``` | ||
|
|
||
| Close a proposal: | ||
|
|
||
| ```bash | ||
| npx wgc proposal update-status product-changes \ | ||
| --federation-graph my-graph \ | ||
| --namespace production \ | ||
| --status closed | ||
| ``` | ||
|
|
||
| The command exits with a non-zero status when the proposal status cannot be updated, allowing the CI job to fail immediately. |
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
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: wundergraph/cosmo
Length of output: 25824
🏁 Script executed:
Repository: wundergraph/cosmo
Length of output: 14478
🏁 Script executed:
Repository: wundergraph/cosmo
Length of output: 34369
Require a value for
--namespace. Change[string]to<namespace>. Updatedocs-website/cli/proposal/update-status.mdxto match.🤖 Prompt for AI Agents