@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
143 lines • 6.11 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangeValidator = exports.SUBSYSTEM_DIR_INDEX = exports.SYSTEM_DIR_INDEX = exports.FileLocation = void 0;
const object_1 = require("../../utils/object");
var FileLocation;
(function (FileLocation) {
/**
* The file in a directory that cannot contain files
*/
FileLocation[FileLocation["FORBIDDEN_DIR"] = 0] = "FORBIDDEN_DIR";
/**
* The file is in a type directory
*/
FileLocation[FileLocation["TYPE_DIR"] = 1] = "TYPE_DIR";
/**
* The file is not an element or a metadata file
*/
FileLocation[FileLocation["NOT_VALIDATED_DIR"] = 2] = "NOT_VALIDATED_DIR";
})(FileLocation = exports.FileLocation || (exports.FileLocation = {}));
exports.SYSTEM_DIR_INDEX = 1;
exports.SUBSYSTEM_DIR_INDEX = exports.SYSTEM_DIR_INDEX + 1;
class ChangeValidator {
static validateAndGetPathComponents(path, mappingMetadata) {
let branch = null;
for (const branchMetadata of mappingMetadata.branches) {
const fileLocation = ChangeValidator.getFileLocation(path, branchMetadata);
if (fileLocation === FileLocation.TYPE_DIR) {
branch = branchMetadata;
}
}
if ((0, object_1.isNil)(branch)) {
return;
}
else {
let pathComponents;
pathComponents = ChangeValidator.getPathComponents(path);
pathComponents = ChangeValidator.overwritePathComponents(pathComponents, branch);
return pathComponents;
}
}
static getPathComponents(path) {
const pathComponents = [];
const pathRegex = new RegExp(/^([^\/]+)\/([^\/]+)\/([^\/]+)\/([^\/]+)$/);
const matchRegex = path.match(pathRegex);
if ((0, object_1.isNotNil)(matchRegex)) {
for (const component of matchRegex) {
pathComponents.push(component);
}
}
else {
return;
}
return pathComponents;
}
static validate(path, mappingMetadata) {
for (const branch of mappingMetadata.branches) {
const fileLocation = ChangeValidator.getFileLocation(path, branch);
if (fileLocation === FileLocation.TYPE_DIR) {
return true;
}
}
return false;
}
// Overwrite alias system/subsystem with their real name
static overwritePathComponents(pathComponents, branch) {
const systems = branch.systems;
let system;
for (const systemMetadata of systems) {
if (pathComponents[exports.SYSTEM_DIR_INDEX] === systemMetadata.name || pathComponents[exports.SYSTEM_DIR_INDEX] === systemMetadata.alias) {
system = systemMetadata;
pathComponents[exports.SYSTEM_DIR_INDEX] = systemMetadata.name;
}
}
if ((0, object_1.isNotNil)(system)) {
const subsystems = system.subsystems;
for (const subsystemMetadata of subsystems) {
if (pathComponents[exports.SUBSYSTEM_DIR_INDEX] === subsystemMetadata.name || pathComponents[exports.SUBSYSTEM_DIR_INDEX] === subsystemMetadata.alias) {
pathComponents[exports.SUBSYSTEM_DIR_INDEX] = subsystemMetadata.name;
}
}
}
return pathComponents;
}
static getFileLocation(path, branch) {
const pathComponents = this.getPathComponents(path);
let fileLocation = FileLocation.NOT_VALIDATED_DIR;
if ((0, object_1.isNotNil)(pathComponents)) {
const directoryInRootDir = pathComponents[exports.SYSTEM_DIR_INDEX];
const systems = branch.systems;
let system;
for (const systemMetadata of systems) {
if (directoryInRootDir === systemMetadata.name) {
system = systemMetadata;
break;
}
if (directoryInRootDir === systemMetadata.alias) {
system = systemMetadata;
break;
}
}
if ((0, object_1.isNotNil)(system)) {
const subsystems = system.subsystems;
let subsystem;
for (const subsystemMetadata of subsystems) {
if (pathComponents[exports.SUBSYSTEM_DIR_INDEX] === subsystemMetadata.name) {
subsystem = subsystemMetadata;
break;
}
if (pathComponents[exports.SUBSYSTEM_DIR_INDEX] === subsystemMetadata.alias) {
subsystem = subsystemMetadata;
break;
}
}
fileLocation = ((0, object_1.isNotNil)(subsystem)) ? FileLocation.TYPE_DIR : FileLocation.FORBIDDEN_DIR;
}
}
return fileLocation;
}
}
exports.ChangeValidator = ChangeValidator;
//# sourceMappingURL=ChangeValidator.js.map