2020-03-04 17:37:21 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-03-04 17:37:21 -05: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 17:57:47 -04:00
|
|
|
import {resolve} from 'path';
|
2020-03-04 17:37:21 -05:00
|
|
|
|
2020-03-27 21:14:52 -04:00
|
|
|
import {getRepoBaseDir} from '../utils/config';
|
2020-05-28 17:57:47 -04:00
|
|
|
import {debug, info} from '../utils/console';
|
|
|
|
import {allFiles} from '../utils/repo-files';
|
2020-03-20 09:59:35 -04:00
|
|
|
import {logGroup, logHeader} from './logging';
|
2020-07-20 14:53:26 -04:00
|
|
|
import {getGroupsFromYaml} from './parse-yaml';
|
2020-03-04 17:37:21 -05:00
|
|
|
|
2020-05-28 17:57:47 -04:00
|
|
|
export function verify() {
|
|
|
|
/** Full path to PullApprove config file */
|
|
|
|
const PULL_APPROVE_YAML_PATH = resolve(getRepoBaseDir(), '.pullapprove.yml');
|
|
|
|
/** All tracked files in the repository. */
|
|
|
|
const REPO_FILES = allFiles();
|
|
|
|
/** The pull approve config file. */
|
2020-03-20 09:59:35 -04:00
|
|
|
const pullApproveYamlRaw = readFileSync(PULL_APPROVE_YAML_PATH, 'utf8');
|
2020-05-28 17:57:47 -04:00
|
|
|
/** All of the groups defined in the pullapprove yaml. */
|
2020-07-20 14:53:26 -04:00
|
|
|
const groups = getGroupsFromYaml(pullApproveYamlRaw);
|
2020-05-28 17:57:47 -04: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 14:12:25 -04:00
|
|
|
const groupsSkipped = groups.filter(group => !group.conditions.length);
|
2020-05-28 17:57:47 -04:00
|
|
|
/** PullApprove groups with conditions. */
|
2020-04-16 14:12:25 -04:00
|
|
|
const groupsWithConditions = groups.filter(group => !!group.conditions.length);
|
2020-05-28 17:57:47 -04:00
|
|
|
/** Files which are matched by at least one group. */
|
2020-03-20 09:59:35 -04:00
|
|
|
const matchedFiles: string[] = [];
|
2020-05-28 17:57:47 -04:00
|
|
|
/** Files which are not matched by at least one group. */
|
2020-03-20 09:59:35 -04:00
|
|
|
const unmatchedFiles: string[] = [];
|
|
|
|
|
|
|
|
// Test each file in the repo against each group for being matched.
|
|
|
|
REPO_FILES.forEach((file: string) => {
|
2020-04-16 14:12:25 -04:00
|
|
|
if (groupsWithConditions.filter(group => group.testFile(file)).length) {
|
2020-03-20 09:59:35 -04:00
|
|
|
matchedFiles.push(file);
|
2020-03-04 17:37:21 -05:00
|
|
|
} else {
|
2020-03-20 09:59:35 -04:00
|
|
|
unmatchedFiles.push(file);
|
2020-03-04 17:37:21 -05:00
|
|
|
}
|
2020-03-20 09:59:35 -04:00
|
|
|
});
|
2020-05-28 17:57:47 -04:00
|
|
|
/** Results for each group */
|
2020-04-16 14:12:25 -04:00
|
|
|
const resultsByGroup = groupsWithConditions.map(group => group.getResults());
|
2020-05-28 17:57:47 -04:00
|
|
|
/**
|
|
|
|
* Whether all group condition lines match at least one file and all files
|
|
|
|
* are matched by at least one group.
|
|
|
|
*/
|
2020-03-20 09:59:35 -04:00
|
|
|
const verificationSucceeded =
|
|
|
|
resultsByGroup.every(r => !r.unmatchedCount) && !unmatchedFiles.length;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overall result
|
|
|
|
*/
|
|
|
|
logHeader('Overall Result');
|
|
|
|
if (verificationSucceeded) {
|
2020-05-20 17:41:44 -04:00
|
|
|
info('PullApprove verification succeeded!');
|
2020-03-20 09:59:35 -04:00
|
|
|
} else {
|
2020-05-20 17:41:44 -04: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 17:37:21 -05:00
|
|
|
}
|
2020-03-20 09:59:35 -04:00
|
|
|
/**
|
|
|
|
* File by file Summary
|
|
|
|
*/
|
|
|
|
logHeader('PullApprove results by file');
|
2020-05-20 17:41:44 -04:00
|
|
|
info.group(`Matched Files (${matchedFiles.length} files)`);
|
2020-05-28 17:57:47 -04:00
|
|
|
matchedFiles.forEach(file => debug(file));
|
2020-05-20 17:41:44 -04:00
|
|
|
info.groupEnd();
|
|
|
|
info.group(`Unmatched Files (${unmatchedFiles.length} files)`);
|
|
|
|
unmatchedFiles.forEach(file => info(file));
|
|
|
|
info.groupEnd();
|
2020-03-20 09:59:35 -04:00
|
|
|
/**
|
|
|
|
* Group by group Summary
|
|
|
|
*/
|
|
|
|
logHeader('PullApprove results by group');
|
2020-05-20 17:41:44 -04:00
|
|
|
info.group(`Groups skipped (${groupsSkipped.length} groups)`);
|
2020-05-28 17:57:47 -04:00
|
|
|
groupsSkipped.forEach(group => debug(`${group.groupName}`));
|
2020-05-20 17:41:44 -04:00
|
|
|
info.groupEnd();
|
2020-03-20 09:59:35 -04:00
|
|
|
const matchedGroups = resultsByGroup.filter(group => !group.unmatchedCount);
|
2020-05-20 17:41:44 -04:00
|
|
|
info.group(`Matched conditions by Group (${matchedGroups.length} groups)`);
|
2020-05-28 17:57:47 -04:00
|
|
|
matchedGroups.forEach(group => logGroup(group, true, debug));
|
2020-05-20 17:41:44 -04:00
|
|
|
info.groupEnd();
|
2020-03-20 09:59:35 -04:00
|
|
|
const unmatchedGroups = resultsByGroup.filter(group => group.unmatchedCount);
|
2020-05-20 17:41:44 -04:00
|
|
|
info.group(`Unmatched conditions by Group (${unmatchedGroups.length} groups)`);
|
2020-03-20 09:59:35 -04:00
|
|
|
unmatchedGroups.forEach(group => logGroup(group, false));
|
2020-05-20 17:41:44 -04:00
|
|
|
info.groupEnd();
|
2020-03-20 09:59:35 -04:00
|
|
|
|
|
|
|
// Provide correct exit code based on verification success.
|
|
|
|
process.exit(verificationSucceeded ? 0 : 1);
|
2020-03-04 17:37:21 -05:00
|
|
|
}
|