diff --git a/CHANGELOG.md b/CHANGELOG.md index c0e11e8..e07f106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v4.1.0 +- [Allow base/ref override on pull_request events](https://github.com/dorny/paths-filter/pull/NNN) + ## v4.0.0 - [Update action runtime to node24](https://github.com/dorny/paths-filter/pull/294) diff --git a/README.md b/README.md index a4b1035..359ef8a 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob # introduced by the current branch are considered. # All files are considered as added if there is no common ancestor with # base branch or no previous commit. - # This option is ignored if action is triggered by pull_request event. + # This option is ignored if action is triggered by pull_request event, unless 'allow-override-on-pr' is set to true. # Default: repository default branch (e.g. master) base: '' @@ -126,7 +126,7 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob # but you want to get changes on a different branch. # If this is empty and action is triggered by merge_group event, # the head commit in the event will be used. - # This option is ignored if action is triggered by pull_request event. + # This option is ignored if action is triggered by pull_request event, unless 'allow-override-on-pr' is set to true. # default: ${{ github.ref }} ref: @@ -178,6 +178,14 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob # - '!**/*.jpeg' # - '!**/*.md' predicate-quantifier: 'some' + + # When true, the user-provided `base` and/or `ref` inputs are honored even if + # the action is triggered by a pull_request, pull_request_review, + # pull_request_review_comment, or pull_request_target event. In that case the + # action skips the GitHub API path and uses git diff against the provided + # base/ref. Has no effect if `base` and `ref` are both empty. + # Default: false + allow-override-on-pr: 'false' ``` ## Outputs diff --git a/action.yml b/action.yml index 914524f..b6c14c5 100644 --- a/action.yml +++ b/action.yml @@ -49,6 +49,14 @@ inputs: allows to override the "at least one pattern" behavior to make it so that all of the patterns have to match or otherwise the file is excluded. required: false default: 'some' + allow-override-on-pr: + description: | + When true, the user-provided `base` and/or `ref` inputs are honored even if the action + is triggered by a pull_request, pull_request_review, pull_request_review_comment, or + pull_request_target event. In that case the action skips the GitHub API path and uses + git diff against the provided base/ref. Has no effect if `base` and `ref` are both empty. + required: false + default: 'false' outputs: changes: description: JSON array with names of all filters matching any of changed files diff --git a/dist/index.js b/dist/index.js index 3365cd3..3c92ab6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -575,6 +575,7 @@ async function run() { 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 }) || filter_1.PredicateQuantifier.SOME; + const allowOverrideOnPr = core.getBooleanInput('allow-override-on-pr', { required: false }); if (!isExportFormat(listFiles)) { core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`); return; @@ -586,7 +587,7 @@ async function run() { } const filterConfig = { predicateQuantifier }; const filter = new filter_1.Filter(filtersYaml, filterConfig); - const files = await getChangedFiles(token, base, ref, initialFetchDepth); + const files = await getChangedFiles(token, base, ref, initialFetchDepth, allowOverrideOnPr); core.info(`Detected ${files.length} changed files`); const results = filter.match(files); exportResults(results, listFiles); @@ -607,7 +608,7 @@ function getConfigFileContent(configPath) { } return fs.readFileSync(configPath, { encoding: 'utf8' }); } -async function getChangedFiles(token, base, ref, initialFetchDepth) { +async function getChangedFiles(token, base, ref, initialFetchDepth, allowOverrideOnPr) { var _a, _b; // if base is 'HEAD' only local uncommitted changes will be detected // This is the simplest case as we don't need to fetch more commits or evaluate current/before refs @@ -624,6 +625,10 @@ async function getChangedFiles(token, base, ref, initialFetchDepth) { case 'pull_request_review': case 'pull_request_review_comment': case 'pull_request_target': { + if (allowOverrideOnPr && (base || ref)) { + core.info(`'allow-override-on-pr' is enabled and base/ref were provided — skipping PR API and using git diff`); + return getChangedFilesFromGit(base, ref, initialFetchDepth); + } if (ref) { core.warning(`'ref' input parameter is ignored when action is triggered by pull request event`); } diff --git a/src/main.ts b/src/main.ts index 8adb308..a522f0c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,7 @@ async function run(): Promise { 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 + const allowOverrideOnPr = core.getBooleanInput('allow-override-on-pr', {required: false}) if (!isExportFormat(listFiles)) { core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`) @@ -49,7 +50,7 @@ async function run(): Promise { const filterConfig: FilterConfig = {predicateQuantifier} const filter = new Filter(filtersYaml, filterConfig) - const files = await getChangedFiles(token, base, ref, initialFetchDepth) + const files = await getChangedFiles(token, base, ref, initialFetchDepth, allowOverrideOnPr) core.info(`Detected ${files.length} changed files`) const results = filter.match(files) exportResults(results, listFiles) @@ -74,7 +75,13 @@ function getConfigFileContent(configPath: string): string { return fs.readFileSync(configPath, {encoding: 'utf8'}) } -async function getChangedFiles(token: string, base: string, ref: string, initialFetchDepth: number): Promise { +async function getChangedFiles( + token: string, + base: string, + ref: string, + initialFetchDepth: number, + allowOverrideOnPr: boolean +): Promise { // if base is 'HEAD' only local uncommitted changes will be detected // This is the simplest case as we don't need to fetch more commits or evaluate current/before refs if (base === git.HEAD) { @@ -91,6 +98,10 @@ async function getChangedFiles(token: string, base: string, ref: string, initial case 'pull_request_review': case 'pull_request_review_comment': case 'pull_request_target': { + if (allowOverrideOnPr && (base || ref)) { + core.info(`'allow-override-on-pr' is enabled and base/ref were provided — skipping PR API and using git diff`) + return getChangedFilesFromGit(base, ref, initialFetchDepth) + } if (ref) { core.warning(`'ref' input parameter is ignored when action is triggered by pull request event`) }