Skip to content

Commit

Permalink
Filters input accepts path to external yaml file (#8)
Browse files Browse the repository at this point in the history
* Filters input accepts path to external yaml file

* Fix formatting and eslint issues

* Fix file ext of filters yaml file

* Update documentation in README file
  • Loading branch information
dorny authored May 24, 2020
1 parent 0612377 commit a2e5f9f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/filters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
any:
- "**/*"
6 changes: 5 additions & 1 deletion .github/workflows/pull-request-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
- uses: actions/checkout@v2
- uses: ./
id: filter
with:
filters: '.github/filters.yml'
- uses: ./
id: inlineFilter
with:
filters: |
src:
Expand All @@ -31,5 +35,5 @@ jobs:
any:
- "**/*"
- name: filter-test
if: steps.filter.outputs.any != 'true'
if: steps.filter.outputs.any != 'true' || steps.inlineFilter.outputs.any != 'true'
run: exit 1
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Output variables can be later used in the `if` clause to conditionally run speci

### Inputs
- **`githubToken`**: GitHub Access Token - defaults to `${{ github.token }}`
- **`filters`**: YAML dictionary where keys specifies rule names and values are lists of file path patterns
- **`filters`**: Path to the configuration file or directly embedded string in YAML format. Filter configuration is a dictionary, where keys specifies rule names and values are lists of file path patterns.

### Outputs
- For each rule it sets output variable named by the rule to text:
Expand Down Expand Up @@ -53,6 +53,7 @@ jobs:
- uses: dorny/pr-changed-files-filter@v1
id: filter
with:
# inline YAML or path to separate file (e.g.: .github/filters.yaml)
filters: |
backend:
- 'backend/**/*'
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inputs:
required: true
default: ${{ github.token }}
filters:
description: 'YAML dictionary where keys specifies rule names and values are lists of (globbing) file path patterns'
description: 'Path to the configuration file or YAML string with filters definition'
required: true
runs:
using: 'node12'
Expand Down
18 changes: 16 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4129,21 +4129,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(__webpack_require__(747));
const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469));
const filter_1 = __importDefault(__webpack_require__(235));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const token = core.getInput('githubToken', { required: true });
const filterYaml = core.getInput('filters', { required: true });
const filtersInput = core.getInput('filters', { required: true });
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput;
const client = new github.GitHub(token);
if (github.context.eventName !== 'pull_request') {
core.setFailed('This action can be triggered only by pull_request event');
return;
}
const pr = github.context.payload.pull_request;
const filter = new filter_1.default(filterYaml);
const filter = new filter_1.default(filtersYaml);
const files = yield getChangedFiles(client, pr);
const result = filter.match(files);
for (const key in result) {
Expand All @@ -4155,6 +4157,18 @@ function run() {
}
});
}
function isPathInput(text) {
return !text.includes('\n');
}
function getConfigFileContent(configPath) {
if (!fs.existsSync(configPath)) {
throw new Error(`Configuration file '${configPath}' not found`);
}
if (!fs.lstatSync(configPath).isFile()) {
throw new Error(`'${configPath}' is not a file.`);
}
return fs.readFileSync(configPath, { encoding: 'utf8' });
}
// Uses github REST api to get list of files changed in PR
function getChangedFiles(client, pullRequest) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
25 changes: 22 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Webhooks} from '@octokit/webhooks'
Expand All @@ -7,7 +8,10 @@ import Filter from './filter'
async function run(): Promise<void> {
try {
const token = core.getInput('githubToken', {required: true})
const filterYaml = core.getInput('filters', {required: true})
const filtersInput = core.getInput('filters', {required: true})

const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput

const client = new github.GitHub(token)

if (github.context.eventName !== 'pull_request') {
Expand All @@ -16,9 +20,8 @@ async function run(): Promise<void> {
}

const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest
const filter = new Filter(filterYaml)
const filter = new Filter(filtersYaml)
const files = await getChangedFiles(client, pr)

const result = filter.match(files)
for (const key in result) {
core.setOutput(key, String(result[key]))
Expand All @@ -28,6 +31,22 @@ async function run(): Promise<void> {
}
}

function isPathInput(text: string): boolean {
return !text.includes('\n')
}

function getConfigFileContent(configPath: string): string {
if (!fs.existsSync(configPath)) {
throw new Error(`Configuration file '${configPath}' not found`)
}

if (!fs.lstatSync(configPath).isFile()) {
throw new Error(`'${configPath}' is not a file.`)
}

return fs.readFileSync(configPath, {encoding: 'utf8'})
}

// Uses github REST api to get list of files changed in PR
async function getChangedFiles(
client: github.GitHub,
Expand Down

0 comments on commit a2e5f9f

Please sign in to comment.