Compare commits

..

No commits in common. "master" and "v2" have entirely different histories.
master ... v2

11 changed files with 38617 additions and 32106 deletions

View file

@ -1,5 +1,4 @@
name: "Build"
on:
push:
paths-ignore: [ '*.md' ]
@ -10,11 +9,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- uses: actions/checkout@v3
- run: |
npm install
npm run all
@ -22,7 +17,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: ./
id: filter
with:

View file

@ -10,11 +10,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- uses: actions/checkout@v3
- run: |
npm install
npm run all
@ -24,7 +20,7 @@ jobs:
permissions:
pull-requests: read
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: ./
id: filter
with:
@ -45,7 +41,7 @@ jobs:
permissions:
pull-requests: read
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: ./
id: filter
with:
@ -57,7 +53,7 @@ jobs:
test-without-token:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: ./
id: filter
with:
@ -70,7 +66,7 @@ jobs:
test-wd-without-token:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
with:
path: somewhere
- uses: ./somewhere
@ -86,7 +82,7 @@ jobs:
test-local-changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- run: echo "NEW FILE" > local
- run: git add local
- uses: ./
@ -106,7 +102,7 @@ jobs:
test-change-type:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- name: configure GIT user
run: git config user.email "john@nowhere.local" && git config user.name "John Doe"
- name: modify working tree

View file

