@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
140 lines • 7.17 kB
JavaScript
;
/*
* Copyright (c) 2019 Broadcom. All Rights Reserved. The term
* "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This software and all information contained therein is
* confidential and proprietary and shall not be duplicated,
* used, disclosed, or disseminated in any way except as
* authorized by the applicable license agreement, without the
* express written permission of Broadcom. All authorized
* reproductions must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO
* THE EXTENT PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS
* SOFTWARE WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT
* LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL BROADCOM
* BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR
* DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS
* INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS
* EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const BaseHandler_1 = require("../../BaseHandler");
const object_1 = require("../../../utils/object");
const imperative_1 = require("@zowe/imperative");
const api_1 = require("../../../api");
const fs = require("fs");
const child_process_1 = require("child_process");
const AddElementOptions_1 = require("./AddElementOptions");
class MappingAddElementHandler extends BaseHandler_1.BaseHandler {
static getElementsFromFile(elementsPath) {
let elements;
try {
imperative_1.Logger.getAppLogger().trace(`Reading elements file from: ${elementsPath}`);
const elementsFile = fs.readFileSync(elementsPath, "utf8"); // read with default encoding
imperative_1.Logger.getAppLogger().trace(`Get elements file content: \n${elementsFile}`);
elements = JSON.parse(elementsFile);
}
catch (err) {
throw new imperative_1.ImperativeError({
msg: `${elementsPath} does not contain a valid file.`,
});
}
return elements;
}
processWithSession() {
return __awaiter(this, void 0, void 0, function* () {
let response;
const task = {
percentComplete: imperative_1.TaskProgress.ZERO_PERCENT,
statusMessage: "Validate options",
stageName: imperative_1.TaskStage.IN_PROGRESS
};
this.progress.startBar({ task });
const context = this.getOption(AddElementOptions_1.AddElementOptions.CONTEXT);
const repoName = this.getOption(AddElementOptions_1.AddElementOptions.MAPPING);
const system = this.getOption(AddElementOptions_1.AddElementOptions.ENDEVOR_SYSTEM, false);
const subsystem = this.getOption(AddElementOptions_1.AddElementOptions.ENDEVOR_SUBSYSTEM, false);
const validate = this.getOption(AddElementOptions_1.AddElementOptions.ENDEVOR_VALIDATE, false);
let branchId = this.getOption(AddElementOptions_1.AddElementOptions.BRANCH_ID, false);
const allBranches = this.getOption(AddElementOptions_1.AddElementOptions.ALL_BRANCHES, false);
const withDependencies = this.getOption(AddElementOptions_1.AddElementOptions.WITH_DEPENDENCIES, false);
const forceGetDependencies = this.getOption(AddElementOptions_1.AddElementOptions.FORCE_GET_DEPENDENCIES, false);
const file = this.getOption(AddElementOptions_1.AddElementOptions.FILE_NAME, false);
task.statusMessage = "Schedule retrieving element";
task.percentComplete = imperative_1.TaskProgress.FIFTY_PERCENT;
let payload;
// if branchId is not in the request try to get it from mapping json
if ((0, object_1.isNil)(branchId)) {
try {
const workDir = (0, child_process_1.execSync)(/^win/.test(process.platform) ? "git rev-parse --show-toplevel >NUL 2>&1" : "git rev-parse --show-toplevel >/dev/null 2>&1", { encoding: "utf8" }).toString().trim();
branchId = api_1.BuildUtils.readMappingMetadata(workDir).git.branch;
}
catch (err) {
branchId = null;
}
}
if ((0, object_1.isNil)(file)) {
const name = this.getOption(AddElementOptions_1.AddElementOptions.ELEMENT_NAME, true);
const type = this.getOption(AddElementOptions_1.AddElementOptions.ELEMENT_TYPE, true);
payload = {
context,
repoName,
elements: [
{
name,
type,
system,
subsystem,
withDependencies,
forceGetDependencies
}
],
validate,
branchId,
allBranches
};
}
else {
let endevorElements;
endevorElements = MappingAddElementHandler.getElementsFromFile(file);
const elements = [];
endevorElements.forEach(e => {
elements.push({ name: e.fullElmName, type: e.typeName, system,
subsystem, withDependencies, forceGetDependencies });
});
payload = {
context,
repoName,
elements,
validate,
branchId,
allBranches
};
}
this.validateRequiredOptions();
response = yield api_1.EBGMappingService.addElement(this.ebgService, payload);
task.percentComplete = imperative_1.TaskProgress.ONE_HUNDRED_PERCENT;
this.console.log("Your request has been processed");
this.console.log("Reports:");
response.reports.forEach(report => {
this.console.log("return code: " + report.code + ", " +
"message: " + report.message);
});
});
}
}
exports.default = MappingAddElementHandler;
//# sourceMappingURL=MappingAddElement.handler.js.map