From 969899da68c826b00b7a9ebce80e7f2a7487d6c4 Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Sun, 29 Jan 2023 14:10:07 -0500 Subject: [PATCH] Add the changedFiles key and update logic in labeler --- __tests__/fixtures/only_pdfs.yml | 3 +- __tests__/labeler.test.ts | 2 +- src/branch.ts | 2 +- src/labeler.ts | 66 +++++++++++++++++++++++--------- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/__tests__/fixtures/only_pdfs.yml b/__tests__/fixtures/only_pdfs.yml index 1bcce76a..0f4b61a7 100644 --- a/__tests__/fixtures/only_pdfs.yml +++ b/__tests__/fixtures/only_pdfs.yml @@ -1,2 +1,3 @@ touched-a-pdf-file: - - any: ['*.pdf'] + - changed-files: + - any: ['*.pdf'] diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index d684d28b..7ec72b51 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -10,7 +10,7 @@ beforeAll(() => { }); }); -const matchConfig = [{any: ['*.txt']}]; +const matchConfig = [{changedFiles: {any: ['*.txt']}}]; describe('checkGlobs', () => { it('returns true when our pattern does match changed files', () => { diff --git a/src/branch.ts b/src/branch.ts index 0a39a1d3..1804a37d 100644 --- a/src/branch.ts +++ b/src/branch.ts @@ -10,7 +10,7 @@ type BranchBase = 'base' | 'head'; export function toBranchMatchConfig(config: any): BranchMatchConfig { if (!config['head-branch'] || config['base-branch']) { - return config; + return {}; } const branchConfig = { diff --git a/src/labeler.ts b/src/labeler.ts index 919571fa..551a2203 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -5,18 +5,15 @@ import {Minimatch} from 'minimatch'; import {checkBranch, toBranchMatchConfig, BranchMatchConfig} from './branch'; -interface FilesChangedMatchConfig { - all?: string[]; - any?: string[]; - filesChanged?: { +interface ChangedFilesMatchConfig { + changedFiles?: { all?: string[]; any?: string[]; }; } -type MatchConfig = FilesChangedMatchConfig & BranchMatchConfig; +export type MatchConfig = ChangedFilesMatchConfig & BranchMatchConfig; -type StringOrMatchConfig = string | MatchConfig; type ClientType = ReturnType; export async function run() { @@ -41,7 +38,7 @@ export async function run() { core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles: string[] = await getChangedFiles(client, prNumber); - const labelGlobs: Map = await getLabelGlobs( + const labelGlobs: Map = await getLabelGlobs( client, configPath ); @@ -103,7 +100,7 @@ async function getChangedFiles( async function getLabelGlobs( client: ClientType, configurationPath: string -): Promise> { +): Promise> { const configurationContent: string = await fetchContent( client, configurationPath @@ -132,8 +129,8 @@ async function fetchContent( function getLabelGlobMapFromObject( configObject: any -): Map { - const labelGlobs: Map = new Map(); +): Map { + const labelGlobs: Map = new Map(); for (const label in configObject) { if (typeof configObject[label] === 'string') { labelGlobs.set(label, [configObject[label]]); @@ -149,17 +146,48 @@ function getLabelGlobMapFromObject( return labelGlobs; } -function toMatchConfig(config: StringOrMatchConfig): MatchConfig { - if (typeof config === 'string') { +function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig { + if (!config['changed-files']) { + return {}; + } + const changedFiles = config['changed-files']; + + // If the value provided is a string or an array of strings then default to `any` matching + if (typeof changedFiles === 'string') { return { - any: [config] + changedFiles: { + any: [changedFiles] + } }; } + const changedFilesMatchConfig = { + changedFiles: {} + }; + + if (Array.isArray(changedFiles)) { + if (changedFiles.every(entry => typeof entry === 'string')) { + changedFilesMatchConfig.changedFiles = { + any: changedFiles + }; + } else { + // If it is not an array of strings then it should be array of further config options + // so assign them to our `changedFilesMatchConfig` + changedFiles.forEach(element => { + Object.assign(changedFilesMatchConfig.changedFiles, element); + }); + } + } + + return changedFilesMatchConfig; +} + +function toMatchConfig(config: MatchConfig): MatchConfig { + const changedFilesConfig = toChangedFilesMatchConfig(config); const branchConfig = toBranchMatchConfig(config); return { - ...config, + ...changedFilesConfig, ...branchConfig }; } @@ -170,7 +198,7 @@ function printPattern(matcher: Minimatch): string { export function checkGlobs( changedFiles: string[], - globs: StringOrMatchConfig[] + globs: MatchConfig[] ): boolean { for (const glob of globs) { core.debug(` checking pattern ${JSON.stringify(glob)}`); @@ -227,14 +255,14 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { } function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean { - if (matchConfig.all !== undefined) { - if (!checkAll(changedFiles, matchConfig.all)) { + if (matchConfig.changedFiles?.all !== undefined) { + if (!checkAll(changedFiles, matchConfig.changedFiles.all)) { return false; } } - if (matchConfig.any !== undefined) { - if (!checkAny(changedFiles, matchConfig.any)) { + if (matchConfig.changedFiles?.any !== undefined) { + if (!checkAny(changedFiles, matchConfig.changedFiles.any)) { return false; } }