Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev-infra): handle excluding files via globs in pullapprove #36162

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions dev-infra/pullapprove/BUILD.bazel
Expand Up @@ -3,11 +3,15 @@ load("@npm_bazel_typescript//:index.bzl", "ts_library")
ts_library(
name = "pullapprove",
srcs = [
"group.ts",
"logging.ts",
"parse-yaml.ts",
"verify.ts",
],
module_name = "@angular/dev-infra-private/pullapprove",
visibility = ["//dev-infra:__subpackages__"],
deps = [
"//dev-infra/utils:config",
"@npm//@types/minimatch",
"@npm//@types/node",
"@npm//@types/shelljs",
Expand Down
158 changes: 158 additions & 0 deletions dev-infra/pullapprove/group.ts
@@ -0,0 +1,158 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 {IMinimatch, Minimatch, match} from 'minimatch';

import {PullApproveGroupConfig} from './parse-yaml';

/** A condition for a group. */
interface GroupCondition {
glob: string;
matcher: IMinimatch;
matchedFiles: Set<string>;
}

/** Result of testing files against the group. */
export interface PullApproveGroupResult {
groupName: string;
matchedIncludes: GroupCondition[];
matchedExcludes: GroupCondition[];
matchedCount: number;
unmatchedIncludes: GroupCondition[];
unmatchedExcludes: GroupCondition[];
unmatchedCount: number;
}

// Regex Matcher for contains_any_globs conditions
const CONTAINS_ANY_GLOBS_REGEX = /^'([^']+)',?$/;

const CONDITION_TYPES = {
INCLUDE_GLOBS: /^contains_any_globs/,
EXCLUDE_GLOBS: /^not contains_any_globs/,
ATTR_LENGTH: /^len\(.*\)/,
};

/** A PullApprove group to be able to test files against. */
export class PullApproveGroup {
// Lines which were not able to be parsed as expected.
private misconfiguredLines: string[] = [];
// Conditions for the group for including files.
private includeConditions: GroupCondition[] = [];
// Conditions for the group for excluding files.
private excludeConditions: GroupCondition[] = [];
// Whether the group has file matchers.
public hasMatchers = false;

constructor(public groupName: string, group: PullApproveGroupConfig) {
for (let condition of group.conditions) {
condition = condition.trim();

if (condition.match(CONDITION_TYPES.INCLUDE_GLOBS)) {
const [conditions, misconfiguredLines] = getLinesForContainsAnyGlobs(condition);
conditions.forEach(globString => this.includeConditions.push({
glob: globString,
matcher: new Minimatch(globString, {dot: true}),
matchedFiles: new Set<string>(),
}));
this.misconfiguredLines.push(...misconfiguredLines);
this.hasMatchers = true;
} else if (condition.match(CONDITION_TYPES.EXCLUDE_GLOBS)) {
const [conditions, misconfiguredLines] = getLinesForContainsAnyGlobs(condition);
conditions.forEach(globString => this.excludeConditions.push({
glob: globString,
matcher: new Minimatch(globString, {dot: true}),
matchedFiles: new Set<string>(),
}));
this.misconfiguredLines.push(...misconfiguredLines);
this.hasMatchers = true;
} else if (condition.match(CONDITION_TYPES.ATTR_LENGTH)) {
// Currently a noop as we do not take any action on this condition type.
} else {
const errMessage = `Unrecognized condition found, unable to parse the following condition: \n\n` +
`From the [${groupName}] group:\n` +
` - ${condition}` +
`\n\n` +
`Known condition regexs:\n` +
`${Object.entries(CONDITION_TYPES).map(([k, v]) => ` ${k} - ${v}`).join('\n')}` +
`\n\n`;
console.error(errMessage);
process.exit(1);
}
}
}

/** Retrieve all of the lines which were not able to be parsed. */
getBadLines(): string[] { return this.misconfiguredLines; }

/** Retrieve the results for the Group, all matched and unmatched conditions. */
getResults(): PullApproveGroupResult {
const matchedIncludes = this.includeConditions.filter(c => !!c.matchedFiles.size);
const matchedExcludes = this.excludeConditions.filter(c => !!c.matchedFiles.size);
const unmatchedIncludes = this.includeConditions.filter(c => !c.matchedFiles.size);
const unmatchedExcludes = this.excludeConditions.filter(c => !c.matchedFiles.size);
const unmatchedCount = unmatchedIncludes.length + unmatchedExcludes.length;
const matchedCount = matchedIncludes.length + matchedExcludes.length;
return {
matchedIncludes,
matchedExcludes,
matchedCount,
unmatchedIncludes,
unmatchedExcludes,
unmatchedCount,
groupName: this.groupName,
};
}

/**
* Tests a provided file path to determine if it would be considered matched by
* the pull approve group's conditions.
*/
testFile(file: string) {
let matched = false;
this.includeConditions.forEach((includeCondition: GroupCondition) => {
if (includeCondition.matcher.match(file)) {
let matchedExclude = false;
this.excludeConditions.forEach((excludeCondition: GroupCondition) => {
if (excludeCondition.matcher.match(file)) {
// Add file as a discovered exclude as it is negating a matched
// include condition.
excludeCondition.matchedFiles.add(file);
matchedExclude = true;
}
});
// An include condition is only considered matched if no exclude
// conditions are found to matched the file.
if (!matchedExclude) {
includeCondition.matchedFiles.add(file);
matched = true;
}
}
});
return matched;
}
}

