@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
168 lines (164 loc) • 7.24 kB
JavaScript
;
var imperative = require('@zowe/imperative');
var EBGConstants = require('../constants/EBGConstants.js');
var MappingMetadata = require('./MappingMetadata.js');
var child_process = require('child_process');
var ChangeValidator = require('./ChangeValidator.js');
var fs = require('fs');
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.
*/
/**
*
* @export
* @class LocalRepositoryUtils
*/
class LocalRepositoryUtils {
static readJSONFileAsObject(filePath, filename) {
try {
imperative.Logger.getAppLogger().trace(`BuildUtils - Read: ${filePath}`);
const file = fs.readFileSync(filePath, "utf8"); // read with default encoding
imperative.Logger.getAppLogger().trace(`BuildUtils - Get ${filename} file content: \n${file}`);
return JSON.parse(file);
}
catch (err) {
throw new imperative.ImperativeError({
msg: `${filePath} does not contain a valid ${filename} file. Error ${err}`,
});
}
}
/**
* Read the metadata file from git working directory gitLocation (last version takes precedence)
*
* @static
* @param {string} gitLocation
* @returns {IMappingMetadata}
* @memberof LocalRepositoryUtils
*/
static readMappingMetadata(gitLocation) {
const VERSION_4 = 4;
const mappingMetadataPath = EBGConstants.EBGConstants.getMappingMetadataFilePath(gitLocation);
const metadata = this.readJSONFileAsObject(mappingMetadataPath, EBGConstants.EBGConstants.MAPPING_METADATA_FILENAME);
this.validateMappingMetadata(metadata);
if (metadata.version === VERSION_4) {
return metadata;
}
return MappingMetadata.MappingMetadata.convert(metadata);
}
static getAllChangedElements(workDir, branchMetadata, mappingMetadata, type, fileExt) {
let changed;
let untracked;
try {
changed = child_process.execSync("git diff --name-status origin/" + branchMetadata.name, { cwd: workDir })
.toString()
.split(/\r?\n/)
.filter(String);
untracked = child_process.execSync("git ls-files --others --exclude-standard", {
cwd: workDir,
})
.toString()
.split(/\r?\n/)
.filter(String);
}
catch (err) {
throw new imperative.ImperativeError({
msg: `Error getting changed elements: ${err}`,
});
}
const modifiedElements = [];
const exemptions = [
EBGConstants.EBGConstants.GITKEEP_FILENAME,
EBGConstants.EBGConstants.GITIGNORE_FILENAME,
EBGConstants.EBGConstants.BRIDGE_METATDATA_FILENAME,
];
changed
.filter((change) => !exemptions.some((e) => change.includes(e)))
.forEach((entry) => {
// example changed:
// M ESCM180/XUEYU010/ISPM/LOCK2
// R100 ESCM180/XUEYU010/ISPM/TESTADD2 ESCM180/XUEYU010/ISPM/REN
// A ESCM180/XUEYU010/ISPM/test
const items = entry.split(/\s+/);
if (items.length <= 1) {
return;
}
let matchResult;
if ((items[0].startsWith("A") || items[0].startsWith("M")) &&
items.length === 2) {
matchResult = ChangeValidator.ChangeValidator.validateAndGetPathComponents(items[1], mappingMetadata);
}
// tslint:disable-next-line:no-magic-numbers
if (items[0].startsWith("R") && items.length === 3) {
matchResult = ChangeValidator.ChangeValidator.validateAndGetPathComponents(items[2], mappingMetadata);
}
if (object.isNotNil(matchResult) &&
matchResult[EBGConstants.EBGConstants.TYPE_DIR_INDEX].toUpperCase() ===
type.toUpperCase()) {
let elementName = matchResult[EBGConstants.EBGConstants.ELEMENT_NAME_INDEX];
// remove file extension
if (object.isNotNil(fileExt) && fileExt !== "") {
elementName = elementName.substr(0, elementName.lastIndexOf("."));
}
modifiedElements.push(elementName);
}
});
untracked
.filter((v) => !exemptions.some((e) => v.includes(e)))
.forEach((entry) => {
// example untracked:
// ESCM180/XUEYU010/ISPM/NEW
const matchResult = ChangeValidator.ChangeValidator.validateAndGetPathComponents(entry, mappingMetadata);
if (object.isNotNil(matchResult) &&
matchResult[EBGConstants.EBGConstants.TYPE_DIR_INDEX].toUpperCase() ===
type.toUpperCase()) {
let elementName = matchResult[EBGConstants.EBGConstants.ELEMENT_NAME_INDEX];
// remove file extension
if (object.isNotNil(fileExt) && fileExt !== "") {
elementName = elementName.substr(0, elementName.lastIndexOf("."));
}
modifiedElements.push(elementName);
}
});
if (modifiedElements.length < 1) {
throw new imperative.ImperativeError({
msg: `There are no changed elements of type '${type}' `,
});
}
return modifiedElements;
}
static validateMappingMetadata(mappingMetadata) {
if (object.isNil(mappingMetadata.version) ||
object.isNil(mappingMetadata.url) ||
object.isNil(mappingMetadata.git) ||
object.isNil(mappingMetadata.endevor) ||
object.isNil(mappingMetadata.git.cloneUrl) ||
object.isNil(mappingMetadata.git.branch) ||
object.isNil(mappingMetadata.git.context) ||
object.isNil(mappingMetadata.git.repository)) {
throw new imperative.ImperativeError({
msg: `mapping.json file is not valid`,
});
}
}
}
exports.LocalRepositoryUtils = LocalRepositoryUtils;