From 0f9f8bf7d2c3f9b9046e7bf16096464812c69650 Mon Sep 17 00:00:00 2001 From: Alden Quimby Date: Thu, 11 May 2023 15:27:48 -0400 Subject: [PATCH] Allow base and ref in PRs to override API behavior --- .../workflows/pull-request-verification.yml | 21 ++++++++ README.md | 54 +++++++++++++++++-- dist/index.js | 6 +-- src/main.ts | 6 +-- 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/.github/workflows/pull-request-verification.yml b/.github/workflows/pull-request-verification.yml index ac21743..b507ee6 100644 --- a/.github/workflows/pull-request-verification.yml +++ b/.github/workflows/pull-request-verification.yml @@ -139,3 +139,24 @@ jobs: || steps.filter.outputs.modified_files != 'LICENSE' || steps.filter.outputs.deleted_files != 'README.md' run: exit 1 + + test-baseref-changes: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: ./ + id: filter + with: + base: ${{ github.base_ref }} + ref: ${{ github.sha }} + filters: | + error: + - not_existing_path/**/* + any: + - "**/*" + - name: filter-test + if: steps.filter.outputs.any != 'true' || steps.filter.outputs.error == 'true' + run: exit 1 + - name: changes-test + if: contains(fromJSON(steps.filter.outputs.changes), 'error') || !contains(fromJSON(steps.filter.outputs.changes), 'any') + run: exit 1 diff --git a/README.md b/README.md index 6825eb1..3f60dd9 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ don't allow this because they don't work on a level of individual jobs or steps. - **Pull requests:** - Workflow triggered by **[pull_request](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request)** or **[pull_request_target](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target)** event - - Changes are detected against the pull request base branch - - Uses GitHub REST API to fetch a list of modified files + - Changes are detected against the pull request base branch (unless base and ref are explicitly provided) + - Uses GitHub REST API to fetch a list of modified files (unless base and ref are explicitly provided) - Requires [pull-requests: read](https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs) permission - **Feature branches:** - Workflow triggered by **[push](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push)** @@ -109,14 +109,12 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob # introduced by the current branch are considered. # All files are considered as added if there is no common ancestor with # base branch or no previous commit. - # This option is ignored if action is triggered by pull_request event. # Default: repository default branch (e.g. master) base: '' # Git reference (e.g. branch name) from which the changes will be detected. # Useful when workflow can be triggered only on the default branch (e.g. repository_dispatch event) # but you want to get changes on a different branch. - # This option is ignored if action is triggered by pull_request event. # default: ${{ github.ref }} ref: @@ -292,7 +290,7 @@ jobs: ### Change detection workflows
- Pull requests: Detect changes against PR base branch + Pull requests (normal use case): Detect changes against PR base branch ```yaml on: @@ -316,6 +314,52 @@ jobs:
+
+ Pull requests (custom use case): Detect changes against tag + +```yaml +on: + pull_request: + branches: + - develop +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dorny/paths-filter@v2 + id: filter + with: + base: some-custom-git-tag + ref: ${{ github.head_ref }} # On a PR, head_ref is the name of the PR branch. + filters: ... # Configure your filters +``` + +
+ +
+ Pull requests (custom use case): Detect changes since the last commit + +```yaml +on: + pull_request: + branches: + - develop +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dorny/paths-filter@v2 + id: filter + with: + base: ${{ github.sha }} + ref: ${{ github.sha }} + filters: ... # Configure your filters +``` + +
+
Feature branch: Detect changes against configured base branch diff --git a/dist/index.js b/dist/index.js index 0602827..1e64e13 100644 --- a/dist/index.js +++ b/dist/index.js @@ -552,12 +552,12 @@ async function getChangedFiles(token, base, ref, initialFetchDepth) { return await git.getChangesOnHead(); } const prEvents = ['pull_request', 'pull_request_review', 'pull_request_review_comment', 'pull_request_target']; - if (prEvents.includes(github.context.eventName)) { + if (prEvents.includes(github.context.eventName) && !base && !ref) { if (ref) { - core.warning(`'ref' input parameter is ignored when 'base' is set to HEAD`); + core.warning(`'ref' input parameter is ignored when 'base' is not also set on PR events.`); } if (base) { - core.warning(`'base' input parameter is ignored when action is triggered by pull request event`); + core.warning(`'base' input parameter is ignored when 'ref' is not also set on PR events.`); } const pr = github.context.payload.pull_request; if (token) { diff --git a/src/main.ts b/src/main.ts index 457765f..b1631b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -69,12 +69,12 @@ async function getChangedFiles(token: string, base: string, ref: string, initial } const prEvents = ['pull_request', 'pull_request_review', 'pull_request_review_comment', 'pull_request_target'] - if (prEvents.includes(github.context.eventName)) { + if (prEvents.includes(github.context.eventName) && !base && !ref) { if (ref) { - core.warning(`'ref' input parameter is ignored when 'base' is set to HEAD`) + core.warning(`'ref' input parameter is ignored when 'base' is not also set on PR events.`) } if (base) { - core.warning(`'base' input parameter is ignored when action is triggered by pull request event`) + core.warning(`'base' input parameter is ignored when 'ref' is not also set on PR events.`) } const pr = github.context.payload.pull_request as Webhooks.WebhookPayloadPullRequestPullRequest if (token) {