Add any and all functions for both checks

This commit is contained in:
Josh Dales
2023-03-24 21:44:26 -04:00
parent a9e07ce8ff
commit 3bec9227d1
5 changed files with 73 additions and 37 deletions

View File

@@ -1,6 +1,6 @@
import { import {
getBranchName, getBranchName,
checkBranch, checkAnyBranch,
toBranchMatchConfig, toBranchMatchConfig,
BranchMatchConfig BranchMatchConfig
} from '../src/branch'; } from '../src/branch';
@@ -25,7 +25,7 @@ describe('getBranchName', () => {
}); });
}); });
describe('checkBranch', () => { describe('checkAnyBranch', () => {
beforeEach(() => { beforeEach(() => {
github.context.payload.pull_request!.head = { github.context.payload.pull_request!.head = {
ref: 'test/feature/123' ref: 'test/feature/123'
@@ -38,14 +38,14 @@ describe('checkBranch', () => {
describe('when a single pattern is provided', () => { describe('when a single pattern is provided', () => {
describe('and the pattern matches the head branch', () => { describe('and the pattern matches the head branch', () => {
it('returns true', () => { it('returns true', () => {
const result = checkBranch(['^test'], 'head'); const result = checkAnyBranch(['^test'], 'head');
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('and the pattern does not match the head branch', () => { describe('and the pattern does not match the head branch', () => {
it('returns false', () => { it('returns false', () => {
const result = checkBranch(['^feature/'], 'head'); const result = checkAnyBranch(['^feature/'], 'head');
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });
@@ -54,21 +54,21 @@ describe('checkBranch', () => {
describe('when multiple patterns are provided', () => { describe('when multiple patterns are provided', () => {
describe('and at least one pattern matches', () => { describe('and at least one pattern matches', () => {
it('returns true', () => { it('returns true', () => {
const result = checkBranch(['^test/', '^feature/'], 'head'); const result = checkAnyBranch(['^test/', '^feature/'], 'head');
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('and all patterns match', () => { describe('and all patterns match', () => {
it('returns true', () => { it('returns true', () => {
const result = checkBranch(['^test/', '/feature/'], 'head'); const result = checkAnyBranch(['^test/', '/feature/'], 'head');
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
describe('and no patterns match', () => { describe('and no patterns match', () => {
it('returns false', () => { it('returns false', () => {
const result = checkBranch(['^feature/', '/test$'], 'head'); const result = checkAnyBranch(['^feature/', '/test$'], 'head');
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });
@@ -77,7 +77,7 @@ describe('checkBranch', () => {
describe('when the branch to check is specified as the base branch', () => { describe('when the branch to check is specified as the base branch', () => {
describe('and the pattern matches the base branch', () => { describe('and the pattern matches the base branch', () => {
it('returns true', () => { it('returns true', () => {
const result = checkBranch(['^main$'], 'base'); const result = checkAnyBranch(['^main$'], 'base');
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });

View File

@@ -1,21 +1,21 @@
import { import {
ChangedFilesMatchConfig, ChangedFilesMatchConfig,
checkAll, checkAllChangedFiles,
checkAny, checkAnyChangedFiles,
toChangedFilesMatchConfig toChangedFilesMatchConfig
} from '../src/changedFiles'; } from '../src/changedFiles';
jest.mock('@actions/core'); jest.mock('@actions/core');
jest.mock('@actions/github'); jest.mock('@actions/github');
describe('checkAll', () => { describe('checkAllChangedFiles', () => {
const changedFiles = ['foo.txt', 'bar.txt']; const changedFiles = ['foo.txt', 'bar.txt'];
describe('when the globs match every file that has changed', () => { describe('when the globs match every file that has changed', () => {
const globs = ['*.txt']; const globs = ['*.txt'];
it('returns true', () => { it('returns true', () => {
const result = checkAll(changedFiles, globs); const result = checkAllChangedFiles(changedFiles, globs);
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
@@ -24,20 +24,20 @@ describe('checkAll', () => {
const globs = ['foo.txt']; const globs = ['foo.txt'];
it('returns false', () => { it('returns false', () => {
const result = checkAll(changedFiles, globs); const result = checkAllChangedFiles(changedFiles, globs);
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });
}); });
describe('checkAny', () => { describe('checkAnyChangedFiles', () => {
const changedFiles = ['foo.txt', 'bar.txt']; const changedFiles = ['foo.txt', 'bar.txt'];
describe('when the globs match any of the files that have changed', () => { describe('when the globs match any of the files that have changed', () => {
const globs = ['foo.txt']; const globs = ['foo.txt'];
it('returns true', () => { it('returns true', () => {
const result = checkAny(changedFiles, globs); const result = checkAnyChangedFiles(changedFiles, globs);
expect(result).toBe(true); expect(result).toBe(true);
}); });
}); });
@@ -46,7 +46,7 @@ describe('checkAny', () => {
const globs = ['*.md']; const globs = ['*.md'];
it('returns false', () => { it('returns false', () => {
const result = checkAny(changedFiles, globs); const result = checkAnyChangedFiles(changedFiles, globs);
expect(result).toBe(false); expect(result).toBe(false);
}); });
}); });

View File

@@ -40,7 +40,7 @@ export function getBranchName(branchBase: BranchBase): string | undefined {
} }
} }
export function checkBranch( export function checkAnyBranch(
regexps: string[], regexps: string[],
branchBase: BranchBase branchBase: BranchBase
): boolean { ): boolean {
@@ -63,6 +63,29 @@ export function checkBranch(
return false; return false;
} }
export function checkAllBranch(
regexps: string[],
branchBase: BranchBase
): boolean {
const branchName = getBranchName(branchBase);
if (!branchName) {
core.debug(` no branch name`);
return false;
}
core.debug(` checking "branch" pattern against ${branchName}`);
const matchers = regexps.map(regexp => new RegExp(regexp));
for (const matcher of matchers) {
if (!matchBranchPattern(matcher, branchName)) {
core.debug(` "branch" patterns matched against ${branchName}`);
return false;
}
}
core.debug(` "branch" patterns did not match against ${branchName}`);
return true;
}
function matchBranchPattern(matcher: RegExp, branchName: string): boolean { function matchBranchPattern(matcher: RegExp, branchName: string): boolean {
core.debug(` - ${matcher}`); core.debug(` - ${matcher}`);
if (matcher.test(branchName)) { if (matcher.test(branchName)) {

View File

@@ -64,7 +64,10 @@ function isMatch(changedFile: string, matchers: Minimatch[]): boolean {
} }
// equivalent to "Array.some()" but expanded for debugging and clarity // equivalent to "Array.some()" but expanded for debugging and clarity
export function checkAny(changedFiles: string[], globs: string[]): boolean { export function checkAnyChangedFiles(
changedFiles: string[],
globs: string[]
): boolean {
const matchers = globs.map(g => new Minimatch(g)); const matchers = globs.map(g => new Minimatch(g));
core.debug(` checking "any" patterns`); core.debug(` checking "any" patterns`);
for (const changedFile of changedFiles) { for (const changedFile of changedFiles) {
@@ -79,7 +82,10 @@ export function checkAny(changedFiles: string[], globs: string[]): boolean {
} }
// equivalent to "Array.every()" but expanded for debugging and clarity // equivalent to "Array.every()" but expanded for debugging and clarity
export function checkAll(changedFiles: string[], globs: string[]): boolean { export function checkAllChangedFiles(
changedFiles: string[],
globs: string[]
): boolean {
const matchers = globs.map(g => new Minimatch(g)); const matchers = globs.map(g => new Minimatch(g));
core.debug(` checking "all" patterns`); core.debug(` checking "all" patterns`);
for (const changedFile of changedFiles) { for (const changedFile of changedFiles) {

View File

@@ -5,9 +5,16 @@ import * as yaml from 'js-yaml';
import { import {
ChangedFilesMatchConfig, ChangedFilesMatchConfig,
getChangedFiles, getChangedFiles,
toChangedFilesMatchConfig toChangedFilesMatchConfig,
checkAllChangedFiles,
checkAnyChangedFiles
} from './changedFiles'; } from './changedFiles';
import {checkBranch, toBranchMatchConfig, BranchMatchConfig} from './branch'; import {
checkAnyBranch,
checkAllBranch,
toBranchMatchConfig,
BranchMatchConfig
} from './branch';
export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig; export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig;
@@ -215,24 +222,24 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
// equivalent to "Array.some()" but expanded for debugging and clarity // equivalent to "Array.some()" but expanded for debugging and clarity
export function checkAny( export function checkAny(
matchConfigs: BaseMatchConfig[], matchConfigs: BaseMatchConfig[],
_changedFiles: string[] changedFiles: string[]
): boolean { ): boolean {
core.debug(` checking "any" patterns`); core.debug(` checking "any" patterns`);
for (const matchConfig of matchConfigs) { for (const matchConfig of matchConfigs) {
if (matchConfig.baseBranch) { if (matchConfig.baseBranch) {
if (checkBranch(matchConfig.baseBranch, 'base')) { if (checkAnyBranch(matchConfig.baseBranch, 'base')) {
return true; return true;
} }
} }
// if (matchConfig.changedFiles) { if (matchConfig.changedFiles) {
// if (checkFiles(matchConfig.changedFiles, changedFiles)) { if (checkAnyChangedFiles(changedFiles, matchConfig.changedFiles)) {
// return true; return true;
// } }
// } }
if (matchConfig.headBranch) { if (matchConfig.headBranch) {
if (checkBranch(matchConfig.headBranch, 'head')) { if (checkAnyBranch(matchConfig.headBranch, 'head')) {
return true; return true;
} }
} }
@@ -245,24 +252,24 @@ export function checkAny(
// equivalent to "Array.every()" but expanded for debugging and clarity // equivalent to "Array.every()" but expanded for debugging and clarity
export function checkAll( export function checkAll(
matchConfigs: BaseMatchConfig[], matchConfigs: BaseMatchConfig[],
_changedFiles: string[] changedFiles: string[]
): boolean { ): boolean {
core.debug(` checking "all" patterns`); core.debug(` checking "all" patterns`);
for (const matchConfig of matchConfigs) { for (const matchConfig of matchConfigs) {
if (matchConfig.baseBranch) { if (matchConfig.baseBranch) {
if (!checkBranch(matchConfig.baseBranch, 'base')) { if (!checkAllBranch(matchConfig.baseBranch, 'base')) {
return false; return false;
} }
} }
// if (matchConfig.changedFiles) { if (matchConfig.changedFiles) {
// if (checkFiles(matchConfig.changedFiles, changedFiles)) { if (checkAllChangedFiles(changedFiles, matchConfig.changedFiles)) {
// return true; return true;
// } }
// } }
if (matchConfig.headBranch) { if (matchConfig.headBranch) {
if (!checkBranch(matchConfig.headBranch, 'head')) { if (!checkAllBranch(matchConfig.headBranch, 'head')) {
return false; return false;
} }
} }