-
Notifications
You must be signed in to change notification settings - Fork 0
CL-1753 | Add instant rollback feature support for Launch CLI #158
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| import Rollback from './rollback'; | ||
| import { Logger } from '../../util'; | ||
| import { cliux } from '@contentstack/cli-utilities'; | ||
|
|
||
| jest.mock('../../util', () => { | ||
| const actual = jest.requireActual('../../util'); | ||
| return { | ||
| ...actual, | ||
| Logger: jest.fn(), | ||
| selectOrg: jest.fn(), | ||
| selectProject: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock('@contentstack/cli-utilities', () => { | ||
| const actual = jest.requireActual('@contentstack/cli-utilities'); | ||
| return { | ||
| ...actual, | ||
| configHandler: { | ||
| get: jest.fn((key) => { | ||
| if (key === 'authtoken') return 'dummy-token'; | ||
| if (key === 'authorisationType') return 'OAuth'; | ||
| if (key === 'oauthAccessToken') return 'dummy-oauth-token'; | ||
| return undefined; | ||
| }), | ||
| }, | ||
| cliux: { | ||
| ...actual.cliux, | ||
| inquire: jest.fn(), | ||
| print: jest.fn(), | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const targetDeployment = { | ||
| uid: 'target-uid', | ||
| status: 'ARCHIVED', | ||
| gitBranch: 'main', | ||
| commitHash: 'abcdef1', | ||
| createdAt: '2026-04-29T00:00:00Z', | ||
| commitMessage: 'previous good build', | ||
| deploymentUrl: 'https://example.com', | ||
| deploymentNumber: 2, | ||
| isRollbackEligible: true, | ||
| }; | ||
|
|
||
| const liveDeployment = { | ||
| ...targetDeployment, | ||
| uid: 'live-uid', | ||
| status: 'LIVE', | ||
| deploymentNumber: 3, | ||
| }; | ||
|
|
||
| const environmentsResponse = { | ||
| data: { | ||
| Environments: { | ||
| edges: [ | ||
| { | ||
| node: { | ||
| uid: 'env-uid', | ||
| name: 'Default', | ||
| deployments: { | ||
| edges: [ | ||
| { node: liveDeployment }, | ||
| { node: targetDeployment }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const buildCommand = (flags: Record<string, any> = {}, queryImpl?: jest.Mock, mutateImpl?: jest.Mock) => { | ||
| const cmd = new Rollback([], {} as any); | ||
| (cmd as any).flags = flags; | ||
| (cmd as any).log = jest.fn(); | ||
| (cmd as any).logger = { log: jest.fn() }; | ||
| (cmd as any).sharedConfig = { currentConfig: { uid: 'project-uid' } }; | ||
| (cmd as any).apolloClient = { | ||
| query: queryImpl || jest.fn(), | ||
| mutate: mutateImpl || jest.fn(), | ||
| }; | ||
| return cmd; | ||
| }; | ||
|
|
||
| describe('Rollback Command', () => { | ||
| let exitMock: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| (Logger as jest.Mock).mockImplementation(() => ({ log: jest.fn() })); | ||
| exitMock = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { | ||
| throw new Error(`process.exit:${code}`); | ||
| }) as any); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('exits when no rollback-eligible deployments are available', async () => { | ||
| const noEligibleResponse = { | ||
| data: { | ||
| Environments: { | ||
| edges: [ | ||
| { | ||
| node: { | ||
| uid: 'env-uid', | ||
| name: 'Default', | ||
| deployments: { edges: [{ node: liveDeployment }] }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
| const query = jest.fn().mockResolvedValueOnce(noEligibleResponse); | ||
| const mutate = jest.fn(); | ||
| const cmd = buildCommand({ environment: 'Default' }, query, mutate); | ||
| jest | ||
| .spyOn(cmd as any, 'fetchCurrentLiveDeployment') | ||
| .mockResolvedValueOnce(liveDeployment); | ||
|
|
||
| await expect((cmd as any).rollbackDeployment()).rejects.toThrow('process.exit:1'); | ||
|
|
||
| expect(mutate).not.toHaveBeenCalled(); | ||
| expect(exitMock).toHaveBeenCalledWith(1); | ||
| expect((cmd as any).log).toHaveBeenCalledWith( | ||
| 'No rollback-eligible deployments are available for this environment.', | ||
| 'error', | ||
| ); | ||
| }); | ||
|
|
||
| it('exits when --deployment flag does not match an eligible deployment', async () => { | ||
| const query = jest.fn().mockResolvedValueOnce(environmentsResponse); | ||
| const mutate = jest.fn(); | ||
| const cmd = buildCommand( | ||
| { environment: 'Default', deployment: 'unknown-uid' }, | ||
| query, | ||
| mutate, | ||
| ); | ||
| jest | ||
| .spyOn(cmd as any, 'fetchCurrentLiveDeployment') | ||
| .mockResolvedValueOnce(liveDeployment); | ||
|
|
||
| await expect((cmd as any).rollbackDeployment()).rejects.toThrow('process.exit:1'); | ||
|
|
||
| expect(mutate).not.toHaveBeenCalled(); | ||
| expect(exitMock).toHaveBeenCalledWith(1); | ||
| expect((cmd as any).log).toHaveBeenCalledWith( | ||
| 'Provided deployment UID is not rollback-eligible or does not exist.', | ||
| 'error', | ||
| ); | ||
| }); | ||
|
|
||
| it('skips the mutation when the user does not confirm', async () => { | ||
| const query = jest.fn().mockResolvedValueOnce(environmentsResponse); | ||
| const mutate = jest.fn(); | ||
| const cmd = buildCommand( | ||
| { environment: 'Default', deployment: 'target-uid', reason: 'audit' }, | ||
| query, | ||
| mutate, | ||
| ); | ||
| jest | ||
| .spyOn(cmd as any, 'fetchCurrentLiveDeployment') | ||
| .mockResolvedValueOnce(liveDeployment); | ||
| (cliux.inquire as jest.Mock).mockResolvedValueOnce(false); // confirm prompt | ||
|
|
||
| await (cmd as any).rollbackDeployment(); | ||
|
|
||
| expect(mutate).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('fires the rollback mutation and prints the success message', async () => { | ||
| const query = jest.fn().mockResolvedValueOnce(environmentsResponse); | ||
| const mutate = jest.fn().mockResolvedValueOnce({ | ||
| data: { | ||
| rollbackDeployment: { status: 'PENDING', environmentUid: 'env-uid' }, | ||
| }, | ||
| }); | ||
| const cmd = buildCommand( | ||
| { environment: 'Default', deployment: 'target-uid', reason: 'restoring' }, | ||
| query, | ||
| mutate, | ||
| ); | ||
| jest | ||
| .spyOn(cmd as any, 'fetchCurrentLiveDeployment') | ||
| .mockResolvedValueOnce(liveDeployment); | ||
| (cliux.inquire as jest.Mock).mockResolvedValueOnce(true); | ||
|
|
||
| await (cmd as any).rollbackDeployment(); | ||
|
|
||
| expect(mutate).toHaveBeenCalledTimes(1); | ||
| const variables = mutate.mock.calls[0][0].variables; | ||
| expect(variables).toEqual({ | ||
| input: { | ||
| deployment: 'target-uid', | ||
| environment: 'env-uid', | ||
| reason: 'restoring', | ||
| }, | ||
| }); | ||
| expect(exitMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('logs an error and exits when the rollback mutation fails', async () => { | ||
| const query = jest.fn().mockResolvedValueOnce(environmentsResponse); | ||
| const error = Object.assign(new Error('boom'), { | ||
| graphQLErrors: [{ extensions: { exception: { name: 'DeploymentRollbackFailed' } } }], | ||
| }); | ||
| const mutate = jest.fn().mockRejectedValueOnce(error); | ||
| const cmd = buildCommand( | ||
| { environment: 'Default', deployment: 'target-uid' }, | ||
| query, | ||
| mutate, | ||
| ); | ||
| jest | ||
| .spyOn(cmd as any, 'fetchCurrentLiveDeployment') | ||
| .mockResolvedValueOnce(liveDeployment); | ||
| (cliux.inquire as jest.Mock) | ||
| .mockResolvedValueOnce('') // reason | ||
| .mockResolvedValueOnce(true); // confirm | ||
|
|
||
| await expect((cmd as any).rollbackDeployment()).rejects.toThrow('process.exit:1'); | ||
|
|
||
| expect(mutate).toHaveBeenCalledTimes(1); | ||
| expect(exitMock).toHaveBeenCalledWith(1); | ||
| expect((cmd as any).log).toHaveBeenCalledWith( | ||
| 'Rollback failed. Please try again. (DeploymentRollbackFailed)', | ||
| 'error', | ||
| ); | ||
| }); | ||
| }); | ||
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.