Skip to content

Commit

Permalink
feat: add push-status command
Browse files Browse the repository at this point in the history
  • Loading branch information
SmoliyY committed Dec 7, 2023
1 parent 1b30ccd commit c8eeec6
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { handlePushStatus } from '../push-status';
import { PushStatusResponse } from '@redocly/openapi-core';

const remotes = {
getPushStatus: jest.fn(),
getRemotesList: jest.fn(),
};

jest.mock('@redocly/openapi-core', () => ({
...jest.requireActual('@redocly/openapi-core'),
BlueHarvestApiClient: jest.fn().mockImplementation(function (this: any, ...args) {
this.remotes = remotes;
}),
}));

describe('handlePushStatus()', () => {
const pushResponseStub: PushStatusResponse = {
buildUrlLogs: 'https://test-build-url-logs',
deploymentStatus: 'SUCCEEDED',
scorecard: [],
status: 'SUCCEEDED',
url: 'https://test-url',
};

beforeEach(() => {
remotes.getRemotesList.mockResolvedValueOnce({items:[{ id: 'test-remote-id' }]});

remotes.getPushStatus.mockResolvedValueOnce(pushResponseStub);

jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
jest.spyOn(process.stdout, 'write').mockImplementation(() => true);

});

it('should return success push status', async () => {
const mockConfig = { apis: {} } as any;
process.env.REDOCLY_AUTHORIZATION = 'test-api-key';

await handlePushStatus(
{
domain: 'test-domain',
mountPath: 'test-mount-path',
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
format: 'json'
},
mockConfig
);

expect(remotes.getRemotesList).toHaveBeenCalledWith(
'test-org',
'test-project',
'test-mount-path'
);
expect(process.stderr.write).toHaveBeenCalledTimes(2)
expect(process.stdout.write).toHaveBeenCalledWith(JSON.stringify({
status: 'Success',
buildUrlLogs: 'https://test-build-url-logs',
url: 'https://test-url',
}, null,2))

});
});
126 changes: 75 additions & 51 deletions packages/cli/src/blue-harvest/commands/push-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { getDomain } from '../domains';
import { getApiKeys } from '../api-keys';

import { BlueHarvestApiClient, Config } from '@redocly/openapi-core';
import { exitWithError, printExecutionTime } from '../../utils';
import {
BlueHarvestApiClient,
Config,
PushStatusBase,
PushStatusResponse,
ScorecardItem,
} from '@redocly/openapi-core';
import { cleanColors, exitWithError, printExecutionTime } from '../../utils';
import * as colors from 'colorette';
import { BaseStatus, PushStatusResponse, ScorecardItem } from 'core/src/redocly/blue-harvest/types';

export type PushStatusOptions = {
organization: string;
Expand All @@ -13,6 +18,7 @@ export type PushStatusOptions = {
pushId: string;
domain?: string;
config?: string;
format?: 'stylish' | 'json';
};

export async function handlePushStatus(argv: PushStatusOptions, config: Config) {
Expand Down Expand Up @@ -57,75 +63,66 @@ export async function handlePushStatus(argv: PushStatusOptions, config: Config)
remoteId: remote.id,
});

process.stderr.write(`\nProcessed push-status for ${colors.yellow(orgId)}, ${colors.yellow(projectId)} and mount path ${colors.yellow(mountPath)}.\n`);

printPushStatus({
...pushStatus,
scorecard: [
{
name: 'test',
status: 'SUCCEEDED',
description: 'test',
targetUrl:
'http://project-2--rem-cicd-rem-01hfphqss3xpe0a9ybk0b252dc-main.preview.localhost',
},
{
name: 'test',
status: 'SUCCEEDED',
description: 'test',
targetUrl:
'http://project-2--rem-cicd-rem-01hfphqss3xpe0a9ybk0b252dc-main.preview.localhost',
},
],
});

printExecutionTime(
'push-status',
startedAt,
'Finished'
process.stderr.write(
`\nProcessed push-status for ${colors.yellow(orgId)}, ${colors.yellow(
projectId
)} and mount path ${colors.yellow(mountPath)}.\n\n`
);

if (argv.format === 'stylish') {
printPushStatus(pushStatus);
} else {
process.stdout.write(JSON.stringify(formatPushStatusToJson(pushStatus), null, 2));
}

printExecutionTime('push-status', startedAt, 'Finished');
} catch (err) {
exitWithError(`✗ Failed to get push status. Reason: ${err.message}\n`);
}
}

