From e4aed390c0f69d130be5a35cf63bbab729c308dd Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Fri, 11 Nov 2022 15:28:47 -0800 Subject: [PATCH 1/9] add label based on code owner --- src/getCodeOwnersFromPaths.ts | 15 +++++++++++++++ src/labeler.ts | 14 ++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/getCodeOwnersFromPaths.ts diff --git a/src/getCodeOwnersFromPaths.ts b/src/getCodeOwnersFromPaths.ts new file mode 100644 index 000000000..7d69c377e --- /dev/null +++ b/src/getCodeOwnersFromPaths.ts @@ -0,0 +1,15 @@ +import Codeowners from 'codeowners' + +export async function getCodeOwnersFromPaths( + paths: string[] +): Promise { + const repos = new Codeowners() + const owners: Set = new Set() + for (const path of paths) { + const pathowners = repos.getOwner(path) + for (const pathowner of pathowners) { + owners.add(pathowner) + } + } + return Array.from(owners); +} \ No newline at end of file diff --git a/src/labeler.ts b/src/labeler.ts index 59cf23f32..2c9c4c457 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -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 { @@ -33,6 +34,7 @@ export async function run() { core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles: string[] = await getChangedFiles(client, prNumber); + const owners: string[] = await getCodeOwnersFromPaths(changedFiles) const labelGlobs: Map = await getLabelGlobs( client, configPath @@ -42,7 +44,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); @@ -156,13 +158,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; } } @@ -213,15 +215,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; } } From 78dfee2b69bf2cdb8196cde584f844c56317123e Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 14:25:00 -0800 Subject: [PATCH 2/9] add log --- src/labeler.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/labeler.ts b/src/labeler.ts index 2c9c4c457..930a5e087 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -35,6 +35,8 @@ export async function run() { core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles: string[] = await getChangedFiles(client, prNumber); const owners: string[] = await getCodeOwnersFromPaths(changedFiles) + const ownersStr = owners.toString() + core.debug(`found all codeowners: ${ownersStr}`) const labelGlobs: Map = await getLabelGlobs( client, configPath From 81b2a05bb2f61986a9ede06414c221f7d8c08717 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 14:34:02 -0800 Subject: [PATCH 3/9] fix syntax --- src/labeler.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/labeler.ts b/src/labeler.ts index 930a5e087..20d91c86e 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -34,9 +34,9 @@ export async function run() { core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles: string[] = await getChangedFiles(client, prNumber); - const owners: string[] = await getCodeOwnersFromPaths(changedFiles) - const ownersStr = owners.toString() - core.debug(`found all codeowners: ${ownersStr}`) + const owners: string[] = await getCodeOwnersFromPaths(changedFiles); + const ownersStr = owners.toString(); + core.debug(`found all codeowners: ${ownersStr}`); const labelGlobs: Map = await getLabelGlobs( client, configPath From 015e36ff3efc362612db44218bf649c09c3d9bb5 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 14:47:09 -0800 Subject: [PATCH 4/9] fix log --- src/labeler.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/labeler.ts b/src/labeler.ts index 20d91c86e..c76593cdd 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -34,9 +34,10 @@ export async function run() { 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 ownersStr = owners.toString(); - core.debug(`found all codeowners: ${ownersStr}`); + core.debug("found all codeowners: " + ownersStr); const labelGlobs: Map = await getLabelGlobs( client, configPath @@ -192,6 +193,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; From cf9c69585084eece43c940ac54fc7bdbcad16301 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 14:56:15 -0800 Subject: [PATCH 5/9] fix typo --- src/getCodeOwnersFromPaths.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/getCodeOwnersFromPaths.ts b/src/getCodeOwnersFromPaths.ts index 7d69c377e..6bd975444 100644 --- a/src/getCodeOwnersFromPaths.ts +++ b/src/getCodeOwnersFromPaths.ts @@ -3,12 +3,14 @@ import Codeowners from 'codeowners' export async function getCodeOwnersFromPaths( paths: string[] ): Promise { - const repos = new Codeowners() - const owners: Set = new Set() + const repos = new Codeowners(); + const owners: Set = new Set(); + core.debug("fetching codeowner"); for (const path of paths) { - const pathowners = repos.getOwner(path) + const pathowners = repos.getOwner(path); for (const pathowner of pathowners) { - owners.add(pathowner) + owners.add(pathowner); + core.debug(`found codeowner: ${pathowner}`); } } return Array.from(owners); From 066efb9b19b10595d4d74733ae7d93361faa03b2 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 15:01:42 -0800 Subject: [PATCH 6/9] st --- src/labeler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/labeler.ts b/src/labeler.ts index c76593cdd..7da7ee693 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -32,12 +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 ownersStr = owners.toString(); - core.debug("found all codeowners: " + ownersStr); const labelGlobs: Map = await getLabelGlobs( client, configPath From 73cb10fa2abdd4980516723189cf95b8a108b129 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 16:51:21 -0800 Subject: [PATCH 7/9] update codeowner file --- CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODEOWNERS b/CODEOWNERS index 992d27f09..278adda86 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1 @@ -* @actions/actions-runtime +* @limebike/ios-supply From d2f6637ac391a0596cd73e08f99fd715aeb5ec74 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 16:56:32 -0800 Subject: [PATCH 8/9] update --- __tests__/fixtures/only_pdfs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/fixtures/only_pdfs.yml b/__tests__/fixtures/only_pdfs.yml index 1bcce76ae..f8067e6eb 100644 --- a/__tests__/fixtures/only_pdfs.yml +++ b/__tests__/fixtures/only_pdfs.yml @@ -1,2 +1,2 @@ touched-a-pdf-file: - - any: ['*.pdf'] + - any: ['@limebike/ios-supply'] From 178f087d70e2168fd85f207768f2273d261c7208 Mon Sep 17 00:00:00 2001 From: Chen Zheng Date: Mon, 28 Nov 2022 16:58:49 -0800 Subject: [PATCH 9/9] fix --- CODEOWNERS | 2 +- __tests__/fixtures/only_pdfs.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 278adda86..992d27f09 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1 +1 @@ -* @limebike/ios-supply +* @actions/actions-runtime diff --git a/__tests__/fixtures/only_pdfs.yml b/__tests__/fixtures/only_pdfs.yml index f8067e6eb..9a583a337 100644 --- a/__tests__/fixtures/only_pdfs.yml +++ b/__tests__/fixtures/only_pdfs.yml @@ -1,2 +1,2 @@ touched-a-pdf-file: - - any: ['@limebike/ios-supply'] + - any: ['@actions/actions-runtime']