/**
* Extract all of the individual globs from a group condition,
* providing both the valid and invalid lines.
*/
function getLinesForContainsAnyGlobs(lines: string) {
const invalidLines: string[] = [];
const validLines = lines.split('\n')
.slice(1, -1)
.map((glob: string) => {
const trimmedGlob = glob.trim();
const match = trimmedGlob.match(CONTAINS_ANY_GLOBS_REGEX);
if (!match) {
invalidLines.push(trimmedGlob);
return '';
}
return match[1];
})
.filter(globString => !!globString);
return [validLines, invalidLines];
}
42 changes: 42 additions & 0 deletions dev-infra/pullapprove/logging.ts
@@ -0,0 +1,42 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 {PullApproveGroupResult} from './group';

/** Create logs for each pullapprove group result. */
export function logGroup(group: PullApproveGroupResult, matched = true) {
const includeConditions = matched ? group.matchedIncludes : group.unmatchedIncludes;
const excludeConditions = matched ? group.matchedExcludes : group.unmatchedExcludes;
console.groupCollapsed(`[${group.groupName}]`);
if (includeConditions.length) {
console.group('includes');
includeConditions.forEach(
matcher => console.info(`${matcher.glob} - ${matcher.matchedFiles.size}`));
console.groupEnd();
}
if (excludeConditions.length) {
console.group('excludes');
excludeConditions.forEach(
matcher => console.info(`${matcher.glob} - ${matcher.matchedFiles.size}`));
console.groupEnd();
}
console.groupEnd();
}

/** Logs a header within a text drawn box. */
export function logHeader(...params: string[]) {
const totalWidth = 80;
const fillWidth = totalWidth - 2;
const headerText = params.join(' ').substr(0, fillWidth);
const leftSpace = Math.ceil((fillWidth - headerText.length) / 2);
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, '─')}┘`);
}
33 changes: 33 additions & 0 deletions dev-infra/pullapprove/parse-yaml.ts
@@ -0,0 +1,33 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 {parse as parseYaml} from 'yaml';

export interface PullApproveGroupConfig {
conditions: string;
reviewers: {
users: string[],
teams: string[],
};
}

export interface PullApproveConfig {
version: number;
github_api_version?: string;
pullapprove_conditions?: {
condition: string,
unmet_status: string,
explanation: string,
}[];
groups: {
[key: string]: PullApproveGroupConfig,
};
}

export function parsePullApproveYaml(rawYaml: string): PullApproveConfig {
return parseYaml(rawYaml) as PullApproveConfig;
}