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';
|
|
|
|
import * as path from 'path';
|
|
|
|
import {cd, exec, set} from 'shelljs';
|
|
|
|
|
2020-03-27 21:14:52 -04:00
|
|
|
import {getRepoBaseDir} from '../utils/config';
|
2020-05-20 17:41:44 -04:00
|
|
|
import {info} from '../utils/console';
|
2020-03-27 21:14:52 -04:00
|
|
|
|
2020-03-20 09:59:35 -04:00
|
|
|
import {PullApproveGroup} from './group';
|
|
|
|
import {logGroup, logHeader} from './logging';
|
|
|
|
import {parsePullApproveYaml} from './parse-yaml';
|
2020-03-04 17:37:21 -05:00
|
|
|
|
2020-04-16 14:12:25 -04:00
|
|
|
export function verify(verbose = false) {
|
2020-03-04 17:37:21 -05:00
|
|
|
// Exit early on shelljs errors
|
|
|
|
set('-e');
|
2020-03-20 09:59:35 -04:00
|
|
|
// Full path of the angular project directory
|
|
|
|
const PROJECT_DIR = getRepoBaseDir();
|
|
|
|
// Change to the Angular project directory
|
|
|
|
cd(PROJECT_DIR);
|
2020-03-04 17:37:21 -05:00
|
|
|
// Full path to PullApprove config file
|
2020-03-20 09:59:35 -04:00
|
|
|
const PULL_APPROVE_YAML_PATH = path.resolve(PROJECT_DIR, '.pullapprove.yml');
|
2020-03-04 17:37:21 -05:00
|
|
|
// All relative path file names in the git repo, this is retrieved using git rather
|
|
|
|
// that a glob so that we only get files that are checked in, ignoring things like
|
|
|
|
// node_modules, .bazelrc.user, etc
|
2020-03-20 09:59:35 -04:00
|
|
|
const REPO_FILES =
|
|
|
|
exec('git ls-files', {silent: true}).trim().split('\n').filter((_: string) => !!_);
|
|
|
|
// The pull approve config file.
|
|
|
|
const pullApproveYamlRaw = readFileSync(PULL_APPROVE_YAML_PATH, 'utf8');
|
|
|
|
// JSON representation of the pullapprove yaml file.
|
|
|
|
const pullApprove = parsePullApproveYaml(pullApproveYamlRaw);
|
|
|
|
// All of the groups defined in the pullapprove yaml.
|
|
|
|
const groups = Object.entries(pullApprove.groups).map(([groupName, group]) => {
|
|
|
|
return new PullApproveGroup(groupName, group);
|
|
|
|
});
|
2020-04-16 14:12:25 -04:00
|
|
|
// PullApprove groups without conditions. These are skipped in the verification
|
|
|
|
// as those would always be active and cause zero unmatched files.
|
|
|
|
const groupsSkipped = groups.filter(group => !group.conditions.length);
|
|
|
|
// PullApprove groups with conditions.
|
|
|
|
const groupsWithConditions = groups.filter(group => !!group.conditions.length);
|
2020-03-20 09:59:35 -04:00
|
|
|
// Files which are matched by at least one group.
|
|
|
|
const matchedFiles: string[] = [];
|
|
|
|
// Files which are not matched by at least one group.
|
|
|
|
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
|
|
|
});
|
|
|
|
// Results for each group
|
2020-04-16 14:12:25 -04:00
|
|
|
const resultsByGroup = groupsWithConditions.map(group => group.getResults());
|
2020-03-20 09:59:35 -04:00
|
|
|
// Whether all group condition lines match at least one file and all files
|
|
|
|
// are matched by at least one group.
|
|
|
|
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)`);
|
|
|
|
verbose && matchedFiles.forEach(file => info(file));
|
|
|
|
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)`);
|
|
|
|
verbose && groupsSkipped.forEach(group => info(`${group.groupName}`));
|
|
|
|
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-04-16 14:12:25 -04:00
|
|
|
verbose && matchedGroups.forEach(group => logGroup(group));
|
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
|
|
|
}
|