Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add matched files to output #24

Merged
merged 7 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ jobs:
# Override ${{ github.event.pull_request.head.sha }} to HEAD for testing
head: 'HEAD'
- name: Check Output
run: '[[ "${{ steps.filter.outputs.test }}" = "true" ]]'
run: |
[[ "${{ steps.filter.outputs.test }}" = "true" ]] &&
[[ "${{ steps.filter.outputs.test_files }}" = "testzone/touched.txt" ]]
# test case of issue #7
test-3:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -97,4 +99,34 @@ jobs:
# Although "base" has a additional commit modifying the file matching "testzone_1/*.txt" (testzone_1/added_by_other_pr.txt),
# the file is already merged in the "base"
run: '[[ "${{ steps.filter.outputs.test }}" = "false" && "${{ steps.filter.outputs.this_pr }}" = "true" ]]'

test-4:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Committer Info
run: |
git config user.name 'Github Action' && \
git config user.email '[email protected]'
# add a fake commit
- name: Add Commit
run: |
touch a.txt && \
touch b.txt && \
git add a.txt b.txt && \
git commit -m "add txt files"
- uses: ./
id: filter
with:
filters: |
test:
- '*.txt'
a:
- a.txt
# Override ${{ github.event.pull_request.head.sha }} to HEAD for testing
head: 'HEAD'
- name: Check Output
run: |
[[ "${{ steps.filter.outputs.test }}" = "true" ]] &&
[[ "${{ steps.filter.outputs.test_files }}" = "a.txt b.txt" ]] &&
[[ "${{ steps.filter.outputs.a }}" = "true" ]] &&
[[ "${{ steps.filter.outputs.a_files }}" = "a.txt" ]]
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: tony84727/changed-file-filter@0.0.3
- uses: tony84727/changed-file-filter@0.2.0
id: filter
with:
# head: optional head commit SHA, default to ${{ github.event.pull_request.head.sha || github.sha }}
Expand All @@ -37,4 +37,11 @@ jobs:
- name: Test Doc
if: steps.filter.outputs.doc == 'true'
run: ./doc/test.sh
- name: Print changed doc files
if: steps.filter.outputs.doc == 'true'
run: echo "Changed doc files: ${{ step.filter.outputs.doc_files }}"
```

### Contributing

Before creating a pull request make sure to execute ``npm run all``, and also to commit the generated files.
6 changes: 4 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ const rule_1 = __webpack_require__(902);
const glob_1 = __webpack_require__(85);
function evaluateRule(rule, changedFiles) {
const globber = glob_1.newGlobber(rule.match);
return changedFiles.find(globber) !== undefined;
return changedFiles.filter(globber);
}
function getBaseSha(event) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -1565,9 +1565,11 @@ function run() {
const changedFiles = yield git_1.getChangedFiles(baseSha, headSha);
core.debug(`changedFiles: ${changedFiles}`);
for (const r of rules) {
const changed = evaluateRule(r, changedFiles) ? 'true' : 'false';
const matchedFiles = evaluateRule(r, changedFiles);
const changed = matchedFiles.length > 0 ? 'true' : 'false';
core.debug(`rule: ${r.name}, changed: ${changed}`);
core.setOutput(r.name, changed);
core.setOutput(`${r.name}_files`, matchedFiles.join(' '));
}
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "changed-file-filter",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"description": "Only run Github Action job when specific files changed",
"main": "lib/main.js",
Expand Down
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as core from '@actions/core'
import {getChangedFiles, unshallow, revParse} from './git'
import {Rule, parseRules} from './rule'
import {newGlobber} from './glob'
function evaluateRule(rule: Rule, changedFiles: string[]): boolean {
function evaluateRule(rule: Rule, changedFiles: string[]): string[] {
const globber = newGlobber(rule.match)
return changedFiles.find(globber) !== undefined
return changedFiles.filter(globber)
}

async function getBaseSha(event: string): Promise<string> {
Expand Down Expand Up @@ -35,9 +35,11 @@ async function run(): Promise<void> {
const changedFiles = await getChangedFiles(baseSha, headSha)
core.debug(`changedFiles: ${changedFiles}`)
for (const r of rules) {
const changed = evaluateRule(r, changedFiles) ? 'true' : 'false'
const matchedFiles = evaluateRule(r, changedFiles)
const changed = matchedFiles.length > 0 ? 'true' : 'false'
core.debug(`rule: ${r.name}, changed: ${changed}`)
core.setOutput(r.name, changed)
core.setOutput(`${r.name}_files`, matchedFiles.join(' '))
}
} catch (error) {
core.setFailed(error.message)
Expand Down