mirror of
https://github.com/dorny/paths-filter.git
synced 2025-06-08 10:01:34 +00:00
feat: add config parameter for predicate quantifier
Setting the new 'predicate-quantifier' configuration parameter to 'every' makes it so that all the patterns have to match a file for it to be considered changed. This can be leveraged to ensure that you only build & test software changes that have real impact on the behavior of the code, e.g. you can set up your build to run when Typescript/Rust/etc. files are changed but markdown changes in the diff will be ignored and you consume less resources to build. The default behavior does not change by the introduction of this feature so upgrading can be done safely knowing that existing workflows will not break. Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
This commit is contained in:
parent
ebc4d7e9eb
commit
f90d5265d6
4 changed files with 149 additions and 7 deletions
|
@ -23,6 +23,48 @@ interface FilterRuleItem {
|
|||
isMatch: (str: string) => boolean // Matches the filename
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates the possible logic quantifiers that can be used when determining
|
||||
* if a file is a match or not with multiple patterns.
|
||||
*
|
||||
* The YAML configuration property that is parsed into one of these values is
|
||||
* 'predicate-quantifier' on the top level of the configuration object of the
|
||||
* action.
|
||||
*
|
||||
* The default is to use 'some' which used to be the hardcoded behavior prior to
|
||||
* the introduction of the new mechanism.
|
||||
*
|
||||
* @see https://en.wikipedia.org/wiki/Quantifier_(logic)
|
||||
*/
|
||||
export enum PredicateQuantifier {
|
||||
/**
|
||||
* When choosing 'every' in the config it means that files will only get matched
|
||||
* if all the patterns are satisfied by the path of the file, not just at least one of them.
|
||||
*/
|
||||
EVERY = 'every',
|
||||
/**
|
||||
* When choosing 'some' in the config it means that files will get matched as long as there is
|
||||
* at least one pattern that matches them. This is the default behavior if you don't
|
||||
* specify anything as a predicate quantifier.
|
||||
*/
|
||||
SOME = 'some'
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to define customizations for how the file filtering should work at runtime.
|
||||
*/
|
||||
export type FilterConfig = {readonly predicateQuantifier: PredicateQuantifier}
|
||||
|
||||
/**
|
||||
* An array of strings (at runtime) that contains the valid/accepted values for
|
||||
* the configuration parameter 'predicate-quantifier'.
|
||||
*/
|
||||
export const SUPPORTED_PREDICATE_QUANTIFIERS = Object.values(PredicateQuantifier)
|
||||
|
||||
export function isPredicateQuantifier(x: unknown): x is PredicateQuantifier {
|
||||
return SUPPORTED_PREDICATE_QUANTIFIERS.includes(x as PredicateQuantifier)
|
||||
}
|
||||
|
||||
export interface FilterResults {
|
||||
[key: string]: File[]
|
||||
}
|
||||
|
@ -31,7 +73,7 @@ export class Filter {
|
|||
rules: {[key: string]: FilterRuleItem[]} = {}
|
||||
|
||||
// Creates instance of Filter and load rules from YAML if it's provided
|
||||
constructor(yaml?: string) {
|
||||
constructor(yaml?: string, public readonly filterConfig?: FilterConfig) {
|
||||
if (yaml) {
|
||||
this.load(yaml)
|
||||
}
|
||||
|
@ -62,9 +104,14 @@ export class Filter {
|
|||
}
|
||||
|
||||
private isMatch(file: File, patterns: FilterRuleItem[]): boolean {
|
||||
return patterns.some(
|
||||
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.isMatch(file.filename)
|
||||
)
|
||||
const aPredicate = (rule: Readonly<FilterRuleItem>) => {
|
||||
return (rule.status === undefined || rule.status.includes(file.status)) && rule.isMatch(file.filename)
|
||||
}
|
||||
if (this.filterConfig?.predicateQuantifier === 'every') {
|
||||
return patterns.every(aPredicate)
|
||||
} else {
|
||||
return patterns.some(aPredicate)
|
||||
}
|
||||
}
|
||||
|
||||
private parseFilterItemYaml(item: FilterItemYaml): FilterRuleItem[] {
|
||||
|
|
20
src/main.ts
20
src/main.ts
|
@ -4,7 +4,14 @@ import * as github from '@actions/github'
|
|||
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
|
||||
import {PushEvent, PullRequestEvent} from '@octokit/webhooks-types'
|
||||
|
||||
import {Filter, FilterResults} from './filter'
|
||||
import {
|
||||
isPredicateQuantifier,
|
||||
Filter,
|
||||
FilterConfig,
|
||||
FilterResults,
|
||||
PredicateQuantifier,
|
||||
SUPPORTED_PREDICATE_QUANTIFIERS
|
||||
} from './filter'
|
||||
import {File, ChangeStatus} from './file'
|
||||
import * as git from './git'
|
||||
import {backslashEscape, shellEscape} from './list-format/shell-escape'
|
||||
|
@ -26,13 +33,22 @@ async function run(): Promise<void> {
|
|||
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput
|
||||
const listFiles = core.getInput('list-files', {required: false}).toLowerCase() || 'none'
|
||||
const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', {required: false})) || 10
|
||||
const predicateQuantifier = core.getInput('predicate-quantifier', {required: false}) || PredicateQuantifier.SOME
|
||||
|
||||
if (!isExportFormat(listFiles)) {
|
||||
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`)
|
||||
return
|
||||
}
|
||||
|
||||
const filter = new Filter(filtersYaml)
|
||||
if (!isPredicateQuantifier(predicateQuantifier)) {
|
||||
const predicateQuantifierInvalidErrorMsg =
|
||||
`Input parameter 'predicate-quantifier' is set to invalid value ` +
|
||||
`'${predicateQuantifier}'. Valid values: ${SUPPORTED_PREDICATE_QUANTIFIERS.join(', ')}`
|
||||
throw new Error(predicateQuantifierInvalidErrorMsg)
|
||||
}
|
||||
const filterConfig: FilterConfig = {predicateQuantifier}
|
||||
|
||||
const filter = new Filter(filtersYaml, filterConfig)
|
||||
const files = await getChangedFiles(token, base, ref, initialFetchDepth)
|
||||
core.info(`Detected ${files.length} changed files`)
|
||||
const results = filter.match(files)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue