UNPKG

@copado/copado-cli

Version:

Copado Developer CLI

146 lines 8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 theme_1 = __importDefault(require("../../../service/theme")); const git_1 = require("../../../service/git"); const filesystem_1 = require("../../../copado_commons/filesystem"); const restConnections_1 = require("../../../service/restConnections"); const fs_1 = require("fs"); const path = __importStar(require("path")); 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'); class WorkSet extends sf_plugins_core_1.SfCommand { async run() { const { flags } = await this.parse(WorkSet); try { if (!git_1.Git.isGitProjectRoot()) { throw new core_1.SfError(messages.getMessage('set.invalidGitDirectory')); } this.localConfig = await filesystem_1.CopadoFiles.getLocalConfig(); this.userConfig = await filesystem_1.CopadoFiles.getUserConfig(); const fieldName = this.criteriaField(flags); const fieldValue = flags.story || flags.id || flags.extid; await this.assert(fieldName, fieldValue, flags.extid); const body = { credentialName: flags.credential, value: fieldValue, criteria: fieldName }; const ux = new sf_plugins_core_1.Ux({ jsonEnabled: this.jsonEnabled() }); ux.log(theme_1.default.status.Info(`Setting work to ${fieldValue} ...`)); const response = await restConnections_1.RestConnections.restServiceCall('work', 'set', '', body, this.copadoUser()); const extensionConfigFilePath = path.join(filesystem_1.FILE_DIR_PATHS.LOCAL_DIR, filesystem_1.FILE_DIR_PATHS.DOT_COPADO_DIR, filesystem_1.FILE_DIR_PATHS.EXTENSION_CONFIG_FILE); if ((0, fs_1.existsSync)(extensionConfigFilePath)) { (0, fs_1.unlinkSync)(extensionConfigFilePath); } const baseBranch = this.getBaseBranch(response.userStory); const branch = `feature/${response.userStory.Name}`; await git_1.Git.checkoutBranches(baseBranch, branch); await this.saveWorkInProgress(response, baseBranch); if (response.configFile) { this.createConfigFile(response.configFile, extensionConfigFilePath); } if (response.message) { ux.log(theme_1.default.status.Error(response.message)); } ux.log(theme_1.default.status.Success(messages.getMessage('set.successMessage'), branch)); return response; } catch (e) { throw new core_1.SfError(e.message); } } createConfigFile(cliConfig, extensionConfigFilePath) { const cliConfigurationObject = JSON.parse(cliConfig); (0, fs_1.writeFileSync)(extensionConfigFilePath, JSON.stringify(cliConfigurationObject, null, 2)); } criteriaField(flags) { return flags.story ? 'Name' : flags.id ? 'Id' : flags.extid ? this.copadoExtId() : null; } copadoExtId() { return this.userConfig.get('config').externalid; } copadoUser() { return this.userConfig.get('auth').copadousername; } async assert(fieldName, fieldValue, extId) { if (extId && !this.copadoExtId()) { throw new core_1.SfError(messages.getMessage('copadoExtIdNotCofigured')); } else if (!fieldName || !fieldValue) { throw new core_1.SfError(messages.getMessage('set.invalidFlag')); } else if (!(await git_1.Git.checkUnCommitedWork())) { throw new core_1.SfError(`${messages.getMessage('set.unCommitedWork')} ${fieldValue}`); } } async saveWorkInProgress(response, baseBranch) { var _a; const userstoryMap = new Map(); const copadoLastCommit = response.userStory[`${filesystem_1.COPADO_NAMESPACE}User_Story_Commits__r`] ? (_a = response.userStory[`${filesystem_1.COPADO_NAMESPACE}User_Story_Commits__r`].records[0][`${filesystem_1.COPADO_NAMESPACE}External_Id__c`]) === null || _a === void 0 ? void 0 : _a.split('_')[1] : null; userstoryMap.set('copadouserstoryid', response.userStory.Id); userstoryMap.set('copadouserstoryname', response.userStory.Name); userstoryMap.set('copadobasebranch', baseBranch); userstoryMap.set('copadoenvbranch', response.environmentBranch); userstoryMap.set('copadolastcommit', copadoLastCommit); await filesystem_1.CopadoFiles.setInGroup(this.localConfig, userstoryMap, 'work'); } getBaseBranch(userStory) { if (userStory[`${filesystem_1.COPADO_NAMESPACE}Base_Branch__c`]) return userStory[`${filesystem_1.COPADO_NAMESPACE}Base_Branch__c`]; if (userStory[`${filesystem_1.COPADO_NAMESPACE}Release__r`] && userStory[`${filesystem_1.COPADO_NAMESPACE}Release__r`][`${filesystem_1.COPADO_NAMESPACE}Base_Branch__c`]) return userStory[`${filesystem_1.COPADO_NAMESPACE}Release__r`][`${filesystem_1.COPADO_NAMESPACE}Base_Branch__c`]; if (userStory[`${filesystem_1.COPADO_NAMESPACE}Project__r`] && userStory[`${filesystem_1.COPADO_NAMESPACE}Project__r`][`${filesystem_1.COPADO_NAMESPACE}Deployment_Flow__r`]) return userStory[`${filesystem_1.COPADO_NAMESPACE}Project__r`][`${filesystem_1.COPADO_NAMESPACE}Deployment_Flow__r`][`${filesystem_1.COPADO_NAMESPACE}Main_Branch__c`]; throw new core_1.SfError(messages.getMessage('noMainBranchFound')); } } WorkSet.description = messages.getMessage('set.description'); WorkSet.examples = [messages.getMessage('set.example.1'), messages.getMessage('set.example.2'), messages.getMessage('set.example.3')]; WorkSet.flags = { credential: sf_plugins_core_1.Flags.string({ char: 'c', description: messages.getMessage('set.flags.credential') }), id: sf_plugins_core_1.Flags.string({ char: 'i', description: messages.getMessage('set.flags.id'), exclusive: ['story', 'extid'] }), extid: sf_plugins_core_1.Flags.string({ char: 'e', description: messages.getMessage('set.flags.extid'), exclusive: ['story', 'id'] }), story: sf_plugins_core_1.Flags.string({ char: 's', description: messages.getMessage('set.flags.story'), exclusive: ['id', 'extid'] }) }; exports.default = WorkSet; //# sourceMappingURL=set.js.map