Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 65 additions & 15 deletions src/commands/launch/rollback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,73 @@ describe('Rollback Command', () => {
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 noEligibleResponse = {
data: {
Environments: {
edges: [
{
node: {
uid: 'env-uid',
name: 'Default',
deployments: { edges: [{ node: liveDeployment }] },
},
],
},
},
],
},
};
const query = jest.fn().mockResolvedValueOnce(noEligibleResponse);
},
};

it('exits when no rollback-eligible deployments are available', async () => {
const query = jest
.fn()
.mockResolvedValueOnce(noEligibleResponse)
.mockResolvedValueOnce({
data: { RollbackSettings: { isEnabled: true, retentionCount: 3 } },
});
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('shows a plan-not-available message when instant rollback is not entitled', async () => {
const query = jest
.fn()
.mockResolvedValueOnce(noEligibleResponse)
.mockResolvedValueOnce({
data: { RollbackSettings: { isEnabled: false, retentionCount: 0 } },
});
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(
'Instant rollback isn\'t available on your organization\'s plan.',
'error',
);
});

it('falls back to the generic message when the settings fetch fails', async () => {
const query = jest
.fn()
.mockResolvedValueOnce(noEligibleResponse)
.mockRejectedValueOnce(new Error('network error'));
const mutate = jest.fn();
const cmd = buildCommand({ environment: 'Default' }, query, mutate);
jest
Expand Down
20 changes: 19 additions & 1 deletion src/commands/launch/rollback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
environmentsQuery,
latestLiveDeploymentQuery,
rollbackDeploymentMutation,
rollbackSettingsQuery,
} from '../../graphql';
import { Logger, selectOrg, selectProject } from '../../util';

Expand Down Expand Up @@ -88,7 +89,12 @@ export default class Rollback extends BaseCommand<typeof Rollback> {
const eligibleSorted = this.getEligibleSortedDeployments(environment, currentLive?.uid);

if (isEmpty(eligibleSorted)) {
this.log('No rollback-eligible deployments are available for this environment.', 'error');
const settings = await this.fetchRollbackSettings();
if (settings?.isEnabled === false) {
this.log('Instant rollback isn\'t available on your organization\'s plan.', 'error');
} else {
this.log('No rollback-eligible deployments are available for this environment.', 'error');
}
process.exit(1);
}

Expand Down Expand Up @@ -194,6 +200,18 @@ export default class Rollback extends BaseCommand<typeof Rollback> {
.catch(() => undefined);
}

/**
* @method fetchRollbackSettings - org-level entitlement; undefined on any fetch failure
*
* @memberof Rollback
*/
async fetchRollbackSettings(): Promise<{ isEnabled: boolean; retentionCount: number } | undefined> {
return this.apolloClient
.query({ query: rollbackSettingsQuery })
.then(({ data }) => data?.RollbackSettings)
.catch(() => undefined);
}

/**
* @method getEligibleSortedDeployments - eligible deployments excluding current live, sorted by number desc
*
Expand Down
10 changes: 10 additions & 0 deletions src/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ const latestLiveDeploymentQuery: DocumentNode = gql`
}
`;

const rollbackSettingsQuery: DocumentNode = gql`
query RollbackSettings {
RollbackSettings {
isEnabled
retentionCount
}
}
`;

const environmentsQuery: DocumentNode = gql`
query Environments($skipRollbackData: Boolean = true) {
Environments {
Expand Down Expand Up @@ -228,4 +237,5 @@ export {
serverlessLogsQuery,
latestLiveDeploymentQuery,
environmentsQuery,
rollbackSettingsQuery,
};
Loading