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
158 lines • 6.71 kB
JavaScript
/* eslint-disable no-useless-escape */
/*
This class is deprecated and kept for backward compatibility
Use NotifProvider class instead :)
*/
import { getCurrentGitBranch } from "./index.js";
import { GitProvider } from "../gitProvider/index.js";
import { UtilsNotifs } from "../notifProvider/index.js";
/**
* @description This function retrieves the job URL from the GitProvider and creates a notification button if the job URL exists.
* The notification button is an object with a 'text' property set to "View Job" and a 'url' property set to the job URL.
* It returns an array of such notification buttons.
*
* @returns {Promise<{ text: string; url: string }[]>} - A Promise that resolves to an array of notification buttons.
*/
export async function getNotificationButtons() {
const notifButtons = [];
const jobUrl = await GitProvider.getJobUrl();
if (jobUrl) {
notifButtons.push({ text: "View Job", url: jobUrl });
}
return notifButtons;
}
/**
* @descriptionThis function retrieves the current Git branch and its URL from the GitProvider.
* It then generates a markdown string for the branch.
* If the branch URL exists, it creates a markdown link with the branch name as the link text.
* Otherwise, it simply formats the branch name in markdown.
*
* @returns {Promise<string>} - A Promise that resolves to a markdown string for the current Git branch.
*/
export async function getBranchMarkdown(type = "slack") {
const currentGitBranch = await getCurrentGitBranch() || "";
let branchMd = type === "jira"
? `{ "label": "${currentGitBranch}"}`
: type === "teams"
? `**${currentGitBranch}**`
: type == "html"
? `<strong>${currentGitBranch}</strong>`
: `*${currentGitBranch}*`;
const branchUrl = await GitProvider.getCurrentBranchUrl();
if (branchUrl) {
branchMd = UtilsNotifs.markdownLink(branchUrl, currentGitBranch, type);
}
return branchMd;
}
/**
* @descriptionThis function retrieves the current Git branch and its URL from the GitProvider.
* It then generates a markdown string for the branch.
* If the branch URL exists, it creates a markdown link with the branch name as the link text.
* Otherwise, it simply formats the branch name in markdown.
*
* @returns {Promise<string>} - A Promise that resolves to a markdown string for the current Git branch.
*/
export async function getOrgMarkdown(instanceUrl, type = "slack") {
if (!instanceUrl) {
return await getBranchMarkdown(type);
}
const linkMarkdown = UtilsNotifs.markdownLink(instanceUrl, instanceUrl.replace("https://", "").replace(".my.salesforce.com", ""), type);
return linkMarkdown;
}
/**
* @function removeMarkdown
*
* @description
* Parse the markdown and returns a string
*
* @param markdown - The markdown string to parse
* @param options - The options for the function
*
* @returns The parsed plain text
*/
export function removeMarkdown(markdown, optionsIn = {
listUnicodeChar: "",
}) {
const options = optionsIn || {};
// eslint-disable-next-line no-prototype-builtins
options.listUnicodeChar = options.hasOwnProperty("listUnicodeChar") ? options.listUnicodeChar : false;
// eslint-disable-next-line no-prototype-builtins
options.stripListLeaders = options.hasOwnProperty("stripListLeaders") ? options.stripListLeaders : true;
// eslint-disable-next-line no-prototype-builtins
options.gfm = options.hasOwnProperty("gfm") ? options.gfm : true;
// eslint-disable-next-line no-prototype-builtins
options.useImgAltText = options.hasOwnProperty("useImgAltText") ? options.useImgAltText : true;
// eslint-disable-next-line no-prototype-builtins
options.preserveLinks = options.hasOwnProperty("preserveLinks") ? options.preserveLinks : false;
let output = markdown || "";
// Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, "");
try {
if (options.stripListLeaders) {
if (options.listUnicodeChar)
output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, options.listUnicodeChar + " $1");
else
output = output.replace(/^([\s\t]*)([\*\-\+]|\d+\.)\s+/gm, "$1");
}
if (options.gfm) {
output = output
// Header
.replace(/\n={2,}/g, "\n")
// Fenced codeblocks
.replace(/~{3}.*\n/g, "")
// Strikethrough
.replace(/~~/g, "")
// Fenced codeblocks
.replace(/`{3}.*\n/g, "");
}
if (options.preserveLinks) {
// Remove inline links while preserving the links
output = output.replace(/\[(.*?)\][\[\(](.*?)[\]\)]/g, "$1 ($2)");
}
output = output
// Remove HTML tags
.replace(/<[^>]*>/g, "")
// Remove setext-style headers
.replace(/^[=\-]{2,}\s*$/g, "")
// Remove footnotes?
.replace(/\[\^.+?\](\: .*?$)?/g, "")
.replace(/\s{0,2}\[.*?\]: .*?$/g, "")
// Remove images
.replace(/\!\[(.*?)\][\[\(].*?[\]\)]/g, options.useImgAltText ? "$1" : "")
// Remove inline links
.replace(/\[(.*?)\][\[\(].*?[\]\)]/g, "$1")
// Remove blockquotes
.replace(/^\s{0,3}>\s?/g, "")
.replace(/(^|\n)\s{0,3}>\s?/g, "\n\n")
// Remove reference-style links?
.replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "")
// Remove atx-style headers
.replace(/^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm, "$1$2$3")
// Remove emphasis (repeat the line to remove double emphasis)
.replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, "$2")
.replace(/([\*_]{1,3})(\S.*?\S{0,1})\1/g, "$2")
// Remove code blocks
.replace(/(`{3,})(.*?)\1/gm, "$2")
// Remove inline code
.replace(/`(.+?)`/g, "$1")
// Replace two or more newlines with exactly two? Not entirely sure this belongs here...
.replace(/\n{2,}/g, "\n\n");
}
catch (e) {
console.error(e);
return markdown;
}
return output;
}
export function getSeverityIcon(severity) {
const severityIcons = {
critical: "⛔",
error: "❌",
warning: "⚠️",
info: "📄",
success: "✅",
log: "📰",
};
return severityIcons[severity] || "❔";
}
//# sourceMappingURL=notifUtils.js.map