Skip to content

Commit 28cec18

Browse files
lykahb9999years
authored andcommitted
Collect and report number of changes
1 parent de90cc6 commit 28cec18

File tree

11 files changed

+373
-168
lines changed

11 files changed

+373
-168
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
# v3.0.3
4+
- Added stat feature which computes diffs in subtrees
5+
36
## v3.0.2
47
- [Add config parameter for predicate quantifier](https://github.com/dorny/paths-filter/pull/224)
58

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Paths Changes Filter
1+
# Paths Changes Filter And Diff Stat
22

33
[GitHub Action](https://github.com/features/actions) that enables conditional execution of workflow steps and jobs, based on the files modified by pull request, on a feature
44
branch, or by the recently pushed commits.
@@ -73,6 +73,7 @@ For more scenarios see [examples](#examples) section.
7373
## What's New
7474

7575
- New major release `v3` after update to Node 20 [Breaking change]
76+
- Add `stat` parameter that enables output of the file changes statistics per filter.
7677
- Add `ref` input parameter
7778
- Add `list-files: csv` format
7879
- Configure matrix job to run for each folder with changes using `changes` output
@@ -154,14 +155,14 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob
154155
# Default: ${{ github.token }}
155156
token: ''
156157
157-
# Optional parameter to override the default behavior of file matching algorithm.
158+
# Optional parameter to override the default behavior of file matching algorithm.
158159
# By default files that match at least one pattern defined by the filters will be included.
159160
# This parameter allows to override the "at least one pattern" behavior to make it so that
160-
# all of the patterns have to match or otherwise the file is excluded.
161-
# An example scenario where this is useful if you would like to match all
162-
# .ts files in a sub-directory but not .md files.
163-
# The filters below will match markdown files despite the exclusion syntax UNLESS
164-
# you specify 'every' as the predicate-quantifier parameter. When you do that,
161+
# all of the patterns have to match or otherwise the file is excluded.
162+
# An example scenario where this is useful if you would like to match all
163+
# .ts files in a sub-directory but not .md files.
164+
# The filters below will match markdown files despite the exclusion syntax UNLESS
165+
# you specify 'every' as the predicate-quantifier parameter. When you do that,
165166
# it will only match the .ts files in the subdirectory as expected.
166167
#
167168
# backend:
@@ -179,6 +180,7 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob
179180
- For each filter, it sets an output variable with the name `${FILTER_NAME}_count` to the count of matching files.
180181
- If enabled, for each filter it sets an output variable with the name `${FILTER_NAME}_files`. It will contain a list of all files matching the filter.
181182
- `changes` - JSON array with names of all filters matching any of the changed files.
183+
- If `stat` input is set to an output format, the output variable `stat` contains JSON or CSV value with the change statistics for each filter.
182184

183185
## Examples
184186

@@ -558,10 +560,37 @@ jobs:
558560
559561
</details>
560562
563+
<details>
564+
<summary>Passing number of added lines from a filter to another action</summary>
565+
566+
```yaml
567+
- uses: dorny/paths-filter@v2
568+
id: filter
569+
with:
570+
# Enable listing of diff stat matching each filter.
571+
# Paths to files will be available in `stat` output variable.
572+
# Stat will be formatted as JSON object
573+
stat: json
574+
575+
# In this example all changed files are passed to the following action to do
576+
# some custom processing.
577+
filters: |
578+
changed:
579+
- '**'
580+
- name: Lint Markdown
581+
uses: johndoe/some-action@v1
582+
# Run action only if the change is large enough.
583+
if: ${{fromJson(steps.filter.outputs.stat).changed.additionCount > 1000}}
584+
with:
585+
files: ${{ steps.filter.outputs.changed_files }}
586+
```
587+
588+
</details>
589+
561590
## See also
562591
563592
- [test-reporter](https://github.com/dorny/test-reporter) - Displays test results from popular testing frameworks directly in GitHub
564593
565594
## License
566595
567-
The scripts and documentation in this project are released under the [MIT License](https://github.com/dorny/paths-filter/blob/master/LICENSE)
596+
The scripts and documentation in this project are released under the [MIT License](https://github.com/lykahb/paths-filter/blob/master/LICENSE)

__tests__/filter.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('matching specific change status', () => {
181181
- added: "**/*"
182182
`
183183
let filter = new Filter(yaml)
184-
const files = [{status: ChangeStatus.Added, filename: 'file.js'}]
184+
const files = [{status: ChangeStatus.Added, filename: 'file.js', additions: 1, deletions: 0}]
185185
const match = filter.match(files)
186186
expect(match.add).toEqual(files)
187187
})
@@ -192,7 +192,7 @@ describe('matching specific change status', () => {
192192
- added|modified: "**/*"
193193
`
194194
let filter = new Filter(yaml)
195-
const files = [{status: ChangeStatus.Modified, filename: 'file.js'}]
195+
const files = [{status: ChangeStatus.Modified, filename: 'file.js', additions: 1, deletions: 1}]
196196
const match = filter.match(files)
197197
expect(match.addOrModify).toEqual(files)
198198
})
@@ -214,12 +214,6 @@ describe('matching specific change status', () => {
214214

215215
function modified(paths: string[]): File[] {
216216
return paths.map(filename => {
217-
return {filename, status: ChangeStatus.Modified}
218-
})
219-
}
220-
221-
function renamed(paths: string[]): File[] {
222-
return paths.map(filename => {
223-
return {filename, status: ChangeStatus.Renamed}
217+
return {filename, status: ChangeStatus.Modified, additions: 1, deletions: 1}
224218
})
225219
}

__tests__/git.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as git from '../src/git'
22
import {ChangeStatus} from '../src/file'
33

44
describe('parsing output of the git diff command', () => {
5-
test('parseGitDiffOutput returns files with correct change status', async () => {
6-
const files = git.parseGitDiffOutput(
5+
test('parseGitDiffNameStatusOutput returns files with correct change status', async () => {
6+
const files = git.parseGitDiffNameStatusOutput(
77
'A\u0000LICENSE\u0000' + 'M\u0000src/index.ts\u0000' + 'D\u0000src/main.ts\u0000'
88
)
99
expect(files.length).toBe(3)
@@ -14,6 +14,17 @@ describe('parsing output of the git diff command', () => {
1414
expect(files[2].filename).toBe('src/main.ts')
1515
expect(files[2].status).toBe(ChangeStatus.Deleted)
1616
})
17+
18+
test('parseGitDiffNumstatOutput returns files with correct change status', async () => {
19+
const files = git.parseGitDiffNumstatOutput('4\t2\tLICENSE\u0000' + '5\t0\tsrc/index.ts\u0000')
20+
expect(files.length).toBe(2)
21+
expect(files[0].filename).toBe('LICENSE')
22+
expect(files[0].additions).toBe(4)
23+
expect(files[0].deletions).toBe(2)
24+
expect(files[1].filename).toBe('src/index.ts')
25+
expect(files[1].additions).toBe(5)
26+
expect(files[1].deletions).toBe(0)
27+
})
1728
})
1829

1930
describe('git utility function tests (those not invoking git)', () => {

action.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
name: 'Paths Changes Filter'
1+
name: 'Paths Changes Filter And Diff Stat'
22
description: 'Execute your workflow steps only if relevant files are modified.'
3-
author: 'Michal Dorner <[email protected]>'
3+
author: 'Michal Dorner <[email protected]>, Boris Lykah<[email protected]>'
44
inputs:
55
token:
66
description: 'GitHub Access Token'
@@ -36,6 +36,16 @@ inputs:
3636
Backslash escapes every potentially unsafe character.
3737
required: false
3838
default: none
39+
stat:
40+
description: |
41+
Enables listing of that enables output of the file change statistics per filter, similar to `git diff --shortstat`.
42+
If some changes do not match any filter, the output includes an additional entry with the filter name 'other'.
43+
'none' - Disables listing of stats (default).
44+
'csv' - Coma separated list that has name of filter, count of additions, count of deletions, count of changed files.
45+
If needed it uses double quotes to wrap name of filter with unsafe characters. For example, `"some filter",12,7,2`.
46+
'json' - Serialized as JSON object where the filter names are keys. For example, `{"some filter": {"additionCount": 12, "deletionCount": 7, "fileCount": 2}}`
47+
required: false
48+
default: none
3949
initial-fetch-depth:
4050
description: |
4151
How many commits are initially fetched from base branch.

0 commit comments

Comments
 (0)