UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

62 lines 2.65 kB
import { isEmpty, isString } from 'lodash-es'; import { BackportError } from './backport-error.js'; import { getSourceBranchFromCommits } from './get-source-branch-from-commits.js'; import { promptForTargetBranches } from './prompts.js'; export async function getTargetBranches(options, commits) { // target branches already specified (in contrast to letting the user choose from a list) if (!isEmpty(options.targetBranches)) { return options.targetBranches; } // target branches from the first commit const suggestedTargetBranches = commits.at(0)?.suggestedTargetBranches ?? []; // require target branches to be specified when when in non-interactive mode if (!options.interactive) { if (isEmpty(suggestedTargetBranches)) { throw new BackportError({ code: 'no-branches-exception' }); } return suggestedTargetBranches; } const sourceBranch = getSourceBranchFromCommits(commits); const targetBranchChoices = getTargetBranchChoices(options, suggestedTargetBranches, sourceBranch); // render prmompt for selecting target branches return promptForTargetBranches({ targetBranchChoices, isMultipleChoice: options.multipleBranches, }); } export function getTargetBranchChoices(options, suggestedTargetBranches, sourceBranch) { // exclude sourceBranch from targetBranchChoices const targetBranchesChoices = getTargetBranchChoicesAsObject(options.targetBranchChoices).filter((choice) => choice.name !== sourceBranch); if (isEmpty(targetBranchesChoices)) { throw new BackportError({ code: 'config-error-exception', message: 'Missing target branch choices', }); } if (!options.branchLabelMapping) { return targetBranchesChoices; } // select missing target branches (based on pull request labels) return targetBranchesChoices.map((choice) => { const isChecked = suggestedTargetBranches.includes(choice.name); return { ...choice, checked: isChecked }; }); } // `targetBranchChoices` can either be a string or an object. // It must be transformed so it is always treated as an object troughout the application function getTargetBranchChoicesAsObject(targetBranchChoices) { if (!targetBranchChoices) { return []; } return targetBranchChoices.map((choice) => { if (isString(choice)) { return { name: choice, value: choice, checked: false, }; } return { ...choice, value: choice.name }; }); } //# sourceMappingURL=get-target-branches.js.map