| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 {readFileSync} from 'fs'; | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  | import {resolve} from 'path'; | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  | import {debug, info} from '../utils/console'; | 
					
						
							| 
									
										
										
										
											2021-06-03 15:59:20 +02:00
										 |  |  | import {GitClient} from '../utils/git/git-client'; | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  | import {logGroup, logHeader} from './logging'; | 
					
						
							| 
									
										
										
										
											2020-07-20 11:53:26 -07:00
										 |  |  | import {getGroupsFromYaml} from './parse-yaml'; | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  | export function verify() { | 
					
						
							| 
									
										
										
										
											2021-06-03 15:59:20 +02:00
										 |  |  |   const git = GitClient.get(); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** Full path to PullApprove config file */ | 
					
						
							| 
									
										
										
										
											2021-04-12 14:50:50 -07:00
										 |  |  |   const PULL_APPROVE_YAML_PATH = resolve(git.baseDir, '.pullapprove.yml'); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** All tracked files in the repository. */ | 
					
						
							| 
									
										
										
										
											2021-04-12 14:50:50 -07:00
										 |  |  |   const REPO_FILES = git.allFiles(); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** The pull approve config file. */ | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const pullApproveYamlRaw = readFileSync(PULL_APPROVE_YAML_PATH, 'utf8'); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** All of the groups defined in the pullapprove yaml. */ | 
					
						
							| 
									
										
										
										
											2020-07-20 11:53:26 -07:00
										 |  |  |   const groups = getGroupsFromYaml(pullApproveYamlRaw); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * PullApprove groups without conditions. These are skipped in the verification | 
					
						
							|  |  |  |    * as those would always be active and cause zero unmatched files. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-04-16 20:12:25 +02:00
										 |  |  |   const groupsSkipped = groups.filter(group => !group.conditions.length); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** PullApprove groups with conditions. */ | 
					
						
							| 
									
										
										
										
											2020-04-16 20:12:25 +02:00
										 |  |  |   const groupsWithConditions = groups.filter(group => !!group.conditions.length); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** Files which are matched by at least one group. */ | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const matchedFiles: string[] = []; | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** Files which are not matched by at least one group. */ | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const unmatchedFiles: string[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Test each file in the repo against each group for being matched.
 | 
					
						
							|  |  |  |   REPO_FILES.forEach((file: string) => { | 
					
						
							| 
									
										
										
										
											2020-04-16 20:12:25 +02:00
										 |  |  |     if (groupsWithConditions.filter(group => group.testFile(file)).length) { | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |       matchedFiles.push(file); | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |       unmatchedFiles.push(file); | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** Results for each group */ | 
					
						
							| 
									
										
										
										
											2020-04-16 20:12:25 +02:00
										 |  |  |   const resultsByGroup = groupsWithConditions.map(group => group.getResults()); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Whether all group condition lines match at least one file and all files | 
					
						
							|  |  |  |    * are matched by at least one group. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const verificationSucceeded = | 
					
						
							|  |  |  |       resultsByGroup.every(r => !r.unmatchedCount) && !unmatchedFiles.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Overall result | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   logHeader('Overall Result'); | 
					
						
							|  |  |  |   if (verificationSucceeded) { | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |     info('PullApprove verification succeeded!'); | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |     info(`PullApprove verification failed.`); | 
					
						
							|  |  |  |     info(); | 
					
						
							|  |  |  |     info(`Please update '.pullapprove.yml' to ensure that all necessary`); | 
					
						
							|  |  |  |     info(`files/directories have owners and all patterns that appear in`); | 
					
						
							|  |  |  |     info(`the file correspond to actual files/directories in the repo.`); | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * File by file Summary | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   logHeader('PullApprove results by file'); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.group(`Matched Files (${matchedFiles.length} files)`); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   matchedFiles.forEach(file => debug(file)); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.groupEnd(); | 
					
						
							|  |  |  |   info.group(`Unmatched Files (${unmatchedFiles.length} files)`); | 
					
						
							|  |  |  |   unmatchedFiles.forEach(file => info(file)); | 
					
						
							|  |  |  |   info.groupEnd(); | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Group by group Summary | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   logHeader('PullApprove results by group'); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.group(`Groups skipped (${groupsSkipped.length} groups)`); | 
					
						
							| 
									
										
										
										
											2020-05-28 14:57:47 -07:00
										 |  |  |   groupsSkipped.forEach(group => debug(`${group.groupName}`)); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.groupEnd(); | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const matchedGroups = resultsByGroup.filter(group => !group.unmatchedCount); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.group(`Matched conditions by Group (${matchedGroups.length} groups)`); | 
					
						
							| 
									
										
										
										
											2020-07-23 15:53:46 -07:00
										 |  |  |   matchedGroups.forEach(group => logGroup(group, 'matchedConditions', debug)); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.groupEnd(); | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  |   const unmatchedGroups = resultsByGroup.filter(group => group.unmatchedCount); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.group(`Unmatched conditions by Group (${unmatchedGroups.length} groups)`); | 
					
						
							| 
									
										
										
										
											2020-07-23 15:53:46 -07:00
										 |  |  |   unmatchedGroups.forEach(group => logGroup(group, 'unmatchedConditions')); | 
					
						
							|  |  |  |   info.groupEnd(); | 
					
						
							|  |  |  |   const unverifiableConditionsInGroups = | 
					
						
							|  |  |  |       resultsByGroup.filter(group => group.unverifiableConditions.length > 0); | 
					
						
							|  |  |  |   info.group(`Unverifiable conditions by Group (${unverifiableConditionsInGroups.length} groups)`); | 
					
						
							|  |  |  |   unverifiableConditionsInGroups.forEach(group => logGroup(group, 'unverifiableConditions')); | 
					
						
							| 
									
										
										
										
											2020-05-20 14:41:44 -07:00
										 |  |  |   info.groupEnd(); | 
					
						
							| 
									
										
										
										
											2020-03-20 06:59:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Provide correct exit code based on verification success.
 | 
					
						
							|  |  |  |   process.exit(verificationSucceeded ? 0 : 1); | 
					
						
							| 
									
										
										
										
											2020-03-04 14:37:21 -08:00
										 |  |  | } |