From 5c76e5b5c01c603f71e46be07c2651e6b2253c42 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 24 May 2020 22:30:59 +0200 Subject: [PATCH 1/4] Filters input accepts path to external yaml file --- .github/filters.yaml | 2 ++ .../workflows/pull-request-verification.yml | 6 +++- README.md | 1 + action.yml | 2 +- src/main.ts | 31 ++++++++++++++++--- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 .github/filters.yaml diff --git a/.github/filters.yaml b/.github/filters.yaml new file mode 100644 index 00000000..36fab30d --- /dev/null +++ b/.github/filters.yaml @@ -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 40637a05..67993082 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 5bcdd100..a76df568 100644 --- a/README.md +++ b/README.md @@ -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 1250a10d..eda50f06 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/src/main.ts b/src/main.ts index d3854ec4..5f3c1923 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,19 @@ +import * as fs from 'fs' import * as core from '@actions/core' import * as github from '@actions/github' -import {Webhooks} from '@octokit/webhooks' +import { Webhooks } from '@octokit/webhooks' import Filter from './filter' async function run(): Promise { try { - const token = core.getInput('githubToken', {required: true}) - const filterYaml = core.getInput('filters', {required: true}) + const token = core.getInput('githubToken', { 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 +22,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 +33,22 @@ async function run(): Promise { } } +function isPathInput(text: string): boolean { + return text.indexOf('\n') === -1 +} + +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, From 355c1b001f421bd15ccfe854eda7bb172a9336b9 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 24 May 2020 22:40:14 +0200 Subject: [PATCH 2/4] Fix formatting and eslint issues --- dist/index.js | 18 ++++++++++++++++-- src/main.ts | 14 ++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2c8f48e1..724adf53 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 5f3c1923..31755a4f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,18 +1,16 @@ import * as fs from 'fs' import * as core from '@actions/core' import * as github from '@actions/github' -import { Webhooks } from '@octokit/webhooks' +import {Webhooks} from '@octokit/webhooks' import Filter from './filter' async function run(): Promise { try { - const token = core.getInput('githubToken', { required: true }) - const filtersInput = core.getInput('filters', { required: true }) + const token = core.getInput('githubToken', {required: true}) + const filtersInput = core.getInput('filters', {required: true}) - const filtersYaml = isPathInput(filtersInput) - ? getConfigFileContent(filtersInput) - : filtersInput + const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput const client = new github.GitHub(token) @@ -34,7 +32,7 @@ async function run(): Promise { } function isPathInput(text: string): boolean { - return text.indexOf('\n') === -1 + return !text.includes('\n') } function getConfigFileContent(configPath: string): string { @@ -46,7 +44,7 @@ function getConfigFileContent(configPath: string): string { throw new Error(`'${configPath}' is not a file.`) } - return fs.readFileSync(configPath, { encoding: 'utf8' }) + return fs.readFileSync(configPath, {encoding: 'utf8'}) } // Uses github REST api to get list of files changed in PR From 54ec69778d24624a7f2708d277491072a99a9ee5 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 24 May 2020 22:41:51 +0200 Subject: [PATCH 3/4] Fix file ext of filters yaml file --- .github/{filters.yaml => filters.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{filters.yaml => filters.yml} (100%) diff --git a/.github/filters.yaml b/.github/filters.yml similarity index 100% rename from .github/filters.yaml rename to .github/filters.yml From 2dcbbc9b4eaa5382c3a6666e68f3367a77bf1e39 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Sun, 24 May 2020 22:48:54 +0200 Subject: [PATCH 4/4] Update documentation in README file --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a76df568..cdef5668 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: