UNPKG

@broadcom/endevor-bridge-for-git-for-zowe-cli

Version:

Endevor Bridge for Git plug-in for Zowe CLI

221 lines (217 loc) 9.28 kB
'use strict'; var imperative = require('@zowe/imperative'); var EBGSession = require('./sessions/EBGSession.js'); require('path'); 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) 2026 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 { /** * Full set of command handler parameters from imperative */ params; apiClient; optionValidator; optionUtils; endevorProfileUtils; endevorProfile; zosmfProfile; baseProfile; /** * 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>} */ async process(params) { // 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 = imperative.ImperativeConfig.instance.config?.exists || false; if (usingTeamConfig) { if (this.profileExists(conf.properties.profiles, "endevor")) { this.endevorProfile = { ...conf.properties.profiles[conf.properties.defaults.endevor] ?.properties, }; } if (this.profileExists(conf.properties.profiles, "base")) { this.baseProfile = { ...conf.properties.profiles[conf.properties.defaults.base] ?.properties, }; } if (this.profileExists(conf.properties.profiles, "zosmf")) { this.zosmfProfile = { ...conf.properties.profiles[conf.properties.defaults.zosmf] ?.properties, }; } } else { // for V1 compatibility if (object.isNotNil(params.definition.profile)) { // Load Endevor profile if needed if (params.definition.profile.optional?.includes(EndevorSession.EndevorSession.PROFILE_TYPE)) ; if (params.definition.profile.optional?.includes(BaseProfileDefinitions.BaseProfileDefinitions.PROFILE_TYPE)) ; } } this.params = params; if (params.definition?.profile?.optional?.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), authApi: new index.AuthControllerApi(apiConfiguration), hookScriptsApi: new index.HooksScriptControllerApi(apiConfiguration), }; } await 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); } nullToBlank = (nullable) => { return object.isNil(nullable) ? "" : nullable; }; /** * 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); } completeProgressTask(task) { task.percentComplete = imperative.TaskProgress.ONE_HUNDRED_PERCENT; task.stageName = imperative.TaskStage.COMPLETE; } failProgressTask(task, message) { task.statusMessage = message ? message : "Task failed"; task.stageName = imperative.TaskStage.FAILED; } /** * 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 ?? "-"); 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;