UNPKG

@copado/copado-cli

Version:

Copado Developer CLI

192 lines 10.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@salesforce/core"); const kit_1 = require("@salesforce/kit"); const theme_1 = __importDefault(require("../../../service/theme")); const filesystem_1 = require("../../../copado_commons/filesystem"); const restConnections_1 = require("../../../service/restConnections"); const promotedUserStories_1 = __importDefault(require("../../../selector/promotedUserStories")); const sf_plugins_core_1 = require("@salesforce/sf-plugins-core"); core_1.Messages.importMessagesDirectory(__dirname); const messages = core_1.Messages.loadMessages('@copado/copado-cli', 'copado_work'); // Map polling time to counter const SECONDS_TO_COUNTER = new Map([ [10, 12], [30, 10], [60, 60], [300, 240] ]); const finishedStatuses = ['Successful', 'Error', 'Canceled']; class WorkSubmit extends sf_plugins_core_1.SfCommand { constructor() { super(...arguments); this.pollCounter = new Map(); } async run() { var _a, _b, _c, _d, _e; let result; const { flags } = await this.parse(WorkSubmit); this.copadoUser = await filesystem_1.CopadoFiles.getCurrentUser(); const workConfig = (_a = (await filesystem_1.CopadoFiles.getLocalConfig())) === null || _a === void 0 ? void 0 : _a.get('work'); this.userStoryName = workConfig === null || workConfig === void 0 ? void 0 : workConfig.copadouserstoryname; const ux = new sf_plugins_core_1.Ux({ jsonEnabled: this.jsonEnabled() }); try { if (flags.validate) { ux.log(`${theme_1.default.status.Info(messages.getMessage('submit.submitting'), this.userStoryName, messages.getMessage('submit.validationOnly'))}`); ux.spinner.start(''); result = await this.start('validate'); await this.showPromotionDetails(ux); } else if (flags.promote) { ux.log(`${theme_1.default.status.Info(messages.getMessage('submit.flagging'), this.userStoryName, messages.getMessage('submit.readyToPromote'))}`); ux.spinner.start(''); result = await this.start('promote'); } else if (flags.deploy) { ux.log(`${theme_1.default.status.Info(messages.getMessage('submit.submitting'), this.userStoryName)}`); ux.spinner.start(''); result = await this.start('deploy'); await this.showPromotionDetails(ux); } else { throw new core_1.SfError(messages.getMessage('submit.invalidFlag')); } if (flags.wait && result.jobExecutionId) { ux.log(`${theme_1.default.status.Info(messages.getMessage('submit.promotionWaiting'))} ${result.jobExecutionId}`); ux.spinner.start(''); result = await this.startPolling(result.jobExecutionId, ux); if (result.status === 'Successful') { const promotedUserStories = await new promotedUserStories_1.default().byUserStoryId(this.userStoryId); if (!promotedUserStories.length) { throw new core_1.SfError(`Invalid data`); } const lastdeployment = ((_c = (_b = promotedUserStories[0]) === null || _b === void 0 ? void 0 : _b.Promotion__r) === null || _c === void 0 ? void 0 : _c.Last_Deployment_Execution_Id__c) || ((_e = (_d = promotedUserStories[0]) === null || _d === void 0 ? void 0 : _d.copado__Promotion__r) === null || _e === void 0 ? void 0 : _e.copado__Last_Deployment_Execution_Id__c); ux.log(`${theme_1.default.status.Info(messages.getMessage('submit.deploymentWaiting'))} ${lastdeployment}`); ux.spinner.start(''); await this.startPolling(lastdeployment, ux); } } else if (!flags.wait && result.jobExecutionId) { this.showJobStatusInfo(result, ux); } } catch (err) { throw new core_1.SfError(theme_1.default.status.Error(err.message)); } return result; } async start(action) { this.userStoryId = await filesystem_1.CopadoFiles.getCopadoUserStoryId(); return await restConnections_1.RestConnections.restServiceCall('work', 'submit', action, { usId: this.userStoryId }, this.copadoUser); } async showPromotionDetails(ux) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; const promotedUserStories = await new promotedUserStories_1.default().byUserStoryId(this.userStoryId); if (!promotedUserStories.length) { throw new core_1.SfError(`Invalid data`); } const destinationEnvironment = ((_c = (_b = (_a = promotedUserStories[0]) === null || _a === void 0 ? void 0 : _a.Promotion__r) === null || _b === void 0 ? void 0 : _b.Destination_Environment__r) === null || _c === void 0 ? void 0 : _c.Name) || ((_f = (_e = (_d = promotedUserStories[0]) === null || _d === void 0 ? void 0 : _d.copado__Promotion__r) === null || _e === void 0 ? void 0 : _e.copado__Destination_Environment__r) === null || _f === void 0 ? void 0 : _f.Name); const promotionName = ((_h = (_g = promotedUserStories[0]) === null || _g === void 0 ? void 0 : _g.Promotion__r) === null || _h === void 0 ? void 0 : _h.Name) || ((_k = (_j = promotedUserStories[0]) === null || _j === void 0 ? void 0 : _j.copado__Promotion__r) === null || _k === void 0 ? void 0 : _k.Name); const promotionId = ((_l = promotedUserStories[0]) === null || _l === void 0 ? void 0 : _l.Promotion__c) || ((_m = promotedUserStories[0]) === null || _m === void 0 ? void 0 : _m.copado__Promotion__c); ux.spinner.stop(); ux.log(`${messages.getMessage('submit.destinationEnvironment')} ${theme_1.default.status.Success(destinationEnvironment)}`); ux.log(`${messages.getMessage('submit.promotionCreated')} ${theme_1.default.status.Success(promotionName, '(', promotionId, ')')}`); } showJobStatusInfo(result, ux) { if (result.jobExecutionId) { ux.log(`${messages.getMessage('submit.checkStatusInstruction')} ${theme_1.default.format.underlineBold(messages.getMessage('submit.statusExample'), result.jobExecutionId)}`); } } async startPolling(jobId, ux) { try { const frequency = this.getFrequency(); const wait = frequency * SECONDS_TO_COUNTER.get(frequency); const body = await this.polling(jobId, frequency, wait); if (body.status === 'Error') { ux.log(`${theme_1.default.status.Error(messages.getMessage('submit.jobError'), body.errorMessage)}`); } else if (body.status === 'Successful') { ux.log(theme_1.default.status.Success(messages.getMessage('submit.jobSuccess'))); } return body; } catch (err) { if (err.name == 'OperationTimedOut') { this.startPolling(jobId, ux); } else { throw new Error(err.message); } } } async polling(jobId, frequency, timeout) { const ux = new sf_plugins_core_1.Ux(); const me = this; const options = { async poll() { const response = await restConnections_1.RestConnections.restServiceCall('job', 'status', '', { jobId }, me.copadoUser); const jobStatus = response.body.status; const isFinished = finishedStatuses.includes(jobStatus); if (isFinished) { ux.spinner.stop(); } else { me.pollCounter.set(frequency, me.pollCounter.get(frequency) + 1); ux.spinner.start(`${messages.getMessage('submit.currentStatus')} ${response.body.progressStatus || jobStatus}`); } return Promise.resolve({ completed: isFinished, payload: response.body }); }, frequency: kit_1.Duration.seconds(frequency), timeout: kit_1.Duration.seconds(timeout), timeoutErrorName: 'OperationTimedOut' }; const client = await core_1.PollingClient.create(options); const pollResult = await client.subscribe(); return pollResult; } getFrequency() { let pollTime = [...this.pollCounter.keys()][0] || 10; const maxCount = SECONDS_TO_COUNTER.get(pollTime); const currentCount = this.pollCounter.get(pollTime) || 0; if (maxCount === currentCount) { this.pollCounter.clear(); pollTime = [...SECONDS_TO_COUNTER.keys()].sort().find(num => num > pollTime); } this.pollCounter.set(pollTime, 0); return pollTime; } } exports.default = WorkSubmit; WorkSubmit.description = messages.getMessage('submit.description'); WorkSubmit.examples = [ messages.getMessage('submit.example.1'), messages.getMessage('submit.example.2'), messages.getMessage('submit.example.3') ]; WorkSubmit.flags = { promote: sf_plugins_core_1.Flags.boolean({ char: 'p', description: messages.getMessage('submit.flags.promote'), default: false, exclusive: ['deploy', 'validate'] }), deploy: sf_plugins_core_1.Flags.boolean({ char: 'd', description: messages.getMessage('submit.flags.deploy'), default: false, exclusive: ['promote', 'validate'] }), validate: sf_plugins_core_1.Flags.boolean({ char: 'v', description: messages.getMessage('submit.flags.validate'), default: false, exclusive: ['promote', 'deploy'] }), wait: sf_plugins_core_1.Flags.boolean({ char: 'w', description: messages.getMessage('submit.flags.wait') }) }; //# sourceMappingURL=submit.js.map