Rename some functions and variables to match what they are doing

This commit is contained in:
Josh Dales
2023-02-19 16:46:14 -05:00
parent 09f085373a
commit ed31b27f2e
4 changed files with 35 additions and 31 deletions

View File

@@ -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 = [ const matchConfig = [
{'changed-files': [{any: ['*.txt']}]} {'changed-files': [{any: ['*.txt']}]}
] as unknown as MatchConfig[]; ] as unknown as MatchConfig[];

30
dist/index.js vendored
View File

@@ -70,14 +70,14 @@ function getBranchName(branchBase) {
} }
} }
exports.getBranchName = getBranchName; exports.getBranchName = getBranchName;
function checkBranch(glob, branchBase) { function checkBranch(regexps, branchBase) {
const branchName = getBranchName(branchBase); const branchName = getBranchName(branchBase);
if (!branchName) { if (!branchName) {
core.debug(` no branch name`); core.debug(` no branch name`);
return false; return false;
} }
core.debug(` checking "branch" pattern against ${branchName}`); 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) { for (const matcher of matchers) {
if (matchBranchPattern(matcher, branchName)) { if (matchBranchPattern(matcher, branchName)) {
core.debug(` "branch" patterns matched against ${branchName}`); core.debug(` "branch" patterns matched against ${branchName}`);
@@ -164,12 +164,12 @@ function run() {
}); });
core.debug(`fetching changed files for pr #${prNumber}`); core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles = yield getChangedFiles(client, prNumber); const changedFiles = yield getChangedFiles(client, prNumber);
const labelGlobs = yield getLabelGlobs(client, configPath); const labelConfigs = yield getMatchConfigs(client, configPath);
const labels = []; const labels = [];
const labelsToRemove = []; const labelsToRemove = [];
for (const [label, globs] of labelGlobs.entries()) { for (const [label, configs] of labelConfigs.entries()) {
core.debug(`processing ${label}`); core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) { if (checkGlobs(changedFiles, configs)) {
labels.push(label); labels.push(label);
} }
else if (pullRequest.labels.find(l => l.name === label)) { else if (pullRequest.labels.find(l => l.name === label)) {
@@ -213,13 +213,13 @@ function getChangedFiles(client, prNumber) {
return changedFiles; return changedFiles;
}); });
} }
function getLabelGlobs(client, configurationPath) { function getMatchConfigs(client, configurationPath) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const configurationContent = yield fetchContent(client, configurationPath); const configurationContent = yield fetchContent(client, configurationPath);
// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`: // loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
const configObject = yaml.load(configurationContent); const configObject = yaml.load(configurationContent);
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed: // transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
return getLabelGlobMapFromObject(configObject); return getLabelConfigMapFromObject(configObject);
}); });
} }
function fetchContent(client, repoPath) { function fetchContent(client, repoPath) {
@@ -233,7 +233,7 @@ function fetchContent(client, repoPath) {
return Buffer.from(response.data.content, response.data.encoding).toString(); return Buffer.from(response.data.content, response.data.encoding).toString();
}); });
} }
function getLabelGlobMapFromObject(configObject) { function getLabelConfigMapFromObject(configObject) {
const labelGlobs = new Map(); const labelGlobs = new Map();
for (const label in configObject) { for (const label in configObject) {
if (typeof configObject[label] === 'string') { if (typeof configObject[label] === 'string') {
@@ -252,28 +252,28 @@ function toChangedFilesMatchConfig(config) {
if (!config['changed-files']) { if (!config['changed-files']) {
return {}; 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 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 { return {
changedFiles: { changedFiles: {
any: [changedFiles] any: [changedFilesConfig]
} }
}; };
} }
const changedFilesMatchConfig = { const changedFilesMatchConfig = {
changedFiles: {} changedFiles: {}
}; };
if (Array.isArray(changedFiles)) { if (Array.isArray(changedFilesConfig)) {
if (changedFiles.every(entry => typeof entry === 'string')) { if (changedFilesConfig.every(entry => typeof entry === 'string')) {
changedFilesMatchConfig.changedFiles = { changedFilesMatchConfig.changedFiles = {
any: changedFiles any: changedFilesConfig
}; };
} }
else { else {
// If it is not an array of strings then it should be array of further config options // If it is not an array of strings then it should be array of further config options
// so assign them to our `changedFilesMatchConfig` // so assign them to our `changedFilesMatchConfig`
changedFiles.forEach(element => { changedFilesConfig.forEach(element => {
Object.assign(changedFilesMatchConfig.changedFiles, element); Object.assign(changedFilesMatchConfig.changedFiles, element);
}); });
} }

View File

@@ -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); const branchName = getBranchName(branchBase);
if (!branchName) { if (!branchName) {
core.debug(` no branch name`); core.debug(` no branch name`);
@@ -56,7 +59,7 @@ export function checkBranch(glob: string[], branchBase?: BranchBase): boolean {
} }
core.debug(` checking "branch" pattern against ${branchName}`); 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) { for (const matcher of matchers) {
if (matchBranchPattern(matcher, branchName)) { if (matchBranchPattern(matcher, branchName)) {
core.debug(` "branch" patterns matched against ${branchName}`); core.debug(` "branch" patterns matched against ${branchName}`);

View File

@@ -38,16 +38,16 @@ export async function run() {
core.debug(`fetching changed files for pr #${prNumber}`); core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber); const changedFiles: string[] = await getChangedFiles(client, prNumber);
const labelGlobs: Map<string, MatchConfig[]> = await getLabelGlobs( const labelConfigs: Map<string, MatchConfig[]> = await getMatchConfigs(
client, client,
configPath configPath
); );
const labels: string[] = []; const labels: string[] = [];
const labelsToRemove: string[] = []; const labelsToRemove: string[] = [];
for (const [label, globs] of labelGlobs.entries()) { for (const [label, configs] of labelConfigs.entries()) {
core.debug(`processing ${label}`); core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs)) { if (checkGlobs(changedFiles, configs)) {
labels.push(label); labels.push(label);
} else if (pullRequest.labels.find(l => l.name === label)) { } else if (pullRequest.labels.find(l => l.name === label)) {
labelsToRemove.push(label); labelsToRemove.push(label);
@@ -97,7 +97,7 @@ async function getChangedFiles(
return changedFiles; return changedFiles;
} }
async function getLabelGlobs( async function getMatchConfigs(
client: ClientType, client: ClientType,
configurationPath: string configurationPath: string
): Promise<Map<string, MatchConfig[]>> { ): Promise<Map<string, MatchConfig[]>> {
@@ -110,7 +110,7 @@ async function getLabelGlobs(
const configObject: any = yaml.load(configurationContent); const configObject: any = yaml.load(configurationContent);
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed: // transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
return getLabelGlobMapFromObject(configObject); return getLabelConfigMapFromObject(configObject);
} }
async function fetchContent( async function fetchContent(
@@ -127,7 +127,7 @@ async function fetchContent(
return Buffer.from(response.data.content, response.data.encoding).toString(); return Buffer.from(response.data.content, response.data.encoding).toString();
} }
function getLabelGlobMapFromObject( function getLabelConfigMapFromObject(
configObject: any configObject: any
): Map<string, MatchConfig[]> { ): Map<string, MatchConfig[]> {
const labelGlobs: Map<string, MatchConfig[]> = new Map(); const labelGlobs: Map<string, MatchConfig[]> = new Map();
@@ -150,13 +150,13 @@ function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig {
if (!config['changed-files']) { if (!config['changed-files']) {
return {}; 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 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 { return {
changedFiles: { changedFiles: {
any: [changedFiles] any: [changedFilesConfig]
} }
}; };
} }
@@ -165,15 +165,15 @@ function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig {
changedFiles: {} changedFiles: {}
}; };
if (Array.isArray(changedFiles)) { if (Array.isArray(changedFilesConfig)) {
if (changedFiles.every(entry => typeof entry === 'string')) { if (changedFilesConfig.every(entry => typeof entry === 'string')) {
changedFilesMatchConfig.changedFiles = { changedFilesMatchConfig.changedFiles = {
any: changedFiles any: changedFilesConfig
}; };
} else { } else {
// If it is not an array of strings then it should be array of further config options // If it is not an array of strings then it should be array of further config options
// so assign them to our `changedFilesMatchConfig` // so assign them to our `changedFilesMatchConfig`
changedFiles.forEach(element => { changedFilesConfig.forEach(element => {
Object.assign(changedFilesMatchConfig.changedFiles, element); Object.assign(changedFilesMatchConfig.changedFiles, element);
}); });
} }