backport
Version:
A CLI tool that automates the process of backporting commits
64 lines • 3.32 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPullRequestBody = void 0;
const handlebars_1 = __importDefault(require("handlebars"));
const packageVersion_1 = require("../../../../utils/packageVersion");
const getSourceBranchFromCommits_1 = require("../../../getSourceBranchFromCommits");
const logger_1 = require("../../../logger");
const commitFormatters_1 = require("../../commitFormatters");
function getPullRequestBody({ options, commits, targetBranch, }) {
const commitMessagesAsString = commits
.map((c) => {
const message = c.sourcePullRequest
? `[${(0, commitFormatters_1.getFirstLine)(c.sourceCommit.message)}](${c.sourcePullRequest.url})`
: `${(0, commitFormatters_1.getFirstLine)(c.sourceCommit.message)} (${(0, commitFormatters_1.getShortSha)(c.sourceCommit.sha)})`;
return ` - ${message}`;
})
.join('\n');
const sourceBranch = (0, getSourceBranchFromCommits_1.getSourceBranchFromCommits)(commits);
const defaultPrDescription = '# Backport\n\n' +
'This will backport the following commits from `{{sourceBranch}}` to `{{targetBranch}}`:\n' +
'{{commitMessages}}\n\n' +
'<!--- Backport version: {{PACKAGE_VERSION}} -->\n\n' +
'### Questions ?\n' +
'Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport)';
const commitsStringified = stripMarkdownComments(`{{{{raw}}}}${JSON.stringify(commits)}{{{{/raw}}}}`);
const prDescription = (options.prDescription ?? defaultPrDescription)
// replace defaultPrDescription
.replaceAll('{{defaultPrDescription}}', defaultPrDescription)
.replaceAll('{defaultPrDescription}', defaultPrDescription) // for backwards compatibility
// replace commitMessages
.replaceAll('{{commitMessages}}', `{{{{raw}}}}${commitMessagesAsString}{{{{/raw}}}}`)
// replace commits
.replaceAll('{{commits}}', '{{commitsAsJson}}')
.replaceAll('{commits}', commitsStringified) // for backwards compatibility
.replaceAll('{{commitsStringified}}', commitsStringified)
.replaceAll('{{commitsAsJson}}', '{{commits}}')
// replace sourceBranch and targetBranch
.replaceAll('{{sourceBranch}}', sourceBranch)
.replaceAll('{{targetBranch}}', targetBranch)
// replace package version
.replaceAll('{{PACKAGE_VERSION}}', (0, packageVersion_1.getPackageVersion)());
try {
const template = handlebars_1.default.compile(prDescription, { noEscape: true });
const res = template({
sourcePullRequest: commits[0].sourcePullRequest, // assume that all commits are from the same PR
commits,
});
return res;
}
catch (e) {
logger_1.logger.error('Could not compile PR description', e);
return prDescription
.replaceAll('{{{{raw}}}}', '')
.replaceAll('{{{{/raw}}}}', '');
}
}
exports.getPullRequestBody = getPullRequestBody;
function stripMarkdownComments(str) {
return str.replace(/<!--[\s\S]*?-->/g, '');
}
//# sourceMappingURL=getPullRequestBody.js.map