Skip to content

Commit

Permalink
Allow base and ref in PRs to override API behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenquimby committed May 11, 2023
1 parent 4067d88 commit 487e02f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/pull-request-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 49 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -292,7 +290,7 @@ jobs:
### Change detection workflows

<details>
<summary><b>Pull requests:</b> Detect changes against PR base branch</summary>
<summary><b>Pull requests (normal use case):</b> Detect changes against PR base branch</summary>

```yaml
on:
Expand All @@ -316,6 +314,52 @@ jobs:

</details>

<details>
<summary><b>Pull requests (custom use case):</b> Detect changes against tag</summary>

```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
```

</details>

<details>
<summary><b>Pull requests (custom use case):</b> Detect changes since the last commit</summary>

```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
```

</details>

<details>
<summary><b>Feature branch:</b> Detect changes against configured base branch</summary>

Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 487e02f

Please sign in to comment.