From a2e5f9f7bb94ce650550caa0432c6c5bb64dcf79 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 24 May 2020 22:50:33 +0200 Subject: [PATCH] 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 --- .github/filters.yml | 2 ++ .../workflows/pull-request-verification.yml | 6 ++++- README.md | 3 ++- action.yml | 2 +- dist/index.js | 18 +++++++++++-- src/main.ts | 25 ++++++++++++++++--- 6 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 .github/filters.yml diff --git a/.github/filters.yml b/.github/filters.yml new file mode 100644 index 0000000..36fab30 --- /dev/null +++ b/.github/filters.yml @@ -0,0 +1,2 @@ +any: + - "**/*" \ No newline at end of file diff --git a/.github/workflows/pull-request-verification.yml b/.github/workflows/pull-request-verification.yml index 40637a0..6799308 100644 --- a/.github/workflows/pull-request-verification.yml +++ b/.github/workflows/pull-request-verification.yml @@ -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 diff --git a/README.md b/README.md index 5bcdd10..cdef566 100644 --- a/README.md +++ b/README.md @@ -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/**/*' diff --git a/action.yml b/action.yml index 1250a10..eda50f0 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index 2c8f48e..724adf5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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* () { diff --git a/src/main.ts b/src/main.ts index d3854ec..31755a4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { 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 { } 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 { } } +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,