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
160 lines (133 loc) ⢠6.94 kB
JavaScript
/* jscpd:ignore-start */
import { SfCommand, Flags, optionalOrgFlagWithDeprecations } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { CONSTANTS } from '../../../../config/index.js';
import { buildCheckDeployCommitSummary, handlePostDeploymentNotifications, setPullRequestData } from '../../../../common/utils/gitUtils.js';
import { GitProvider } from '../../../../common/gitProvider/index.js';
import c from "chalk";
import { uxLog } from '../../../../common/utils/index.js';
import { setConnectionVariables } from '../../../../common/utils/orgUtils.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('sfdx-hardis', 'org');
export default class DeployNotify extends SfCommand {
static title = 'Deployment Notifications';
static description = `Post notifications related to:
- **Deployment simulation** _(use with --check-only)_
- **Deployment process** _(to call only if your deployment is successful)_
### Integrations
According to the [integrations you configured](${CONSTANTS.DOC_URL_ROOT}/salesforce-ci-cd-setup-integrations-home/), notifications can contain deployment information and [Flow Visual Git Diff](${CONSTANTS.DOC_URL_ROOT}/salesforce-deployment-assistant-home/#flow-visual-git-diff)
- GitHub, Gitlab, Azure DevOps, Bitbucket comments on Pull Requests (including Flows Visual Git Diff)
- Slack, Microsoft Teams, Email deployment summary after a successful deployment
- JIRA tags and comments on tickets that just has been deployed


### Flows Visual Git Diff
- Visually show you the differences on a diagram
- Display the update details without having to open any XML !
š© = added
š„ = removed
š§ = updated


### In custom CI/CD workflow
Example of usage in a custom CI/CD pipeline:
\`\`\`bash
# Disable exit-on-error temporarily
set +e
# Run the deploy command
sf project deploy start [....]
RET_CODE=$?
# Re-enable exit-on-error
set -e
# Determine MYSTATUS based on return code
if [ $RET_CODE -eq 0 ]; then
MYSTATUS="valid"
else
MYSTATUS="invalid"
fi
# Run the notify command with MYSTATUS
sf hardis:project:deploy:notify --check-only --deploy-status "$MYSTATUS"
\`\`\`
### Other usages
This command is for custom SF Cli pipelines, if you are a sfdx-hardis user, it is already embedded in sf hardis:deploy:smart.
You can also use [sfdx-hardis wrapper commands of SF deployment commands](${CONSTANTS.DOC_URL_ROOT}/salesforce-deployment-assistant-setup/#using-custom-cicd-pipeline)
`;
static examples = [
'$ sf hardis:project:deploy:notify --check-only --deploy-status valid --message "This deployment check is valid\\n\\nYahooo !!"',
'$ sf hardis:project:deploy:notify --check-only --deploy-status invalid --message "This deployment check has failed !\\n\\Oh no !!"',
'$ sf hardis:project:deploy:notify --deploy-status valid --message "This deployment has been processed !\\n\\nYahooo !!"'
];
static flags = {
"check-only": Flags.boolean({
char: 'c',
default: false,
description: `Use this option to send notifications from a Deployment simulation job`,
}),
"deploy-status": Flags.string({
char: 's',
options: ["valid", "invalid", "unknown"],
default: "unknown",
description: `Send success, failure or unknown (default) to indicate if the deployment or deployment simulation is in success or not`,
}),
message: Flags.string({
char: "m",
default: "",
description: "Custom message that you want to be added in notifications (string or markdown format)"
}),
debug: Flags.boolean({
char: 'd',
default: false,
description: messages.getMessage('debugMode'),
}),
websocket: Flags.string({
description: messages.getMessage('websocket'),
}),
skipauth: Flags.boolean({
description: 'Skip authentication check when a default username is required',
}),
'target-org': optionalOrgFlagWithDeprecations,
};
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
static requiresProject = true;
/* jscpd:ignore-end */
checkOnly = false;
message = "";
debugMode = false;
deployStatus = "unknown";
async run() {
const { flags } = await this.parse(DeployNotify);
this.checkOnly = flags["check-only"] === true ? true : false;
this.deployStatus = flags["deploy-status"] || "unknown";
this.message = flags.message || "";
this.debugMode = flags.debug || false;
await setConnectionVariables(flags?.['target-org']?.getConnection(), true);
uxLog("action", this, c.cyan("Handling Pull Request comments for a deployment check job..."));
// Add deployment info
await buildCheckDeployCommitSummary();
const prData = {
messageKey: "deployment",
title: (this.checkOnly && this.deployStatus === "valid") ? "ā
Deployment check success" :
(!this.checkOnly && this.deployStatus === "valid") ? "ā
Deployment success" :
(this.checkOnly && this.deployStatus === "invalid") ? "ā Deployment check failure" :
(!this.checkOnly && this.deployStatus === "invalid") ? "ā Deployment failure" :
(this.checkOnly && this.deployStatus === "unknown") ? "𤷠Deployment check status unknown" :
"𤷠Deployment status unknown",
deployErrorsMarkdownBody: this.message,
status: this.deployStatus === "valid" ? "valid" : this.deployStatus === "invalid" ? "invalid" : "tovalidate",
};
setPullRequestData(prData);
// Post comments š
await GitProvider.managePostPullRequestComment(this.checkOnly);
// Post notifications after successful deployment
if (this.checkOnly === false && this.deployStatus === "valid") {
await handlePostDeploymentNotifications(flags, flags["target-org"]?.getUsername(), false, false, this.debugMode, this.message);
}
// Fallback
else {
uxLog("warning", this, c.yellow("No notification has been sent"));
uxLog("warning", this, c.yellow("- Slack / Teams / Email / JIRA messages are sent only if --check-only is false and --deploy-status is valid"));
}
return { message: "Processed notifications" };
}
}
//# sourceMappingURL=notify.js.map