| 
									
										
										
										
											2020-05-15 17:19:13 +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 {MergeConfig, TargetLabel} from './config'; | 
					
						
							|  |  |  | import {matchesPattern} from './string-pattern'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 18:05:51 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Unique error that can be thrown in the merge configuration if an | 
					
						
							|  |  |  |  * invalid branch is targeted. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class InvalidTargetBranchError { | 
					
						
							|  |  |  |   constructor(public failureMessage: string) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Unique error that can be thrown in the merge configuration if an | 
					
						
							|  |  |  |  * invalid label has been applied to a pull request. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class InvalidTargetLabelError { | 
					
						
							|  |  |  |   constructor(public failureMessage: string) {} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 17:19:13 +02:00
										 |  |  | /** Gets the target label from the specified pull request labels. */ | 
					
						
							| 
									
										
										
										
											2020-07-24 17:47:30 +02:00
										 |  |  | export function getTargetLabelFromPullRequest( | 
					
						
							| 
									
										
										
										
											2020-12-16 10:02:48 -08:00
										 |  |  |     config: Pick<MergeConfig, 'labels'>, labels: string[]): TargetLabel { | 
					
						
							|  |  |  |   /** List of discovered target labels for the PR. */ | 
					
						
							|  |  |  |   const matches = []; | 
					
						
							| 
									
										
										
										
											2020-05-15 17:19:13 +02:00
										 |  |  |   for (const label of labels) { | 
					
						
							|  |  |  |     const match = config.labels.find(({pattern}) => matchesPattern(label, pattern)); | 
					
						
							|  |  |  |     if (match !== undefined) { | 
					
						
							| 
									
										
										
										
											2020-12-16 10:02:48 -08:00
										 |  |  |       matches.push(match); | 
					
						
							| 
									
										
										
										
											2020-05-15 17:19:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-12-16 10:02:48 -08:00
										 |  |  |   if (matches.length === 1) { | 
					
						
							|  |  |  |     return matches[0]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (matches.length === 0) { | 
					
						
							|  |  |  |     throw new InvalidTargetLabelError( | 
					
						
							|  |  |  |         'Unable to determine target for the PR as it has no target label.'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   throw new InvalidTargetLabelError( | 
					
						
							|  |  |  |       'Unable to determine target for the PR as it has multiple target labels.'); | 
					
						
							| 
									
										
										
										
											2020-05-15 17:19:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-24 18:05:51 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Gets the branches from the specified target label. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @throws {InvalidTargetLabelError} Invalid label has been applied to pull request. | 
					
						
							|  |  |  |  * @throws {InvalidTargetBranchError} Invalid Github target branch has been selected. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export async function getBranchesFromTargetLabel( | 
					
						
							|  |  |  |     label: TargetLabel, githubTargetBranch: string): Promise<string[]> { | 
					
						
							|  |  |  |   return typeof label.branches === 'function' ? await label.branches(githubTargetBranch) : | 
					
						
							|  |  |  |                                                 await label.branches; | 
					
						
							| 
									
										
										
										
											2020-05-15 17:19:13 +02:00
										 |  |  | } |