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-05-28 17:57:47 -04:00
|
|
|
import {debug, info} from '../utils/console';
|
2021-06-03 09:59:20 -04:00
|
|
|
import {GitClient} from '../utils/git/git-client';
|
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() {
|
2021-06-03 09:59:20 -04:00
|
|
|
const git = GitClient.get();
|
2020-05-28 17:57:47 -04:00
|
|
|
/** Full path to PullApprove config file */
|
2021-04-12 17:50:50 -04:00
|
|
|
const PULL_APPROVE_YAML_PATH = resolve(git.baseDir, '.pullapprove.yml');
|
2020-05-28 17:57:47 -04:00
|
|
|
/** All tracked files in the repository. */
|
2021-04-12 17:50:50 -04:00
|
|
|
const REPO_FILES = git.allFiles();
|
2020-05-28 17:57:47 -04:00
|
|
|
/** 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.
|
|
|
|
*/
|
2021-06-21 13:06:24 -04:00
|
|
|
const allGroupConditionsValid =
|
2020-03-20 09:59:35 -04:00
|
|
|
resultsByGroup.every(r => !r.unmatchedCount) && !unmatchedFiles.length;
|
2021-06-21 13:06:24 -04:00
|
|
|
/** Whether all groups have at least one reviewer user or team defined. */
|
|
|
|
const groupsWithoutReviewers = groups.filter(group => Object.keys(group.reviewers).length === 0);
|
|
|
|
/** The overall result of the verifcation. */
|
|
|
|
const overallResult = allGroupConditionsValid && groupsWithoutReviewers.length === 0;
|
2020-03-20 09:59:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overall result
|
|
|
|
*/
|
|
|
|
logHeader('Overall Result');
|
2021-06-21 13:06:24 -04:00
|
|
|
if (overallResult) {
|
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
|
|
|
}
|
2021-06-21 13:06:24 -04:00
|
|
|
/** Reviewers check */
|
|
|
|
logHeader(`Group Reviewers Check`);
|
|
|
|
if (groupsWithoutReviewers.length === 0) {
|
|
|
|
info('All group contain at least one reviewer user or team.');
|
|
|
|
} else {
|
|
|
|
info.group(`Discovered ${groupsWithoutReviewers.length} group(s) without a reviewer defined`);
|
|
|
|
groupsWithoutReviewers.forEach(g => info(g.groupName));
|
|
|
|
info.groupEnd();
|
|
|
|
}
|
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-07-23 18:53:46 -04:00
|
|
|
matchedGroups.forEach(group => logGroup(group, 'matchedConditions', 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-07-23 18:53:46 -04: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 17:41:44 -04:00
|
|
|
info.groupEnd();
|
2020-03-20 09:59:35 -04:00
|
|
|
|
|
|
|
// Provide correct exit code based on verification success.
|
2021-06-21 13:06:24 -04:00
|
|
|
process.exit(overallResult ? 0 : 1);
|
2020-03-04 17:37:21 -05:00
|
|
|
}
|