| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {Arguments, Argv} from 'yargs'; | 
					
						
							| 
									
										
										
										
											2020-05-20 14:48:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {error, red, yellow} from '../../utils/console'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  | import {GITHUB_TOKEN_GENERATE_URL, mergePullRequest} from './index'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 12:20:55 -07:00
										 |  |  | /** The options available to the merge command via CLI. */ | 
					
						
							|  |  |  | export interface MergeCommandOptions { | 
					
						
							|  |  |  |   'github-token'?: string; | 
					
						
							|  |  |  |   'pr-number': number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  | /** Builds the options for the merge command. */ | 
					
						
							| 
									
										
										
										
											2020-08-14 12:20:55 -07:00
										 |  |  | export function buildMergeCommand(yargs: Argv): Argv<MergeCommandOptions> { | 
					
						
							|  |  |  |   return yargs.help() | 
					
						
							|  |  |  |       .strict() | 
					
						
							|  |  |  |       .positional('pr-number', {demandOption: true, type: 'number'}) | 
					
						
							|  |  |  |       .option('github-token', { | 
					
						
							|  |  |  |         type: 'string', | 
					
						
							|  |  |  |         description: 'Github token. If not set, token is retrieved from the environment variables.' | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Handles the merge command. i.e. performs the merge of a specified pull request. */ | 
					
						
							| 
									
										
										
										
											2020-08-14 12:20:55 -07:00
										 |  |  | export async function handleMergeCommand(args: Arguments<MergeCommandOptions>) { | 
					
						
							|  |  |  |   const githubToken = args['github-token'] || process.env.GITHUB_TOKEN || process.env.TOKEN; | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  |   if (!githubToken) { | 
					
						
							| 
									
										
										
										
											2020-05-20 14:48:50 -07:00
										 |  |  |     error(red('No Github token set. Please set the `GITHUB_TOKEN` environment variable.')); | 
					
						
							|  |  |  |     error(red('Alternatively, pass the `--github-token` command line flag.')); | 
					
						
							|  |  |  |     error(yellow(`You can generate a token here: ${GITHUB_TOKEN_GENERATE_URL}`)); | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  |     process.exit(1); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-14 12:20:55 -07:00
										 |  |  |   await mergePullRequest(args['pr-number'], githubToken); | 
					
						
							| 
									
										
										
										
											2020-05-15 17:21:01 +02:00
										 |  |  | } |