backport
Version:
A CLI tool that automates the process of backporting commits
82 lines • 3.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCommits = void 0;
const chalk_1 = __importDefault(require("chalk"));
const BackportError_1 = require("./BackportError");
const commitFormatters_1 = require("./github/commitFormatters");
const fetchCommitByPullNumber_1 = require("./github/v4/fetchCommits/fetchCommitByPullNumber");
const fetchCommitBySha_1 = require("./github/v4/fetchCommits/fetchCommitBySha");
const fetchCommitsByAuthor_1 = require("./github/v4/fetchCommits/fetchCommitsByAuthor");
const fetchPullRequestsBySearchQuery_1 = require("./github/v4/fetchCommits/fetchPullRequestsBySearchQuery");
const ora_1 = require("./ora");
const prompts_1 = require("./prompts");
function getOraPersistsOption(question, answer) {
return {
symbol: chalk_1.default.green('?'),
text: `${chalk_1.default.bold(question)} ${chalk_1.default.cyan(answer)}`,
};
}
async function getCommits(options) {
const spinner = (0, ora_1.ora)(options.interactive).start();
try {
if (options.sha) {
const shas = Array.isArray(options.sha) ? options.sha : [options.sha];
spinner.text = `Loading commit "${shas.map(commitFormatters_1.getShortSha)}"`;
const commits = await Promise.all(shas.map((sha) => (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha })));
spinner.stopAndPersist(getOraPersistsOption('Select commit', commits
.map((commit) => (0, commitFormatters_1.getFirstLine)(commit.sourceCommit.message))
.join(', ')));
return commits;
}
if (options.pullNumber) {
const pullNumbers = Array.isArray(options.pullNumber)
? options.pullNumber
: [options.pullNumber];
spinner.text = `Loading pull request ${pullNumbers
.map((pullNumber) => `#${pullNumber}`)
.join(', ')}`;
const nestedCommits = await Promise.all(pullNumbers.map((pullNumber) => (0, fetchCommitByPullNumber_1.fetchCommitsByPullNumber)({ ...options, pullNumber })));
const commits = nestedCommits.flat();
// add styles to make it look like a prompt question
spinner.stopAndPersist(getOraPersistsOption('Select pull request', commits
.map((commit) => (0, commitFormatters_1.getFirstLine)(commit.sourceCommit.message))
.join(', ')));
return commits;
}
if (!options.interactive && !options.ls) {
throw new BackportError_1.BackportError('When "--interactive" is disabled either `--sha` or `--pr` must be specified');
}
spinner.text = options.prFilter
? 'Loading pull requests...'
: `Loading commits from branch "${options.sourceBranch}"...`;
const commitChoices = options.prFilter !== undefined
? await (0, fetchPullRequestsBySearchQuery_1.fetchPullRequestsBySearchQuery)({
...options,
prFilter: options.prFilter,
})
: await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)(options);
spinner.stop();
if (options.ls) {
return commitChoices;
}
return (0, prompts_1.promptForCommits)({
commitChoices,
isMultipleChoice: options.multipleCommits,
showDetails: options.details,
});
}
catch (e) {
if (options.ls) {
spinner.stop();
}
else {
spinner.fail();
}
throw e;
}
}
exports.getCommits = getCommits;
//# sourceMappingURL=getCommits.js.map