@ -1,15 +1,5 @@
# Changelog
## v3.0.2
- [Add config parameter for predicate quantifier](https://github.com/dorny/paths-filter/pull/224)
## v3.0.1
- [Compare base and ref when token is empty](https://github.com/dorny/paths-filter/pull/133)
## v3.0.0
- [Update to Node.js 20](https://github.com/dorny/paths-filter/pull/210)
- [Update all dependencies](https://github.com/dorny/paths-filter/pull/215)
## v2.11.1
- [Update @actions/core to v1.10.0 - Fixes warning about deprecated set-output](https://github.com/dorny/paths-filter/pull/167)
- [Document need for pull-requests: read permission](https://github.com/dorny/paths-filter/pull/168)

View file

@ -11,7 +11,6 @@ don't allow this because they don't work on a level of individual jobs or steps.
- [sentry.io](https://sentry.io/) - [backend.yml](https://github.com/getsentry/sentry/blob/2ebe01feab863d89aa7564e6d243b6d80c230ddc/.github/workflows/backend.yml#L36)
- [GoogleChrome/web.dev](https://web.dev/) - [lint-workflow.yml](https://github.com/GoogleChrome/web.dev/blob/3a57b721e7df6fc52172f676ca68d16153bda6a3/.github/workflows/lint-workflow.yml#L26)
- [blog post Configuring python linting to be part of CI/CD using GitHub actions](https://dev.to/freshbooks/configuring-python-linting-to-be-part-of-cicd-using-github-actions-1731#what-files-does-it-run-against) - [py_linter.yml](https://github.com/iamtodor/demo-github-actions-python-linter-configuration/blob/main/.github/workflows/py_linter.yml#L31)
## Supported workflows
@ -46,7 +45,7 @@ don't allow this because they don't work on a level of individual jobs or steps.
## Example
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: changes
with:
filters: |
@ -72,7 +71,6 @@ For more scenarios see [examples](#examples) section.
## What's New
- New major release `v3` after update to Node 20 [Breaking change]
- Add `ref` input parameter
- Add `list-files: csv` format
- Configure matrix job to run for each folder with changes using `changes` output
@ -84,7 +82,7 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob
## Usage
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
with:
# Defines filters applied to detected changed files.
# Each filter has a name and a list of rules.
@ -153,22 +151,6 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob
# changes using git commands.
# Default: ${{ github.token }}
token: ''
# Optional parameter to override the default behavior of file matching algorithm.
# By default files that match at least one pattern defined by the filters will be included.
# This parameter 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.
# An example scenario where this is useful if you would like to match all
# .ts files in a sub-directory but not .md files.
# The filters below will match markdown files despite the exclusion syntax UNLESS
# you specify 'every' as the predicate-quantifier parameter. When you do that,
# it will only match the .ts files in the subdirectory as expected.
#
# backend:
# - 'pkg/a/b/c/**'
# - '!**/*.jpeg'
# - '!**/*.md'
predicate-quantifier: 'some'
```
## Outputs
@ -192,8 +174,8 @@ jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
@ -237,7 +219,7 @@ jobs:
frontend: ${{ steps.filter.outputs.frontend }}
steps:
# For pull requests it's not necessary to checkout the code
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
@ -252,7 +234,7 @@ jobs:
if: ${{ needs.changes.outputs.backend == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- ...
# JOB to build and test frontend code
@ -261,7 +243,7 @@ jobs:
if: ${{ needs.changes.outputs.frontend == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- ...
```
@ -283,7 +265,7 @@ jobs:
packages: ${{ steps.filter.outputs.changes }}
steps:
# For pull requests it's not necessary to checkout the code
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
@ -300,7 +282,7 @@ jobs:
package: ${{ fromJSON(needs.changes.outputs.packages) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- ...
```
@ -324,8 +306,8 @@ jobs:
permissions:
pull-requests: read
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
filters: ... # Configure your filters
@ -345,12 +327,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
with:
# This may save additional git fetch roundtrip if
# merge-base is found within latest 20 commits
fetch-depth: 20
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
base: develop # Change detection against merge-base with this branch
@ -373,8 +355,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# Use context to get the branch where commits were pushed.
@ -401,14 +383,14 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
# Some action that modifies files tracked by git (e.g. code linter)
- uses: johndoe/some-action@v1
# Filter to detect which files were modified
# Changes could be, for example, automatically committed
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
base: HEAD
@ -423,7 +405,7 @@ jobs:
<summary>Define filter rules in own file</summary>
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# Path to file where filters are defined
@ -436,7 +418,7 @@ jobs:
<summary>Use YAML anchors to reuse path expression(s) inside another rule</summary>
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# &shared is YAML anchor,
@ -457,7 +439,7 @@ jobs:
<summary>Consider if file was added, modified or deleted</summary>
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# Changed file can be 'added', 'modified', or 'deleted'.
@ -479,39 +461,13 @@ jobs:
</details>
<details>
<summary>Detect changes in folder only for some file extensions</summary>
```yaml
- uses: dorny/paths-filter@v3
id: filter
with:
# This makes it so that all the patterns have to match a file for it to be
# considered changed. Because we have the exclusions for .jpeg and .md files
# the end result is that if those files are changed they will be ignored
# because they don't match the respective rules excluding them.
#
# 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.
predicate-quantifier: 'every'
filters: |
backend:
- 'pkg/a/b/c/**'
- '!**/*.jpeg'
- '!**/*.md'
```
</details>
### Custom processing of changed files
<details>
<summary>Passing list of modified files as command line args in Linux shell</summary>
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# Enable listing of files matching each filter.
@ -537,7 +493,7 @@ jobs:
<summary>Passing list of modified files as JSON array to another action</summary>
```yaml
- uses: dorny/paths-filter@v3
- uses: dorny/paths-filter@v2
id: filter
with:
# Enable listing of files matching each filter.

View file

@ -1,4 +1,4 @@
import {Filter, FilterConfig, PredicateQuantifier} from '../src/filter'
import {Filter} from '../src/filter'
import {File, ChangeStatus} from '../src/file'
describe('yaml filter parsing tests', () => {
@ -117,37 +117,6 @@ describe('matching tests', () => {
expect(pyMatch.backend).toEqual(pyFiles)
})
test('matches only files that are matching EVERY pattern when set to PredicateQuantifier.EVERY', () => {
const yaml = `
backend:
- 'pkg/a/b/c/**'
- '!**/*.jpeg'
- '!**/*.md'
`
const filterConfig: FilterConfig = {predicateQuantifier: PredicateQuantifier.EVERY}
const filter = new Filter(yaml, filterConfig)
const typescriptFiles = modified(['pkg/a/b/c/some-class.ts', 'pkg/a/b/c/src/main/some-class.ts'])
const otherPkgTypescriptFiles = modified(['pkg/x/y/z/some-class.ts', 'pkg/x/y/z/src/main/some-class.ts'])
const otherPkgJpegFiles = modified(['pkg/x/y/z/some-pic.jpeg', 'pkg/x/y/z/src/main/jpeg/some-pic.jpeg'])
const docsFiles = modified([
'pkg/a/b/c/some-pics.jpeg',
'pkg/a/b/c/src/main/jpeg/some-pic.jpeg',
'pkg/a/b/c/src/main/some-docs.md',
'pkg/a/b/c/some-docs.md'
])
const typescriptMatch = filter.match(typescriptFiles)
const otherPkgTypescriptMatch = filter.match(otherPkgTypescriptFiles)
const docsMatch = filter.match(docsFiles)
const otherPkgJpegMatch = filter.match(otherPkgJpegFiles)
expect(typescriptMatch.backend).toEqual(typescriptFiles)
expect(otherPkgTypescriptMatch.backend).toEqual([])
expect(docsMatch.backend).toEqual([])
expect(otherPkgJpegMatch.backend).toEqual([])
})
test('matches path based on rules included using YAML anchor', () => {
const yaml = `
shared: &shared
@ -217,9 +186,3 @@ function modified(paths: string[]): File[] {
return {filename, status: ChangeStatus.Modified}
})
}
function renamed(paths: string[]): File[] {
return paths.map(filename => {
return {filename, status: ChangeStatus.Renamed}
})
}

View file

@ -44,16 +44,11 @@ inputs:
This option takes effect only when changes are detected using git against different base branch.
required: false
default: '100'
predicate-quantifier:
description: |
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'
outputs:
changes:
description: JSON array with names of all filters matching any of changed files
runs:
using: 'node20'
using: 'node16'
main: 'dist/index.js'
branding:
color: blue

55967
dist/index.js vendored

File diff suppressed because one or more lines are too long

14424
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,6 @@
{
"name": "paths-filter",
"version": "1.0.0",
"engines": {
"node": ">= 20"
},
"private": true,
"description": "Execute your workflow steps only if relevant files are modified.",
"main": "lib/main.js",
@ -30,26 +27,27 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/github": "6.0.0",
"picomatch": "^2.3.1"
"@actions/github": "^2.2.0",
"@octokit/webhooks": "^7.6.2",
"picomatch": "^2.2.2"
},
"devDependencies": {
"@octokit/webhooks-types": "^7.3.1",
"@types/jest": "^29.5.11",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.11.6",
"@types/picomatch": "^2.3.3",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@vercel/ncc": "^0.38.1",
"eslint": "^8.56.0",
"eslint-plugin-github": "^4.10.1",
"eslint-plugin-jest": "^27.6.3",
"jest": "^29.7.0",
"jest-circus": "^29.7.0",
"js-yaml": "^4.1.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
"@types/jest": "^27.4.0",
"@types/js-yaml": "^3.12.4",
"@types/minimatch": "^3.0.3",
"@types/node": "^14.0.5",
"@types/picomatch": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"@vercel/ncc": "^0.33.1",
"eslint": "^8.17.0",
"eslint-plugin-github": "^4.3.6",
"eslint-plugin-jest": "^22.21.0",
"jest": "^27.4.7",
"jest-circus": "^27.4.6",
"js-yaml": "^3.14.0",
"prettier": "^2.0.5",
"ts-jest": "^27.1.3",
"typescript": "^3.9.3"
}
}

View file

@ -23,48 +23,6 @@ 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[]
}
@ -73,7 +31,7 @@ export class Filter {
rules: {[key: string]: FilterRuleItem[]} = {}
// Creates instance of Filter and load rules from YAML if it's provided
constructor(yaml?: string, readonly filterConfig?: FilterConfig) {
constructor(yaml?: string) {
if (yaml) {
this.load(yaml)
}
@ -85,7 +43,7 @@ export class Filter {
return
}
const doc = jsyaml.load(yaml) as FilterYaml
const doc = jsyaml.safeLoad(yaml) as FilterYaml
if (typeof doc !== 'object') {
this.throwInvalidFormatError('Root element is not an object')
}
@ -104,14 +62,9 @@ export class Filter {
}
private isMatch(file: File, patterns: FilterRuleItem[]): boolean {
const aPredicate = (rule: Readonly<FilterRuleItem>): boolean => {
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)
}
return patterns.some(
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.isMatch(file.filename)
)
}
private parseFilterItemYaml(item: FilterItemYaml): FilterRuleItem[] {

View file

@ -1,17 +1,10 @@
import * as fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
import {PushEvent, PullRequestEvent} from '@octokit/webhooks-types'
import type {Octokit} from '@octokit/rest'
import {Webhooks} from '@octokit/webhooks'
import {
isPredicateQuantifier,
Filter,
FilterConfig,
FilterResults,
PredicateQuantifier,
SUPPORTED_PREDICATE_QUANTIFIERS
} from './filter'
import {Filter, FilterResults} from './filter'
import {File, ChangeStatus} from './file'
import * as git from './git'
import {backslashEscape, shellEscape} from './list-format/shell-escape'
@ -33,28 +26,19 @@ 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
}
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 filter = new Filter(filtersYaml)
const files = await getChangedFiles(token, base, ref, initialFetchDepth)
core.info(`Detected ${files.length} changed files`)
const results = filter.match(files)
exportResults(results, listFiles)
} catch (error) {
core.setFailed(getErrorMessage(error))
core.setFailed(error.message)
}
}
@ -92,7 +76,7 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
if (base) {
core.warning(`'base' input parameter is ignored when action is triggered by pull request event`)
}
const pr = github.context.payload.pull_request as PullRequestEvent
const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
if (token) {
return await getChangedFilesFromApi(token, pr)
}
@ -102,11 +86,8 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
// At the same time we don't want to fetch any code from forked repository
throw new Error(`'token' input parameter is required if action is triggered by 'pull_request_target' event`)
}
core.info('Github token is not available - changes will be detected using git diff')
const baseSha = github.context.payload.pull_request?.base.sha
const defaultBranch = github.context.payload.repository?.default_branch
const currentRef = await git.getCurrentRef()
return await git.getChanges(base || baseSha || defaultBranch, currentRef)
core.info('Github token is not available - changes will be detected from PRs merge commit')
return await git.getChangesInLastCommit()
} else {
return getChangedFilesFromGit(base, ref, initialFetchDepth)
}
@ -115,7 +96,8 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
async function getChangedFilesFromGit(base: string, head: string, initialFetchDepth: number): Promise<File[]> {
const defaultBranch = github.context.payload.repository?.default_branch
const beforeSha = github.context.eventName === 'push' ? (github.context.payload as PushEvent).before : null
const beforeSha =
github.context.eventName === 'push' ? (github.context.payload as Webhooks.WebhookPayloadPush).before : null
const currentRef = await git.getCurrentRef()
@ -175,28 +157,31 @@ async function getChangedFilesFromGit(base: string, head: string, initialFetchDe
}
// Uses github REST api to get list of files changed in PR
async function getChangedFilesFromApi(token: string, pullRequest: PullRequestEvent): Promise<File[]> {
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
async function getChangedFilesFromApi(
token: string,
prNumber: Webhooks.WebhookPayloadPullRequestPullRequest
): Promise<File[]> {
core.startGroup(`Fetching list of changed files for PR#${prNumber.number} from Github API`)
try {
const client = github.getOctokit(token)
const client = new github.GitHub(token)
const per_page = 100
const files: File[] = []
core.info(`Invoking listFiles(pull_number: ${pullRequest.number}, per_page: ${per_page})`)
core.info(`Invoking listFiles(pull_number: ${prNumber.number}, per_page: ${per_page})`)
for await (const response of client.paginate.iterator(
client.rest.pulls.listFiles.endpoint.merge({
client.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: pullRequest.number,
pull_number: prNumber.number,
per_page
})
)) {
) as AsyncIterableIterator<Octokit.Response<Octokit.PullsListFilesResponse>>) {
if (response.status !== 200) {
throw new Error(`Fetching list of changed files from GitHub API failed with error code ${response.status}`)
}
core.info(`Received ${response.data.length} items`)
for (const row of response.data as GetResponseDataTypeFromEndpointMethod<typeof client.rest.pulls.listFiles>) {
for (const row of response.data) {
core.info(`[${row.status}] ${row.filename}`)
// There's no obvious use-case for detection of renames
// Therefore we treat it as if rename detection in git diff was turned off.
@ -282,9 +267,4 @@ function isExportFormat(value: string): value is ExportFormat {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value)
}
function getErrorMessage(error: unknown): string {
if (error instanceof Error) return error.message
return String(error)
}
run()