@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
128 lines (124 loc) • 5.32 kB
JavaScript
;
var object = require('../../utils/object.js');
/*
* 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.
*/
exports.FileLocation = void 0;
(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";
})(exports.FileLocation || (exports.FileLocation = {}));
const SYSTEM_DIR_INDEX = 1;
const SUBSYSTEM_DIR_INDEX = SYSTEM_DIR_INDEX + 1;
class ChangeValidator {
static validateAndGetPathComponents(path, mappingMetadata) {
const branch = mappingMetadata.branches.find((branch) => {
return (ChangeValidator.getFileLocation(path, branch) === exports.FileLocation.TYPE_DIR);
});
if (!branch) {
return undefined;
}
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 (!matchRegex) {
return [];
}
for (const component of matchRegex) {
pathComponents.push(component);
}
return pathComponents;
}
static validate(path, mappingMetadata) {
for (const branch of mappingMetadata.branches) {
const fileLocation = ChangeValidator.getFileLocation(path, branch);
if (fileLocation === exports.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 = undefined;
for (const systemMetadata of systems) {
if (pathComponents[SYSTEM_DIR_INDEX] === systemMetadata.name ||
pathComponents[SYSTEM_DIR_INDEX] === systemMetadata.alias) {
system = systemMetadata;
pathComponents[SYSTEM_DIR_INDEX] = systemMetadata.name;
}
}
if (object.isNotNil(system)) {
const subsystems = system.subsystems;
for (const subsystemMetadata of subsystems) {
if (pathComponents[SUBSYSTEM_DIR_INDEX] === subsystemMetadata.name ||
pathComponents[SUBSYSTEM_DIR_INDEX] === subsystemMetadata.alias) {
pathComponents[SUBSYSTEM_DIR_INDEX] = subsystemMetadata.name;
}
}
}
return pathComponents;
}
static getFileLocation(path, branch) {
const pathComponents = this.getPathComponents(path);
let fileLocation = exports.FileLocation.NOT_VALIDATED_DIR;
if (pathComponents.length > SYSTEM_DIR_INDEX) {
const directoryInRootDir = pathComponents[SYSTEM_DIR_INDEX];
const systems = branch.systems;
const system = systems.find((systemMetadata) => {
return (directoryInRootDir === systemMetadata.name ||
directoryInRootDir === systemMetadata.alias);
});
if (system) {
const subsystems = system.subsystems;
const subsystem = subsystems.find((subsystemMetadata) => {
return (pathComponents[SUBSYSTEM_DIR_INDEX] === subsystemMetadata.name ||
pathComponents[SUBSYSTEM_DIR_INDEX] === subsystemMetadata.alias);
});
fileLocation = subsystem
? exports.FileLocation.TYPE_DIR
: exports.FileLocation.FORBIDDEN_DIR;
}
}
return fileLocation;
}
}
exports.ChangeValidator = ChangeValidator;
exports.SUBSYSTEM_DIR_INDEX = SUBSYSTEM_DIR_INDEX;
exports.SYSTEM_DIR_INDEX = SYSTEM_DIR_INDEX;