@broadcom/endevor-bridge-for-git-for-zowe-cli
Version:
Endevor Bridge for Git plug-in for Zowe CLI
197 lines (193 loc) • 9.39 kB
JavaScript
'use strict';
var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
var imperative = require('@zowe/imperative');
var EBGSession = require('./sessions/EBGSession.js');
require('../api/constants/EBGConstants.js');
require('../api/constants/PluginConstants.js');
require('../api/doc/ebg/IMappingView.js');
var object = require('../utils/object.js');
require('../api/doc/IBranchMetadata.js');
require('child_process');
require('../api/utils/ChangeValidator.js');
require('fs');
var OptionUtils = require('../api/utils/OptionUtils.js');
var OptionValidator = require('../api/utils/OptionValidator.js');
var EndevorSession = require('./sessions/EndevorSession.js');
var BaseProfileDefinitions = require('./sessions/BaseProfileDefinitions.js');
var EBGApiClient = require('../api/EBGApiClient.js');
var index = require('../node_modules/@broadcom/bridge-for-git-zowe-client/dist/index.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.
*/
/**
* This class is used by the various handlers in the project as the base class for their implementation.
*/
class BaseHandler {
constructor() {
this.nullToBlank = (nullable) => {
return object.isNil(nullable) ? "" : nullable;
};
}
/**
* This will grab the arguments and create a session before calling the subclass
* {@link BaseHandler#processWithSession} method.
*
* @param {IHandlerParameters} params Command parameters sent by imperative.
*
* @returns {Promise<void>}
*/
process(params) {
return tslib_es6.__awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
// Initialize options validator
this.optionValidator = new OptionValidator.OptionValidator();
this.optionUtils = new OptionUtils.OptionUtils(this.optionValidator);
this.endevorProfileUtils = new EndevorSession.EndevorSession(this.optionValidator);
const conf = imperative.ImperativeConfig.instance.config;
// Load profiles
// The EBG profile is loaded automatically in the arguments
// For the rest of profiles only the specified profiles are loaded in params.profiles
// so it is not necessary to specify the name in params.profiles.get()
const usingTeamConfig = ((_a = imperative.ImperativeConfig.instance.config) === null || _a === void 0 ? void 0 : _a.exists) || false;
if (usingTeamConfig) {
if (this.profileExists(conf.properties.profiles, "endevor")) {
this.endevorProfile = Object.assign({}, (_b = conf.properties.profiles[conf.properties.defaults.endevor]) === null || _b === void 0 ? void 0 : _b.properties);
}
if (this.profileExists(conf.properties.profiles, "base")) {
this.baseProfile = Object.assign({}, (_c = conf.properties.profiles[conf.properties.defaults.base]) === null || _c === void 0 ? void 0 : _c.properties);
}
if (this.profileExists(conf.properties.profiles, "zosmf")) {
this.zosmfProfile = Object.assign({}, (_d = conf.properties.profiles[conf.properties.defaults.zosmf]) === null || _d === void 0 ? void 0 : _d.properties);
}
}
else {
// for V1 compatibility
if (object.isNotNil(params.definition.profile)) {
// Load Endevor profile if needed
if ((_e = params.definition.profile.optional) === null || _e === void 0 ? void 0 : _e.includes(EndevorSession.EndevorSession.PROFILE_TYPE)) ;
if ((_f = params.definition.profile.optional) === null || _f === void 0 ? void 0 : _f.includes(BaseProfileDefinitions.BaseProfileDefinitions.PROFILE_TYPE)) ;
}
}
this.params = params;
if ((_j = (_h = (_g = params.definition) === null || _g === void 0 ? void 0 : _g.profile) === null || _h === void 0 ? void 0 : _h.optional) === null || _j === void 0 ? void 0 : _j.includes(EBGSession.EBGSession.PROFILE_TYPE)) {
const apiConfiguration = index.createConfiguration({
httpApi: index.wrapHttpLibrary(new EBGApiClient.EBGApiClient(this.createEbgSession())),
// Zowe client needs only pathname. URL is taken from session object
baseServer: new index.ServerConfiguration("http://dummy-url", {}),
});
this.apiClient = {
userApi: new index.UsersControllerApi(apiConfiguration),
repositoryApi: new index.RepositoriesControllerApi(apiConfiguration),
endevorApi: new index.EndevorControllerApi(apiConfiguration),
endevorConnectionsApi: new index.EndevorConnectionControllerApi(apiConfiguration),
jobsApi: new index.JobsControllerApi(apiConfiguration),
gitApi: new index.GitControllerApi(apiConfiguration),
mappingApi: new index.MappingsControllerApi(apiConfiguration),
mfWebhookApi: new index.MfWebhookControllerApi(apiConfiguration),
operationApi: new index.OperationsControllerApi(apiConfiguration),
globalApi: new index.GlobalControllerApi(apiConfiguration),
logsApi: new index.LogsControllerApi(apiConfiguration),
};
}
yield this.processWithSession();
// end any progress bar
this.progress.endBar();
});
}
validateRequiredOptions() {
return this.optionValidator.validateRequiredOptions();
}
getOption(optionDefinition, isRequired) {
return this.optionUtils.getOption(isRequired !== undefined
? isRequired
: optionDefinition.required !== undefined
? optionDefinition.required
: false, optionDefinition, this.arguments, optionDefinition.defaultValue);
}
getOptionFromArgument(optionDefinition) {
return this.optionUtils.getOptionFromArgument(optionDefinition, this.arguments);
}
getEndevorSessionOption(optionDefinition, isRequired = true) {
return this.endevorProfileUtils.getOption(isRequired, optionDefinition, this.arguments, optionDefinition.defaultValue, this.endevorProfile);
}
createEbgSession() {
return new EBGSession.EBGSession(this.optionValidator).createSession(this.arguments);
}
/**
* Returns the command line arguments passed
*/
get arguments() {
return this.params.arguments;
}
/**
* Fail the command with an imperative error
* @param {IImperativeError} err - the imperative error parameters
*/
fail(err) {
throw new imperative.ImperativeError(err);
}
/**
* Returns the console interface for the command handler
* @returns {IHandlerResponseConsoleApi}
*/
get console() {
return this.params.response.console;
}
setOutput(format, data, fields, header) {
const formattedOutput = Array.isArray(data)
? data.map((item) => {
if (typeof item === "string") {
return item;
}
return Object.fromEntries(Object.entries(item).map(([key, value]) => [
key,
value === undefined || value === "" ? "-" : value,
]));
})
: (data !== null && data !== void 0 ? data : "-");
this.params.response.format.output({
format,
fields,
output: formattedOutput,
header,
});
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerResponseDataApi}
*/
get data() {
return this.params.response.data;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerProgressApi}
*/
get progress() {
return this.params.response.progress;
}
profileExists(profiles, profileName) {
const filteredProfiles = Object.values(profiles).filter((value) => value.type === profileName);
return filteredProfiles.length > 0;
}
}
exports.BaseHandler = BaseHandler;