diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index 9c31ac9e..e320eb7b 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -10,7 +10,8 @@ beforeAll(() => { }); }); -// I have to double cast here as this is what comes from js-yaml looks like which then gets transformed in toMatchConfig +// I have to double cast here as this is what the output from js-yaml looks like which then gets +// transformed in toMatchConfig const matchConfig = [ {'changed-files': [{any: ['*.txt']}]} ] as unknown as MatchConfig[]; diff --git a/dist/index.js b/dist/index.js index 4533491d..c2c74d56 100644 --- a/dist/index.js +++ b/dist/index.js @@ -70,14 +70,14 @@ function getBranchName(branchBase) { } } exports.getBranchName = getBranchName; -function checkBranch(glob, branchBase) { +function checkBranch(regexps, branchBase) { const branchName = getBranchName(branchBase); if (!branchName) { core.debug(` no branch name`); return false; } core.debug(` checking "branch" pattern against ${branchName}`); - const matchers = glob.map(g => new RegExp(g)); + const matchers = regexps.map(regexp => new RegExp(regexp)); for (const matcher of matchers) { if (matchBranchPattern(matcher, branchName)) { core.debug(` "branch" patterns matched against ${branchName}`); @@ -164,12 +164,12 @@ function run() { }); core.debug(`fetching changed files for pr #${prNumber}`); const changedFiles = yield getChangedFiles(client, prNumber); - const labelGlobs = yield getLabelGlobs(client, configPath); + const labelConfigs = yield getMatchConfigs(client, configPath); const labels = []; const labelsToRemove = []; - for (const [label, globs] of labelGlobs.entries()) { + for (const [label, configs] of labelConfigs.entries()) { core.debug(`processing ${label}`); - if (checkGlobs(changedFiles, globs)) { + if (checkGlobs(changedFiles, configs)) { labels.push(label); } else if (pullRequest.labels.find(l => l.name === label)) { @@ -213,13 +213,13 @@ function getChangedFiles(client, prNumber) { return changedFiles; }); } -function getLabelGlobs(client, configurationPath) { +function getMatchConfigs(client, configurationPath) { return __awaiter(this, void 0, void 0, function* () { const configurationContent = yield fetchContent(client, configurationPath); // loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`: const configObject = yaml.load(configurationContent); // transform `any` => `Map` or throw if yaml is malformed: - return getLabelGlobMapFromObject(configObject); + return getLabelConfigMapFromObject(configObject); }); } function fetchContent(client, repoPath) { @@ -233,7 +233,7 @@ function fetchContent(client, repoPath) { return Buffer.from(response.data.content, response.data.encoding).toString(); }); } -function getLabelGlobMapFromObject(configObject) { +function getLabelConfigMapFromObject(configObject) { const labelGlobs = new Map(); for (const label in configObject) { if (typeof configObject[label] === 'string') { @@ -252,28 +252,28 @@ function toChangedFilesMatchConfig(config) { if (!config['changed-files']) { return {}; } - const changedFiles = config['changed-files']; + const changedFilesConfig = config['changed-files']; // If the value provided is a string or an array of strings then default to `any` matching - if (typeof changedFiles === 'string') { + if (typeof changedFilesConfig === 'string') { return { changedFiles: { - any: [changedFiles] + any: [changedFilesConfig] } }; } const changedFilesMatchConfig = { changedFiles: {} }; - if (Array.isArray(changedFiles)) { - if (changedFiles.every(entry => typeof entry === 'string')) { + if (Array.isArray(changedFilesConfig)) { + if (changedFilesConfig.every(entry => typeof entry === 'string')) { changedFilesMatchConfig.changedFiles = { - any: changedFiles + any: changedFilesConfig }; } 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 => { + changedFilesConfig.forEach(element => { Object.assign(changedFilesMatchConfig.changedFiles, element); }); } diff --git a/src/branch.ts b/src/branch.ts index e4edd524..c177d24a 100644 --- a/src/branch.ts +++ b/src/branch.ts @@ -48,7 +48,10 @@ export function getBranchName(branchBase?: BranchBase): string | undefined { } } -export function checkBranch(glob: string[], branchBase?: BranchBase): boolean { +export function checkBranch( + regexps: string[], + branchBase?: BranchBase +): boolean { const branchName = getBranchName(branchBase); if (!branchName) { core.debug(` no branch name`); @@ -56,7 +59,7 @@ export function checkBranch(glob: string[], branchBase?: BranchBase): boolean { } core.debug(` checking "branch" pattern against ${branchName}`); - const matchers = glob.map(g => new RegExp(g)); + const matchers = regexps.map(regexp => new RegExp(regexp)); for (const matcher of matchers) { if (matchBranchPattern(matcher, branchName)) { core.debug(` "branch" patterns matched against ${branchName}`); diff --git a/src/labeler.ts b/src/labeler.ts index 34306cbf..1510d1cd 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -38,16 +38,16 @@ 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 labelConfigs: Map = await getMatchConfigs( client, configPath ); const labels: string[] = []; const labelsToRemove: string[] = []; - for (const [label, globs] of labelGlobs.entries()) { + for (const [label, configs] of labelConfigs.entries()) { core.debug(`processing ${label}`); - if (checkGlobs(changedFiles, globs)) { + if (checkGlobs(changedFiles, configs)) { labels.push(label); } else if (pullRequest.labels.find(l => l.name === label)) { labelsToRemove.push(label); @@ -97,7 +97,7 @@ async function getChangedFiles( return changedFiles; } -async function getLabelGlobs( +async function getMatchConfigs( client: ClientType, configurationPath: string ): Promise> { @@ -110,7 +110,7 @@ async function getLabelGlobs( const configObject: any = yaml.load(configurationContent); // transform `any` => `Map` or throw if yaml is malformed: - return getLabelGlobMapFromObject(configObject); + return getLabelConfigMapFromObject(configObject); } async function fetchContent( @@ -127,7 +127,7 @@ async function fetchContent( return Buffer.from(response.data.content, response.data.encoding).toString(); } -function getLabelGlobMapFromObject( +function getLabelConfigMapFromObject( configObject: any ): Map { const labelGlobs: Map = new Map(); @@ -150,13 +150,13 @@ function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig { if (!config['changed-files']) { return {}; } - const changedFiles = config['changed-files']; + const changedFilesConfig = config['changed-files']; // If the value provided is a string or an array of strings then default to `any` matching - if (typeof changedFiles === 'string') { + if (typeof changedFilesConfig === 'string') { return { changedFiles: { - any: [changedFiles] + any: [changedFilesConfig] } }; } @@ -165,15 +165,15 @@ function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig { changedFiles: {} }; - if (Array.isArray(changedFiles)) { - if (changedFiles.every(entry => typeof entry === 'string')) { + if (Array.isArray(changedFilesConfig)) { + if (changedFilesConfig.every(entry => typeof entry === 'string')) { changedFilesMatchConfig.changedFiles = { - any: changedFiles + any: changedFilesConfig }; } 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 => { + changedFilesConfig.forEach(element => { Object.assign(changedFilesMatchConfig.changedFiles, element); }); }