function printPushStatus(pushStatus: PushStatusResponse) {
if ('build' in pushStatus && 'deployment' in pushStatus) {
process.stderr.write(`\n${colors.blue('Build')}:\n
${colors.magenta('Url')}: ${colors.cyan(pushStatus.build.url)}
${colors.magenta('Status')}: ${getDeploymentAndBuildStatuses(pushStatus.build.status)}\n\n`);

process.stderr.write(`${colors.blue('Deploy')}:\n
${colors.magenta('Url')}: ${colors.cyan(pushStatus.deployment.url)}
${colors.magenta('Status')}: ${getDeploymentAndBuildStatuses(pushStatus.build.status)}\n`);
function printPushStatus(pushStatusResponse: PushStatusResponse) {
if (
pushStatusResponse.status === 'CONTENT_OUTDATED' ||
pushStatusResponse.status === 'NO_CHANGES' ||
pushStatusResponse.status === 'PROCESSED'
) {
process.stderr.write(`Push status is not processed. Reason: ${pushStatusResponse.status}.\n`);
return;
}

if (pushStatus.build.status === 'SUCCEEDED' && pushStatus.deployment.status === 'SUCCEEDED') {
process.stderr.write(
`${colors.blue('Preview url')}: ${colors.cyan(pushStatus.previewUrl)} \n`
);
}
process.stderr.write(
`${colors.magenta('Status')}: ${getDeploymentAndBuildStatuses(pushStatusResponse.status)}\n\n`
);
process.stderr.write(
`${colors.magenta('Build url logs')}: ${colors.cyan(pushStatusResponse.buildUrlLogs)}\n`
);

if (
pushStatusResponse.status === 'SUCCEEDED' &&
pushStatusResponse.deploymentStatus === 'SUCCEEDED'
) {
process.stderr.write(`${colors.blue('URL')}: ${colors.cyan(pushStatusResponse.url)}\n`);
}

if (pushStatus.scorecard.length) {
printScorecard(pushStatus.scorecard);
}
} else {
process.stderr.write(`Build for push is not started.Reason: ${pushStatus.status}\n`);
if (pushStatusResponse.scorecard.length) {
printScorecard(pushStatusResponse.scorecard);
}
}

function printScorecard(scorecard: ScorecardItem[]) {
process.stderr.write(`\n${colors.blue('Scorecard')}:\n`);
process.stderr.write(`\n${colors.magenta('Scorecard')}:\n`);
for (const scorecardItem of scorecard) {
process.stderr.write(`
${colors.magenta('Name')}: ${scorecardItem.name}
${colors.magenta('Status')}: ${getDeploymentAndBuildStatuses(scorecardItem.status)}
${colors.magenta('Url')}: ${colors.cyan(scorecardItem.targetUrl)}
${colors.magenta('URL')}: ${colors.cyan(scorecardItem.targetUrl)}
${colors.magenta('Description')}: ${scorecardItem.description}\n`);
}
}

function getDeploymentAndBuildStatuses(
status: BaseStatus | 'NOT_STARTED' | 'QUEUED' | 'NO_CHANGES'
status: PushStatusBase | 'NOT_STARTED' | 'QUEUED' | 'NO_CHANGES'
) {
switch (status) {
case 'SUCCEEDED':
Expand All @@ -142,3 +139,30 @@ function getDeploymentAndBuildStatuses(
return `${colors.gray('In progress')}`;
}
}

function formatPushStatusToJson(pushStatusResponse: PushStatusResponse) {
if (
pushStatusResponse.status === 'CONTENT_OUTDATED' ||
pushStatusResponse.status === 'NO_CHANGES' ||
pushStatusResponse.status === 'PROCESSED'
) {
return {
status: pushStatusResponse.status,
};
}

return {
status: cleanColors(getDeploymentAndBuildStatuses(pushStatusResponse.status)),
buildUrlLogs: pushStatusResponse.buildUrlLogs,
...(pushStatusResponse.status === 'SUCCEEDED' &&
pushStatusResponse.deploymentStatus === 'SUCCEEDED' && { url: pushStatusResponse.url }),
...(pushStatusResponse.scorecard.length && {
scorecard: pushStatusResponse.scorecard.map((item) => ({
name: item.name,
status: cleanColors(getDeploymentAndBuildStatuses(item.status)),
description: item.description,
url: item.targetUrl,
})),
}),
};
}
Loading

0 comments on commit c8eeec6

Please sign in to comment.