Repository navigation
Add typed JSON output to app release #8820
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4762dfa
Add typed JSON output to app release
isaacroldan a1fa51a
Skip success hooks after cancelled JSON releases
isaacroldan 7b7e8f2
Focus release JSON tests on changed behavior
isaacroldan 8e4ebdc
Declare release JSON schema as a static field
isaacroldan 126d935
Keep release JSON conversion focused and preserve release failures
isaacroldan 7ccc6a0
Merge main into app release JSON output
isaacroldan 8f5bc82
Simplify app release version lookup errors
isaacroldan d754374
Preserve app release version lookup failures
isaacroldan 7214923
Scope app release lookups and report missing versions
isaacroldan d9753ca
Merge main into app release JSON output
isaacroldan 89030bb
Restore the app release missing-version text banner
isaacroldan 44c7751
Use the normal release error and completion lifecycle
isaacroldan 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,5 @@ | ||
| --- | ||
| '@shopify/cli': minor | ||
| --- | ||
|
|
||
| Add typed JSON output to app release. |
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
4 changes: 2 additions & 2 deletions
4
packages/app/src/cli/api/graphql/app-management/queries/app-version-by-tag.graphql
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| query AppVersionByTag($versionTag: String!) { | ||
| versionByTag(tag: $versionTag) { | ||
| query AppVersionByTag($appId: ID!, $versionTag: String!) { | ||
| versionByTag(appId: $appId, tag: $versionTag) { | ||
| ...VersionInfo | ||
| } | ||
| } | ||
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,242 @@ | ||
| import Release from './release.js' | ||
| import {release} from '../../services/release.js' | ||
| import {linkedAppContext} from '../../services/app-context.js' | ||
| import {testAppLinked, testOrganizationApp} from '../../models/app/app.test-data.js' | ||
| import {AppManagementClient} from '../../utilities/developer-platform-client/app-management-client.js' | ||
| import {appManagementRequestDoc} from '@shopify/cli-kit/node/api/app-management' | ||
| import {graphqlRequestDoc} from '@shopify/cli-kit/node/api/graphql' | ||
| import {inTemporaryDirectory} from '@shopify/cli-kit/node/fs' | ||
| import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' | ||
| import {renderError} from '@shopify/cli-kit/node/ui' | ||
| import {outputInfo} from '@shopify/cli-kit/node/output' | ||
| import {reportAnalyticsEvent} from '@shopify/cli-kit/node/analytics' | ||
| import {Config} from '@oclif/core' | ||
| import {dirname, joinPath} from '@shopify/cli-kit/node/path' | ||
| import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' | ||
| import {ClientError} from 'graphql-request' | ||
| import {GraphQLError} from 'graphql' | ||
| import {fileURLToPath} from 'node:url' | ||
|
|
||
| const {rawRequest} = vi.hoisted(() => ({rawRequest: vi.fn()})) | ||
|
|
||
| vi.mock('graphql-request', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import('graphql-request')>()), | ||
| GraphQLClient: class { | ||
| requestConfig = {} | ||
| rawRequest = rawRequest | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('../../services/release.js') | ||
| vi.mock('../../services/app-context.js') | ||
| vi.mock('@shopify/cli-kit/node/api/app-management') | ||
| vi.mock('@shopify/cli-kit/node/analytics') | ||
|
|
||
| const version = { | ||
| id: 123, | ||
| uuid: 'gid://shopify/Version/123', | ||
| versionTag: 'v1', | ||
| message: 'Release message', | ||
| location: 'https://dev.shopify.com/dashboard/1/apps/1/versions/123', | ||
| appModuleVersions: [], | ||
| } | ||
|
|
||
| const originalExitCode = process.exitCode | ||
| afterEach(() => { | ||
| process.exitCode = originalExitCode | ||
| vi.unstubAllEnvs() | ||
| AppManagementClient.resetInstance() | ||
| }) | ||
|
|
||
| describe('app release command', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(appManagementRequestDoc).mockImplementation((options) => | ||
| graphqlRequestDoc({ | ||
| ...options, | ||
| api: 'App Management', | ||
| url: 'https://app-management.example/graphql', | ||
| preferredBehaviour: 'non-blocking', | ||
| }), | ||
| ) | ||
| vi.mocked(linkedAppContext).mockResolvedValue({ | ||
| app: testAppLinked(), | ||
| remoteApp: testOrganizationApp(), | ||
| } as Awaited<ReturnType<typeof linkedAppContext>>) | ||
| vi.mocked(release).mockResolvedValue({status: 'success', version}) | ||
| }) | ||
|
|
||
| test('writes one JSON result and sends diagnostics to stderr', async () => { | ||
| vi.mocked(release).mockImplementationOnce(async () => { | ||
| outputInfo('Releasing an app version') | ||
| return {status: 'success', version} | ||
| }) | ||
| await inTemporaryDirectory(async (tmp) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await runRelease(['--path', tmp, '--version', 'v1', '--json', '--allow-updates']) | ||
| expect(JSON.parse(stdout())).toEqual({ | ||
| status: 'success', | ||
| app: {name: 'app1', clientId: 'api-key'}, | ||
| release: {version: {gid: version.uuid, name: 'v1', message: 'Release message', url: version.location}}, | ||
| }) | ||
| expect(JSON.parse(stderr())).toMatchObject({type: 'diagnostic', message: 'Releasing an app version'}) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('release rejection uses one shared error document', async () => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', '1') | ||
| const userErrors = [{message: 'Release failed.', category: 'validation', details: [], field: ['version']}] | ||
| vi.mocked(release).mockResolvedValue({status: 'failed', version, userErrors}) | ||
| await inTemporaryDirectory(async (tmp) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await expect(runRelease(['--path', tmp, '--version', 'v1', '--json', '--allow-updates'])).rejects.toThrow() | ||
| expect(JSON.parse(stdout())).toMatchObject({ | ||
| error: {type: 'abort', message: "Version couldn't be released.", details: {userErrors}}, | ||
| }) | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test.each(['json', 'text'])('missing version retains the %s error output', async (format) => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', format === 'json' ? '1' : '0') | ||
| const {release: releaseService} = | ||
| await vi.importActual<typeof import('../../services/release.js')>('../../services/release.js') | ||
| vi.mocked(release).mockImplementationOnce(releaseService) | ||
| const client = AppManagementClient.getInstance() | ||
| client.token = () => Promise.resolve('token') | ||
| rawRequest.mockRejectedValueOnce( | ||
| new ClientError( | ||
| { | ||
| status: 200, | ||
| headers: new Headers(), | ||
| errors: [ | ||
| new GraphQLError('Query errors', { | ||
| path: ['versionByTag'], | ||
| extensions: {app_errors: {errors: [{category: 'not_found', message: 'The entity could not be found.'}]}}, | ||
| }), | ||
| ], | ||
| }, | ||
| {query: 'query AppVersionByTag { versionByTag { id } }'}, | ||
| ), | ||
| ) | ||
| vi.mocked(linkedAppContext).mockResolvedValueOnce({ | ||
| app: testAppLinked(), | ||
| remoteApp: testOrganizationApp(), | ||
| developerPlatformClient: client, | ||
| } as unknown as Awaited<ReturnType<typeof linkedAppContext>>) | ||
| await inTemporaryDirectory(async (directory) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| const argv = ['--path', directory, '--version', 'missing', '--allow-updates'] | ||
| if (format === 'json') argv.push('--json') | ||
| await expect(runRelease(argv)).rejects.toThrow() | ||
| expect(reportAnalyticsEvent).toHaveBeenCalledExactlyOnceWith( | ||
| expect.objectContaining({ | ||
| errorMessage: format === 'json' ? 'Version missing could not be found.' : "Version couldn't be released.", | ||
| exitMode: 'expected_error', | ||
| }), | ||
| ) | ||
| expect(rawRequest).toHaveBeenCalledWith(expect.stringContaining('versionByTag(appId: $appId'), { | ||
| appId: testOrganizationApp().id, | ||
| versionTag: 'missing', | ||
| }) | ||
| if (format === 'json') { | ||
| expect(JSON.parse(stdout())).toEqual({error: {type: 'abort', message: 'Version missing could not be found.'}}) | ||
| expect(stderr()).toBe('') | ||
| } else { | ||
| expect(stdout()).toBe('') | ||
| const actualBanner = stderr() | ||
| await withCapturedStandardStreams(async ({stderr: expectedStderr}) => { | ||
| renderError({ | ||
| headline: "Version couldn't be released.", | ||
| body: ['Version', {userInput: 'missing'}, 'could not be found.'], | ||
| }) | ||
| expect(actualBanner).toBe(expectedStderr()) | ||
| }) | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test.each([ | ||
| {status: 503, message: 'Service unavailable'}, | ||
| {status: 403, message: 'Access denied'}, | ||
| {status: 404, message: 'Cannot find a valid organization'}, | ||
| ])('preserves the lookup failure in JSON: HTTP $status', async ({status, message}) => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', '1') | ||
| const {release: releaseService} = | ||
| await vi.importActual<typeof import('../../services/release.js')>('../../services/release.js') | ||
| vi.mocked(release).mockImplementationOnce(releaseService) | ||
| const client = AppManagementClient.getInstance() | ||
| client.token = () => Promise.resolve('token') | ||
| const releaseRequest = vi.spyOn(client, 'release') | ||
| rawRequest.mockRejectedValueOnce( | ||
| new ClientError( | ||
| {status, headers: new Headers({'x-request-id': 'lookup-request-id'}), errors: [new GraphQLError(message)]}, | ||
| {query: 'query AppVersionByTag { versionByTag { id } }'}, | ||
| ), | ||
| ) | ||
| vi.mocked(linkedAppContext).mockResolvedValueOnce({ | ||
| app: testAppLinked(), | ||
| remoteApp: testOrganizationApp(), | ||
| developerPlatformClient: client, | ||
| } as unknown as Awaited<ReturnType<typeof linkedAppContext>>) | ||
| await inTemporaryDirectory(async (directory) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await expect( | ||
| runRelease([ | ||
| '--path', | ||
| directory, | ||
| '--version', | ||
| 'v1', | ||
| '--json', | ||
| '--allow-updates', | ||
| '--allow-deletes', | ||
| '--no-input', | ||
| ]), | ||
| ).rejects.toThrow() | ||
| const {error} = JSON.parse(stdout()) | ||
| expect(error).toMatchObject({type: 'abort', message: expect.stringContaining(message)}) | ||
| expect(error.message).toContain('Request ID: lookup-request-id') | ||
| if (status === 403) expect(error.tryMessage).toContain('shopify auth login') | ||
| expect(stderr()).toBe('') | ||
| expect(releaseRequest).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('cancelled JSON runs postrun without an explicit exit', async () => { | ||
| vi.mocked(release).mockResolvedValue({status: 'cancelled'}) | ||
| const exit = vi.spyOn(process, 'exit').mockReturnValue(undefined as never) | ||
| try { | ||
| await inTemporaryDirectory(async (directory) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| const config = await releaseConfig() | ||
| const metadata = config.findCommand('app:release')! | ||
| vi.spyOn(config, 'findCommand').mockReturnValue({...metadata, id: 'app:release', load: async () => Release}) | ||
| const hooks = vi.spyOn(config, 'runHook').mockResolvedValue({successes: [], failures: []}) | ||
| const argv = ['--path', directory, '--version', 'v1', '--json', '--allow-updates'] | ||
| const result = await config.runCommand('app:release', argv) | ||
| expect(result).toEqual({app: testAppLinked()}) | ||
| expect(hooks).toHaveBeenCalledWith('postrun', {argv, Command: Release, result}) | ||
| expect(exit).not.toHaveBeenCalled() | ||
| expect(JSON.parse(stdout())).toEqual({status: 'cancelled'}) | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| }) | ||
| } finally { | ||
| exit.mockRestore() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| async function runRelease(argv: string[]) { | ||
| return Release.run(argv, await releaseConfig()) | ||
| } | ||
|
|
||
| async function releaseConfig() { | ||
| const config = await Config.load({root: joinPath(dirname(fileURLToPath(import.meta.url)), '../../../..')}) | ||
| // The source package is bundled in the installed CLI, not a custom plugin. | ||
| config.plugins.delete('@shopify/app') | ||
| return config | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.