/* ** Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. ** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. */ 'use strict'; const { resolve } = require('node:path'); const { ActionResult } = require('../../../services/actionresult/ActionResult'); const DeployActionResult = require('../../../services/actionresult/DeployActionResult'); const CommandUtils = require('../../../utils/CommandUtils'); const ProjectInfoService = require('../../../services/ProjectInfoService'); const AccountSpecificValuesUtils = require('../../../utils/AccountSpecificValuesUtils'); const ApplyInstallationPreferencesUtils = require('../../../utils/ApplyInstallationPreferencesUtils'); const NodeTranslationService = require('../../../services/NodeTranslationService'); const { executeWithSpinner } = require('../../../ui/CliSpinner'); const BaseAction = require('../../base/BaseAction'); const { getProjectDefaultAuthId } = require('../../../utils/AuthenticationUtils'); const { toErrorMessages } = require('../../../utils/ErrorMessageUtils'); const { createCredentialSessionProvider } = require('../../../utils/AuthSessionProvider'); const { DEPLOY_MODE, DEPLOY_COMMAND, prepareDeployExecution, } = require('@oracle/suitecloud-sdk-core').commands; const { executeProjectCommand, PROJECT_COMMAND, SDK_OPERATION_STATUS, } = require('@oracle/suitecloud-sdk-core').commands; const { executeWithAuthRetry, shouldRetryAuthByResult, } = require('@oracle/suitecloud-sdk-core').auth; const { PROJECT_SUITEAPP } = require('../../../ApplicationConstants'); const { COMMAND_DEPLOY, PROJECT_COMMAND_LOG } = require('../../../services/TranslationKeys'); const IGNORED_OPTIONS = { VALIDATE: 'validate', }; const COMMAND = { OPTIONS: { AUTH_ID: 'authid', ACCOUNT_SPECIFIC_VALUES: 'accountspecificvalues', JSON: 'json', LOG: 'log', PROJECT: 'project', }, FLAGS: { NO_PREVIEW: DEPLOY_COMMAND.FLAGS.NO_PREVIEW, PREVIEW: DEPLOY_COMMAND.FLAGS.PREVIEW, SKIP_WARNING: DEPLOY_COMMAND.FLAGS.SKIP_WARNING, APPLY_INSTALLATION_PREFERENCES: DEPLOY_COMMAND.FLAGS.APPLY_INSTALLATION_PREFERENCES, }, }; module.exports = class DeployAction extends ( BaseAction ) { constructor(options) { super(options); const projectInfoService = new ProjectInfoService(this._projectFolder); this._projectType = projectInfoService.getProjectType(); this._projectName = normalizeManifestValue(getProjectInfoValue(projectInfoService, 'getProjectName')); const publisherId = normalizeManifestValue(getProjectInfoValue(projectInfoService, 'getPublisherId')); const projectId = normalizeManifestValue(getProjectInfoValue(projectInfoService, 'getProjectId')); this._suiteAppId = this._projectType === PROJECT_SUITEAPP && publisherId && projectId ? `${publisherId}.${projectId}` : undefined; } preExecute(params) { AccountSpecificValuesUtils.validate(params, this._projectFolder); ApplyInstallationPreferencesUtils.validate(params, this._projectFolder, this._commandMetadata.name, this._log); return { ...params, [COMMAND.OPTIONS.PROJECT]: CommandUtils.quoteString(this._projectFolder), [COMMAND.OPTIONS.AUTH_ID]: getProjectDefaultAuthId(this._executionPath), ...AccountSpecificValuesUtils.transformArgument(params), }; } async execute(params) { try { if (params[IGNORED_OPTIONS.VALIDATE]) { await this._log.warning(NodeTranslationService.getMessage(COMMAND_DEPLOY.WARNINGS.VALIDATE_OPTION_IGNORED)); } const deployExecution = prepareDeployExecution(params); if (deployExecution.mode === DEPLOY_MODE.PREVIEW) { return await this._preview(deployExecution.params, deployExecution.flags); } return await this._deploy(deployExecution.params, deployExecution.flags); } catch (error) { return ActionResult.Builder.withErrors(toErrorMessages(error)).build(); } } async _preview(params, flags) { try { const sdkParams = CommandUtils.extractCommandOptions(params, this._commandMetadata); const projectFolder = CommandUtils.unquoteString(sdkParams[COMMAND.OPTIONS.PROJECT]); const dryrunOperationResult = await this._executeProjectCommandWithAuthRetry({ command: PROJECT_COMMAND.PREVIEW, projectFolder, sdkParams, flags, message: NodeTranslationService.getMessage( COMMAND_DEPLOY.MESSAGES.PREVIEWING, this._suiteAppId || this._projectName, getProjectDefaultAuthId(this._executionPath), ), }); return dryrunOperationResult.status === SDK_OPERATION_STATUS.SUCCESS ? ActionResult.Builder.withData(dryrunOperationResult.data) .withResultMessage(dryrunOperationResult.resultMessage) .withCommandParameters(sdkParams) .withCommandFlags(flags) .build() : ActionResult.Builder.withErrors(dryrunOperationResult.errorMessages) .withCommandParameters(sdkParams) .withCommandFlags(flags) .build(); } catch (error) { return ActionResult.Builder.withErrors(toErrorMessages(error)).build(); } } async _deploy(params, flags) { const installationPreferencesApplied = flags.includes( COMMAND.FLAGS.APPLY_INSTALLATION_PREFERENCES ); try { const sdkParams = CommandUtils.extractCommandOptions(params, this._commandMetadata); const projectFolder = CommandUtils.unquoteString(sdkParams[COMMAND.OPTIONS.PROJECT]); const operationResult = await this._executeProjectCommandWithAuthRetry({ command: PROJECT_COMMAND.DEPLOY, projectFolder, sdkParams, flags, message: NodeTranslationService.getMessage( COMMAND_DEPLOY.MESSAGES.DEPLOYING, this._suiteAppId || this._projectName, getProjectDefaultAuthId(this._executionPath), ), }); return operationResult.status === SDK_OPERATION_STATUS.SUCCESS ? DeployActionResult.Builder.withData(operationResult.data) .withResultMessage(operationResult.resultMessage) .withAppliedInstallationPreferences(installationPreferencesApplied) .withProjectType(this._projectType) .withProjectFolder(this._projectFolder) .withCommandParameters(sdkParams) .withCommandFlags(flags) .build() : DeployActionResult.Builder.withErrors(operationResult.errorMessages) .withAppliedInstallationPreferences(installationPreferencesApplied) .withProjectType(this._projectType) .withCommandParameters(sdkParams) .withCommandFlags(flags) .build(); } catch (error) { return DeployActionResult.Builder.withErrors(toErrorMessages(error)) .withAppliedInstallationPreferences(installationPreferencesApplied) .withProjectType(this._projectType) .withCommandParameters(params) .withCommandFlags(flags) .build(); } } async _executeProjectCommandWithAuthRetry({ command, projectFolder, sdkParams, flags, message }) { const authId = sdkParams[COMMAND.OPTIONS.AUTH_ID]; const authSessionProvider = createCredentialSessionProvider(this._sdkPath, this._executionEnvironmentContext); const operationResult = await executeWithSpinner({ action: executeWithAuthRetry({ authId, authSessionProvider, shouldRetryAuth: shouldRetryAuthByResult, executeWithAuthSession: (authCredentials) => this._executeProjectCommand({ command, projectFolder, sdkParams, flags, authCredentials, }), }), message, }); if (operationResult.logFilePath) { this._log.info(NodeTranslationService.getMessage(PROJECT_COMMAND_LOG.MESSAGES.WRITING, operationResult.logFilePath)); } if (operationResult.logWriteWarning) { this._log.warning(operationResult.logWriteWarning); } return operationResult; } _executeProjectCommand({ command, projectFolder, sdkParams, flags, authCredentials }) { return executeProjectCommand({ command, projectFolder, hostName: authCredentials.hostName, accessToken: authCredentials.accessToken, rawOutput: isRawOutputRequested(sdkParams), logFileLocation: sdkParams[COMMAND.OPTIONS.LOG] ? resolve(this._executionPath, CommandUtils.unquoteString(sdkParams[COMMAND.OPTIONS.LOG])) : undefined, params: sdkParams, flags, userAgent: this._executionEnvironmentContext?.toUserAgentString?.(), summaryContext: this._buildSummaryContext(authCredentials, flags), }); } _buildSummaryContext(authCredentials, flags = []) { const accountInfo = authCredentials && authCredentials.accountInfo ? authCredentials.accountInfo : {}; return { accountName: accountInfo.companyName, accountId: accountInfo.companyId, roleName: accountInfo.roleName, ...(this._projectType === PROJECT_SUITEAPP && this._suiteAppId ? { suiteAppId: this._suiteAppId, applyInstallationPreferences: flags.includes(COMMAND.FLAGS.APPLY_INSTALLATION_PREFERENCES), } : { projectName: this._projectName }), }; } }; function isRawOutputRequested(commandParameters) { return !!commandParameters[COMMAND.OPTIONS.JSON]; } function normalizeManifestValue(value) { if (Array.isArray(value)) { return value.length > 0 ? String(value[0]) : ''; } if (value === undefined || value === null) { return ''; } return String(value); } function getProjectInfoValue(projectInfoService, methodName) { if (!projectInfoService || typeof projectInfoService[methodName] !== 'function') { return ''; } return projectInfoService[methodName](); }