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

Chen/Codeowners #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/fixtures/only_pdfs.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
touched-a-pdf-file:
- any: ['*.pdf']
- any: ['@actions/actions-runtime']
17 changes: 17 additions & 0 deletions src/getCodeOwnersFromPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Codeowners from 'codeowners'

export async function getCodeOwnersFromPaths(
paths: string[]
): Promise<string[]> {
const repos = new Codeowners();
const owners: Set<string> = new Set();
core.debug("fetching codeowner");
for (const path of paths) {
const pathowners = repos.getOwner(path);
for (const pathowner of pathowners) {
owners.add(pathowner);
core.debug(`found codeowner: ${pathowner}`);
}
}
return Array.from(owners);
}
18 changes: 11 additions & 7 deletions src/labeler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import * as yaml from "js-yaml";
import {getCodeOwnersFromPaths} from './getCodeOwnersFromPaths'
import { Minimatch, IMinimatch } from "minimatch";

interface MatchConfig {
Expand Down Expand Up @@ -31,8 +32,10 @@ export async function run() {
pull_number: prNumber,
});

core.debug(`fetching changed files for pr #${prNumber}`);
core.debug(`fetching changed files for PR #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber);
core.debug("fetching codeowners");
const owners: string[] = await getCodeOwnersFromPaths(changedFiles);
const labelGlobs: Map<string, StringOrMatchConfig[]> = await getLabelGlobs(
client,
configPath
Expand All @@ -42,7 +45,7 @@ export async function run() {
const labelsToRemove: string[] = [];
for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) {
if (checkGlobs(owners, globs)) {
labels.push(label);
} else if (pullRequest.labels.find((l) => l.name === label)) {
labelsToRemove.push(label);
Expand Down Expand Up @@ -156,13 +159,13 @@ function printPattern(matcher: IMinimatch): string {
}

export function checkGlobs(
changedFiles: string[],
codeowners: string[],
globs: StringOrMatchConfig[]
): boolean {
for (const glob of globs) {
core.debug(` checking pattern ${JSON.stringify(glob)}`);
const matchConfig = toMatchConfig(glob);
if (checkMatch(changedFiles, matchConfig)) {
if (checkMatch(codeowners, matchConfig)) {
return true;
}
}
Expand All @@ -188,6 +191,7 @@ function checkAny(changedFiles: string[], globs: string[]): boolean {
const matchers = globs.map((g) => new Minimatch(g));
core.debug(` checking "any" patterns`);
for (const changedFile of changedFiles) {
core.debug(` checking ${changedFile}`)
if (isMatch(changedFile, matchers)) {
core.debug(` "any" patterns matched against ${changedFile}`);
return true;
Expand All @@ -213,15 +217,15 @@ function checkAll(changedFiles: string[], globs: string[]): boolean {
return true;
}

function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
function checkMatch(codeowners: string[], matchConfig: MatchConfig): boolean {
if (matchConfig.all !== undefined) {
if (!checkAll(changedFiles, matchConfig.all)) {
if (!checkAll(codeowners, matchConfig.all)) {
return false;
}
}

if (matchConfig.any !== undefined) {
if (!checkAny(changedFiles, matchConfig.any)) {
if (!checkAny(codeowners, matchConfig.any)) {
return false;
}
}
Expand Down