|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import chalk from 'chalk'; |
| 10 | +import * as minimist from 'minimist'; |
| 11 | +import {isAbsolute, resolve} from 'path'; |
| 12 | + |
| 13 | +import {Config, readAndValidateConfig} from './config'; |
| 14 | +import {promptConfirm} from './console'; |
| 15 | +import {MergeResult, MergeStatus, PullRequestMergeTask} from './index'; |
| 16 | + |
| 17 | +// Run the CLI. |
| 18 | +main(); |
| 19 | + |
| 20 | +/** |
| 21 | + * Entry-point for the merge script CLI. The script can be used to merge individual pull requests |
| 22 | + * into branches based on the `PR target` labels that have been set in a configuration. The script |
| 23 | + * aims to reduce the manual work that needs to be performed to cherry-pick a PR into multiple |
| 24 | + * branches based on a target label. |
| 25 | + */ |
| 26 | +async function main() { |
| 27 | + const {config, prNumber, force, githubToken} = parseCommandLine(); |
| 28 | + const api = new PullRequestMergeTask(config, githubToken); |
| 29 | + |
| 30 | + // Perform the merge. Force mode can be activated through a command line flag. |
| 31 | + // Alternatively, if the merge fails with non-fatal failures, the script |
| 32 | + // will prompt whether it should rerun in force mode. |
| 33 | + const mergeResult = await api.merge(prNumber, force); |
| 34 | + |
| 35 | + // Handle the result of the merge. If failures have been reported, exit |
| 36 | + // the process with a non-zero exit code. |
| 37 | + if (!await handleMergeResult(mergeResult, force)) { |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Prompts whether the specified pull request should be forcibly merged. If so, merges |
| 43 | + * the specified pull request forcibly (ignoring non-critical failures). |
| 44 | + * @returns Whether the specified pull request has been forcibly merged. |
| 45 | + */ |
| 46 | + async function promptAndPerformForceMerge(): Promise<boolean> { |
| 47 | + if (await promptConfirm('Do you want to forcibly proceed with merging?')) { |
| 48 | + // Perform the merge in force mode. This means that non-fatal failures |
| 49 | + // are ignored and the merge continues. |
| 50 | + const forceMergeResult = await api.merge(prNumber, true); |
| 51 | + // Handle the merge result. Note that we disable the force merge prompt since |
| 52 | + // a failed force merge will never succeed with a second force merge. |
| 53 | + return await handleMergeResult(forceMergeResult, true); |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Handles the merge result by printing console messages, exiting the process |
| 60 | + * based on the result, or by restarting the merge if force mode has been enabled. |
| 61 | + * @returns Whether the merge was successful or not. |
| 62 | + */ |
| 63 | + async function handleMergeResult(result: MergeResult, disableForceMergePrompt = false) { |
| 64 | + const {failure, status} = result; |
| 65 | + const canForciblyMerge = failure && failure.nonFatal; |
| 66 | + |
| 67 | + switch (status) { |
| 68 | + case MergeStatus.SUCCESS: |
| 69 | + console.info(chalk.green(`Successfully merged the pull request: ${prNumber}`)); |
| 70 | + return true; |
| 71 | + case MergeStatus.DIRTY_WORKING_DIR: |
| 72 | + console.error(chalk.red( |
| 73 | + `Local working repository not clean. Please make sure there are ` + |
| 74 | + `no uncommitted changes.`)); |
| 75 | + return false; |
| 76 | + case MergeStatus.UNKNOWN_GIT_ERROR: |
| 77 | + console.error(chalk.red( |
| 78 | + 'An unknown Git error has been thrown. Please check the output ' + |
| 79 | + 'above for details.')); |
| 80 | + return false; |
| 81 | + case MergeStatus.FAILED: |
| 82 | + console.error(chalk.yellow(`Could not merge the specified pull request.`)); |
| 83 | + console.error(chalk.red(failure!.message)); |
| 84 | + if (canForciblyMerge && !disableForceMergePrompt) { |
| 85 | + console.info(); |
| 86 | + console.info(chalk.yellow('The pull request above failed due to non-critical errors.')); |
| 87 | + console.info(chalk.yellow(`This error can be forcibly ignored if desired.`)); |
| 88 | + return await promptAndPerformForceMerge(); |
| 89 | + } |
| 90 | + return false; |
| 91 | + default: |
| 92 | + throw Error(`Unexpected merge result: ${status}`); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TODO(devversion): Use Yargs for this once the script has been moved to `angular/angular`. |
| 98 | +/** Parses the command line and returns the passed options. */ |
| 99 | +function parseCommandLine(): |
| 100 | + {config: Config, force: boolean, prNumber: number, dryRun?: boolean, githubToken: string} { |
| 101 | + const {config: configPath, githubToken: githubTokenArg, force, _: [prNumber]} = |
| 102 | + minimist<any>(process.argv.slice(2), { |
| 103 | + string: ['githubToken', 'config', 'pr'], |
| 104 | + alias: { |
| 105 | + 'githubToken': 'github-token', |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + if (!configPath) { |
| 110 | + console.error(chalk.red('No configuration file specified. Please pass the `--config` option.')); |
| 111 | + process.exit(1); |
| 112 | + } |
| 113 | + |
| 114 | + if (!prNumber) { |
| 115 | + console.error(chalk.red('No pull request specified. Please pass a pull request number.')); |
| 116 | + process.exit(1); |
| 117 | + } |
| 118 | + |
| 119 | + const configFilePath = isAbsolute(configPath) ? configPath : resolve(configPath); |
| 120 | + const {config, errors} = readAndValidateConfig(configFilePath); |
| 121 | + |
| 122 | + if (errors) { |
| 123 | + console.error(chalk.red('Configuration could not be read:')); |
| 124 | + errors.forEach(desc => console.error(chalk.yellow(` * ${desc}`))); |
| 125 | + process.exit(1); |
| 126 | + } |
| 127 | + |
| 128 | + const githubToken = githubTokenArg || process.env.GITHUB_TOKEN || process.env.TOKEN; |
| 129 | + if (!githubToken) { |
| 130 | + console.error( |
| 131 | + chalk.red('No Github token is set. Please set the `GITHUB_TOKEN` environment variable.')); |
| 132 | + console.error(chalk.red('Alternatively, pass the `--github-token` command line flag.')); |
| 133 | + process.exit(1); |
| 134 | + } |
| 135 | + |
| 136 | + return {config: config!, prNumber, githubToken, force}; |
| 137 | +} |
0 commit comments