|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const getMetadata = require('../metadata'); |
| 4 | +const CLI = require('../../lib/cli'); |
| 5 | +const Request = require('../../lib/request'); |
| 6 | +const { runPromise } = require('../../lib/run'); |
| 7 | +const LandingSession = require('../../lib/landing_session'); |
| 8 | +const epilogue = require('./epilogue'); |
| 9 | +const yargs = require('yargs'); |
| 10 | + |
| 11 | +const landOptions = { |
| 12 | + apply: { |
| 13 | + describe: 'Apply a patch with the given PR id', |
| 14 | + type: 'number' |
| 15 | + }, |
| 16 | + amend: { |
| 17 | + describe: 'Amend the current commit', |
| 18 | + type: 'boolean' |
| 19 | + }, |
| 20 | + continue: { |
| 21 | + alias: 'c', |
| 22 | + describe: 'Continue the landing session', |
| 23 | + type: 'boolean' |
| 24 | + }, |
| 25 | + final: { |
| 26 | + describe: 'Verify the landed PR and clean up', |
| 27 | + type: 'boolean' |
| 28 | + }, |
| 29 | + abort: { |
| 30 | + describe: 'Abort the current landing session', |
| 31 | + type: 'boolean' |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +function builder(yargs) { |
| 36 | + return yargs |
| 37 | + .options(landOptions).positional('prid', { |
| 38 | + describe: 'ID of the Pull Request', |
| 39 | + type: 'number' |
| 40 | + }) |
| 41 | + .epilogue(epilogue) |
| 42 | + .example('git node land 12344', |
| 43 | + 'Land https://github.com/nodejs/node/pull/12344 in the current directory') |
| 44 | + .example('git node land --abort', |
| 45 | + 'Abort the current session') |
| 46 | + .example('git node land --amend', |
| 47 | + 'Append metadata to the current commit message') |
| 48 | + .example('git node land --final', |
| 49 | + 'Verify the landed PR and clean up') |
| 50 | + .example('git node land --continue', |
| 51 | + 'Continue the current landing session'); |
| 52 | +} |
| 53 | + |
| 54 | +const START = 'start'; |
| 55 | +const APPLY = 'apply'; |
| 56 | +const AMEND = 'amend'; |
| 57 | +const FINAL = 'final'; |
| 58 | +const CONTINUE = 'continue'; |
| 59 | +const ABORT = 'abort'; |
| 60 | + |
| 61 | +function handler(argv) { |
| 62 | + if (argv.prid && Number.isInteger(argv.prid)) { |
| 63 | + return land(START, argv); |
| 64 | + } |
| 65 | + const provided = []; |
| 66 | + for (const type of Object.keys(landOptions)) { |
| 67 | + if (argv[type]) { |
| 68 | + provided.push(type); |
| 69 | + } |
| 70 | + } |
| 71 | + if (provided.length === 0) { |
| 72 | + yargs.showHelp(); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (provided.length > 1) { |
| 76 | + yargs.showHelp(); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + return land(provided[0], argv); |
| 81 | +} |
| 82 | + |
| 83 | +function land(state, argv) { |
| 84 | + const cli = new CLI(process.stderr); |
| 85 | + const req = new Request(); |
| 86 | + const dir = process.cwd(); |
| 87 | + |
| 88 | + return runPromise(main(state, argv, cli, req, dir)).catch((err) => { |
| 89 | + if (cli.spinner.enabled) { |
| 90 | + cli.spinner.fail(); |
| 91 | + } |
| 92 | + throw err; |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +module.exports = { |
| 97 | + command: 'land [prid|options]', |
| 98 | + describe: |
| 99 | + 'Manage the current landing session or start a new one for a pull request', |
| 100 | + builder, |
| 101 | + handler |
| 102 | +}; |
| 103 | + |
| 104 | +async function main(state, argv, cli, req, dir) { |
| 105 | + let session = new LandingSession(cli, req, dir); |
| 106 | + |
| 107 | + try { |
| 108 | + session.restore(); |
| 109 | + } catch (err) { // JSON error? |
| 110 | + if (state === ABORT) { |
| 111 | + await session.abort(); |
| 112 | + return; |
| 113 | + } |
| 114 | + cli.warn( |
| 115 | + 'Failed to detect previous session. ' + |
| 116 | + 'please run `git node land --abort`'); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (state === START) { |
| 121 | + if (session.hasStarted()) { |
| 122 | + cli.warn( |
| 123 | + 'Previous `git node land` session for ' + |
| 124 | + `${session.pullName} in progress.`); |
| 125 | + cli.log('run `git node land --abort` before starting a new session'); |
| 126 | + return; |
| 127 | + } |
| 128 | + session = new LandingSession(cli, req, dir, argv.prid); |
| 129 | + const { repo, owner, prid } = session; |
| 130 | + const metadata = await getMetadata({ repo, owner, prid }, cli); |
| 131 | + return session.start(metadata); |
| 132 | + } else if (state === APPLY) { |
| 133 | + return session.apply(); |
| 134 | + } else if (state === AMEND) { |
| 135 | + return session.amend(); |
| 136 | + } else if (state === FINAL) { |
| 137 | + return session.final(); |
| 138 | + } else if (state === ABORT) { |
| 139 | + return session.abort(); |
| 140 | + } else if (state === CONTINUE) { |
| 141 | + return session.continue(); |
| 142 | + } |
| 143 | +} |
0 commit comments