Filters input accepts path to external yaml file (#8)

* 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
This commit is contained in:
Michal Dorner 2020-05-24 22:50:33 +02:00 committed by GitHub
parent 0612377665
commit a2e5f9f7bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 8 deletions

2
.github/filters.yml vendored Normal file
View File

@ -0,0 +1,2 @@
any:
- "**/*"

View File

@ -22,6 +22,10 @@ jobs:
- uses: actions/checkout@v2
- uses: ./
id: filter
with:
filters: '.github/filters.yml'
- uses: ./
id: inlineFilter
with:
filters: |
src:
@ -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

View File

@ -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:
@ -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/**/*'

View File

@ -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'

18
dist/index.js vendored
View File

@ -4129,6 +4129,7 @@ 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));
@ -4136,14 +4137,15 @@ 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) {
@ -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* () {

View File

@ -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'
@ -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') {
@ -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]))
@ -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,