feat(dev-infra): migrate pullapprove tool to use new logging system (#37232)

Migrate the pullapprove tool in ng-dev to use new logging system rather
than directly calling console.* to create a better experience
for users.

PR Close #37232
This commit is contained in:
Joey Perrott 2020-05-20 14:41:44 -07:00 committed by Matias Niemelä
parent 9b3ca19675
commit 367f6fe12d
3 changed files with 35 additions and 28 deletions

View File

@ -5,6 +5,8 @@
* 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 {error} from '../utils/console';
import {convertConditionToFunction} from './condition_evaluator';
import {PullApproveGroupConfig} from './parse-yaml';
@ -58,9 +60,11 @@ export class PullApproveGroup {
matchedFiles: new Set(),
});
} catch (e) {
console.error(`Could not parse condition in group: ${this.groupName}`);
console.error(` - ${expression}`);
console.error(`Error:`, e.message, e.stack);
error(`Could not parse condition in group: ${this.groupName}`);
error(` - ${expression}`);
error(`Error:`);
error(e.message);
error(e.stack);
process.exit(1);
}
});
@ -84,7 +88,7 @@ export class PullApproveGroup {
`From the [${this.groupName}] group:\n` +
` - ${expression}` +
`\n\n${e.message} ${e.stack}\n\n`;
console.error(errMessage);
error(errMessage);
process.exit(1);
}
});

View File

@ -6,18 +6,19 @@
* found in the LICENSE file at https://angular.io/license
*/
import {info} from '../utils/console';
import {PullApproveGroupResult} from './group';
/** Create logs for each pullapprove group result. */
export function logGroup(group: PullApproveGroupResult, matched = true) {
const conditions = matched ? group.matchedConditions : group.unmatchedConditions;
console.groupCollapsed(`[${group.groupName}]`);
info.group(`[${group.groupName}]`);
if (conditions.length) {
conditions.forEach(matcher => {
const count = matcher.matchedFiles.size;
console.info(`${count} ${count === 1 ? 'match' : 'matches'} - ${matcher.expression}`)
info(`${count} ${count === 1 ? 'match' : 'matches'} - ${matcher.expression}`);
});
console.groupEnd();
info.groupEnd();
}
}
@ -30,7 +31,7 @@ export function logHeader(...params: string[]) {
const rightSpace = fillWidth - leftSpace - headerText.length;
const fill = (count: number, content: string) => content.repeat(count);
console.info(`${fill(fillWidth, '─')}`);
console.info(`${fill(leftSpace, ' ')}${headerText}${fill(rightSpace, ' ')}`);
console.info(`${fill(fillWidth, '─')}`);
info(`${fill(fillWidth, '─')}`);
info(`${fill(leftSpace, ' ')}${headerText}${fill(rightSpace, ' ')}`);
info(`${fill(fillWidth, '─')}`);
}

View File

@ -10,6 +10,7 @@ import * as path from 'path';
import {cd, exec, set} from 'shelljs';
import {getRepoBaseDir} from '../utils/config';
import {info} from '../utils/console';
import {PullApproveGroup} from './group';
import {logGroup, logHeader} from './logging';
@ -67,38 +68,39 @@ export function verify(verbose = false) {
*/
logHeader('Overall Result');
if (verificationSucceeded) {
console.info('PullApprove verification succeeded!');
info('PullApprove verification succeeded!');
} else {
console.info(`PullApprove verification failed.\n`);
console.info(`Please update '.pullapprove.yml' to ensure that all necessary`);
console.info(`files/directories have owners and all patterns that appear in`);
console.info(`the file correspond to actual files/directories in the repo.`);
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.`);
}
/**
* File by file Summary
*/
logHeader('PullApprove results by file');
console.groupCollapsed(`Matched Files (${matchedFiles.length} files)`);
verbose && matchedFiles.forEach(file => console.info(file));
console.groupEnd();
console.groupCollapsed(`Unmatched Files (${unmatchedFiles.length} files)`);
unmatchedFiles.forEach(file => console.info(file));
console.groupEnd();
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();
/**
* Group by group Summary
*/
logHeader('PullApprove results by group');
console.groupCollapsed(`Groups skipped (${groupsSkipped.length} groups)`);
verbose && groupsSkipped.forEach(group => console.info(`${group.groupName}`));
console.groupEnd();
info.group(`Groups skipped (${groupsSkipped.length} groups)`);
verbose && groupsSkipped.forEach(group => info(`${group.groupName}`));
info.groupEnd();
const matchedGroups = resultsByGroup.filter(group => !group.unmatchedCount);
console.groupCollapsed(`Matched conditions by Group (${matchedGroups.length} groups)`);
info.group(`Matched conditions by Group (${matchedGroups.length} groups)`);
verbose && matchedGroups.forEach(group => logGroup(group));
console.groupEnd();
info.groupEnd();
const unmatchedGroups = resultsByGroup.filter(group => group.unmatchedCount);
console.groupCollapsed(`Unmatched conditions by Group (${unmatchedGroups.length} groups)`);
info.group(`Unmatched conditions by Group (${unmatchedGroups.length} groups)`);
unmatchedGroups.forEach(group => logGroup(group, false));
console.groupEnd();
info.groupEnd();
// Provide correct exit code based on verification success.
process.exit(verificationSucceeded ? 0 : 1);