@salesforce/plugin-release-management
Version:
A plugin for preparing and publishing npm packages
124 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
const os = require("os");
const core_1 = require("@salesforce/core");
const command_1 = require("@salesforce/command");
const core_2 = require("@octokit/core");
const kit_1 = require("@salesforce/kit");
const chalk_1 = require("chalk");
const shelljs_1 = require("shelljs");
const ts_types_1 = require("@salesforce/ts-types");
const dependabot_1 = require("../../dependabot");
core_1.Messages.importMessagesDirectory(__dirname);
const messages = core_1.Messages.loadMessages('@salesforce/plugin-release-management', 'dependabot.consolidate');
class Consolidate extends command_1.SfdxCommand {
async run() {
const baseRepoObject = await (0, dependabot_1.getOwnerAndRepo)(this.flags.owner, this.flags.repo);
const baseBranch = (0, ts_types_1.ensureString)(this.flags['base-branch']);
const targetBranch = (0, ts_types_1.ensureString)(this.flags['target-branch']);
const auth = (0, ts_types_1.ensureString)(new kit_1.Env().getString('GH_TOKEN'), 'GH_TOKEN is required to be set in the environment');
const octokit = new core_2.Octokit({ auth });
const pullRequests = await octokit.request('GET /repos/{owner}/{repo}/pulls', baseRepoObject);
const dependabotPRs = pullRequests.data.filter((d) => d.state === 'open' &&
d.user.login === 'dependabot[bot]' &&
(0, dependabot_1.meetsVersionCriteria)(d.title, this.flags['max-version-bump']) &&
!this.shouldBeIgnored(d.title));
let prBody = `This PR consolidates all dependabot PRs that are less than or equal to a ${this.flags['max-version-bump']} version bump${os.EOL}${os.EOL}`;
for (const pr of dependabotPRs) {
prBody += `Closes #${pr.number}${os.EOL}`;
}
const prTitle = `Consolidate ${this.flags['max-version-bump']} dependabot PRs`;
this.log((0, chalk_1.bold)('PR Title:'));
this.log(prTitle);
this.log((0, chalk_1.bold)('PR Body:'));
this.log(prBody);
this.log((0, chalk_1.bold)('Commits:'));
for (const pr of dependabotPRs) {
this.log(` ${(0, chalk_1.cyan)(pr.head.sha)} [#${pr.number} ${pr.title}]`);
}
if (!this.flags.dryrun) {
try {
this.exec('git fetch origin');
this.exec('git fetch -p');
this.exec(`git checkout ${baseBranch}`);
this.exec('git pull');
this.exec(`git checkout -b ${targetBranch}`);
const shas = dependabotPRs.map((d) => d.head.sha);
for (const sha of shas) {
this.exec(`git cherry-pick ${sha} --strategy-option=theirs`);
}
if (!this.flags['no-pr']) {
this.exec(`git push -u origin ${targetBranch} --no-verify`);
const response = await octokit.request('POST /repos/{owner}/{repo}/pulls', {
...baseRepoObject,
head: targetBranch,
base: baseBranch,
body: prBody,
title: prTitle,
});
this.log();
// eslint-disable-next-line no-underscore-dangle
this.log(`${(0, chalk_1.green)('Created Pull Request:')} ${response.data._links.html.href}`);
}
}
catch (err) {
this.error(err);
}
}
}
shouldBeIgnored(title) {
const ignore = this.flags.ignore;
return ignore.some((i) => title.includes(i));
}
exec(cmd, silent = false) {
this.log((0, chalk_1.bold)(cmd));
(0, shelljs_1.exec)(cmd, { silent });
}
}
exports.default = Consolidate;
Consolidate.description = messages.getMessage('description');
Consolidate.examples = messages.getMessage('examples').split(os.EOL);
Consolidate.flagsConfig = {
'max-version-bump': dependabot_1.maxVersionBumpFlag,
'base-branch': command_1.flags.string({
description: messages.getMessage('baseBranch'),
char: 'b',
default: 'main',
required: true,
}),
'target-branch': command_1.flags.string({
description: messages.getMessage('targetBranch'),
char: 't',
default: 'consolidate-dependabot',
required: true,
}),
ignore: command_1.flags.array({
description: messages.getMessage('ignore'),
default: [],
}),
dryrun: command_1.flags.boolean({
description: messages.getMessage('dryrun'),
char: 'd',
default: false,
}),
'no-pr': command_1.flags.boolean({
description: messages.getMessage('noPR'),
default: false,
}),
owner: command_1.flags.string({
description: messages.getMessage('owner'),
char: 'o',
}),
repo: command_1.flags.string({
description: messages.getMessage('repo'),
char: 'r',
dependsOn: ['owner'],
}),
};
//# sourceMappingURL=consolidate.js.map