sfdx-hardis
Version:
Swiss-army-knife Toolbox for Salesforce. Allows you to define a complete CD/CD Pipeline. Orchestrate base commands and assist users with interactive wizards
113 lines • 5.46 kB
JavaScript
import { SfError } from '@salesforce/core';
import c from 'chalk';
import { getCurrentGitBranch, uxLog } from '../utils/index.js';
import { GitProvider } from '../gitProvider/index.js';
import { authOrg } from '../utils/authUtils.js';
import { findUserByUsernameLike } from '../utils/orgUtils.js';
export class ActionsProvider {
customUsernameToUse = null;
static async buildActionInstance(cmd) {
let actionInstance = null;
const type = cmd.type || 'command';
if (type === 'command') {
const CommandAction = await import('./commandAction.js');
actionInstance = new CommandAction.CommandAction();
}
else if (type === 'apex') {
const ApexAction = await import('./apexAction.js');
actionInstance = new ApexAction.ApexAction();
}
else if (type === 'data') {
const DataAction = await import('./dataAction.js');
actionInstance = new DataAction.DataAction();
}
else if (type === 'publish-community') {
const PublishCommunityAction = await import('./publishCommunityAction.js');
actionInstance = new PublishCommunityAction.PublishCommunityAction();
}
else if (type === 'manual') {
const ManualAction = await import('./manualAction.js');
actionInstance = new ManualAction.ManualAction();
}
else {
uxLog("error", this, c.yellow(`[DeploymentActions] Action type [${cmd.type}] is not yet implemented for action [${cmd.id}]: ${cmd.label}`));
cmd.result = {
statusCode: "failed",
skippedReason: `Action type [${cmd.type}] is not implemented`
};
}
return actionInstance;
}
getLabel() {
throw new SfError('getLabel should be implemented on this call');
}
/**
* Perform pre-run validations for the given command.
* Return null when the command is valid and may proceed.
* Return an ActionResult when the command must be short-circuited
* (for example: missing parameters -> failed, or manual -> skipped).
*/
async checkValidityIssues(cmd) {
const parametersValidityIssue = await this.checkParameters(cmd);
if (parametersValidityIssue) {
return parametersValidityIssue;
}
const authValidityIssue = await this.checkAuthCustomUsernameIssues(cmd);
if (authValidityIssue) {
return authValidityIssue;
}
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async checkParameters(_cmd) {
uxLog('warning', this, c.yellow(`checkParameters is not implemented on ${this.getLabel()}`));
return null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async run(cmd) {
uxLog('warning', this, c.yellow(`run is not implemented on ${this.getLabel()}`));
return { statusCode: 'skipped', skippedReason: 'Not implemented' };
}
async checkAuthCustomUsernameIssues(cmd) {
if (this.customUsernameToUse) {
return null;
}
if (cmd.customUsername) {
const conn = globalThis.jsForceConn;
const user = await findUserByUsernameLike(cmd.customUsername, conn);
if (!user) {
uxLog('error', this, c.red(`[DeploymentActions] Custom username [${cmd.customUsername}] not found for action ${cmd.label}`));
return { statusCode: 'failed', skippedReason: `Custom username [${cmd.customUsername}] not found` };
}
let authResult;
try {
const instanceUrl = conn.instanceUrl;
const prInfo = await GitProvider.getPullRequestInfo({ useCache: true });
let targetBranch = prInfo?.targetBranch;
if (!targetBranch) {
targetBranch = await getCurrentGitBranch({ formatted: true }) || undefined;
}
uxLog('log', this, c.grey(`[DeploymentActions] Authenticating with custom username [${user.Username}] for action ${cmd.label}, using targetBranch ${targetBranch}...`));
authResult = await authOrg(targetBranch, {
forceUsername: user.Username,
instanceUrl: instanceUrl,
setDefault: false,
});
}
catch (error) {
uxLog('error', this, c.red(`[DeploymentActions] Error during authentication with custom username [${user.Username}] for action ${cmd.label}: ${error}`));
return { statusCode: 'failed', skippedReason: `Error during authentication with custom username [${user.Username}]: ${error}` };
}
if (authResult === true) {
this.customUsernameToUse = user.Username;
uxLog('log', this, c.green(`[DeploymentActions] Authenticated with custom username [${this.customUsernameToUse}] for action ${cmd.label}`));
}
else {
uxLog('error', this, c.red(`[DeploymentActions] Failed to authenticate with custom username [${user.Username}] for action ${cmd.label}`));
return { statusCode: 'failed', skippedReason: `Failed to authenticate with custom username [${user.Username}]` };
}
}
return null;
}
}
//# sourceMappingURL=actionsProvider.js.map