Skip to content

Commit 27b752a

Browse files
authored
Add option to run integration tests from PRs from forks (#167)
With our [first community PR](#161) (🎉 ), there was an issue that the integration tests for the PR did not run since they could not access the API token for the test user from the repo secrets. This changes the check workflows so that the tests run on the `pull_request_target` event, not on the `pull_request` event, which allows passing the secrets to the workflow even in case of forks. To prevent leaking the secrets, running the workflows on a PR from a fork requires approving the workflow run by a maintainer via an environment protection rule. I've put @vdusek, @janbuchar, @jirimoravcik and me as maintainers of that environment, so that we get notified in case of a fork PR and can approve the workflow runs. The `pull_request_target` event runs in the context of the PR base branch, not the head branch, for security reasons, so to run the tests against the PR code, we have to check it out explicitly instead of checking out the default ref.
1 parent 530403a commit 27b752a

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

.github/workflows/check_version_availability.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ jobs:
1010
if: (!startsWith(github.event.pull_request.title, 'docs:'))
1111

1212
steps:
13+
# We need to check out the head commit in case of PRs,
14+
# and the default ref otherwise (during release).
1315
- name: Checkout repository
1416
uses: actions/checkout@v4
17+
with:
18+
ref: "${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || '' }}"
1519

1620
- name: Set up Python
1721
uses: actions/setup-python@v4

.github/workflows/integration_tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ jobs:
2020
max-parallel: 1 # no concurrency on this level, to not overshoot the test user limits
2121

2222
steps:
23+
# We need to check out the head commit in case of PRs,
24+
# and the default ref otherwise (during release).
2325
- name: Checkout repository
2426
uses: actions/checkout@v4
27+
with:
28+
ref: "${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || '' }}"
2529

2630
- name: Set up Python ${{ matrix.python-version }}
2731
uses: actions/setup-python@v4

.github/workflows/lint_and_type_checks.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ jobs:
1212
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1313

1414
steps:
15+
# We need to check out the head commit in case of PRs,
16+
# and the default ref otherwise (during release).
1517
- name: Checkout repository
1618
uses: actions/checkout@v4
19+
with:
20+
ref: "${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || '' }}"
1721

1822
- name: Set up Python ${{ matrix.python-version }}
1923
uses: actions/setup-python@v4

.github/workflows/run_checks.yaml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Code quality checks
22

33
on:
4-
pull_request:
4+
pull_request_target:
55

66
jobs:
77
check_version_availability:
@@ -17,8 +17,33 @@ jobs:
1717
needs: [lint_and_type_checks]
1818
uses: ./.github/workflows/unit_tests.yaml
1919

20+
# If the PR comes from the main repo, run integration tests directly
2021
integration_tests:
22+
if: github.event.pull_request.base.repo.owner == 'apify'
2123
name: Run integration tests
2224
needs: [lint_and_type_checks, unit_tests]
2325
uses: ./.github/workflows/integration_tests.yaml
2426
secrets: inherit
27+
28+
# If the PR comes from a fork,
29+
# we need to approve running the workflow first before allowing it to run,
30+
# so that we can check for any unauthorized access to our secrets.
31+
# We need two workflow jobs for that,
32+
# because jobs calling reusable workflows can't use an environment.
33+
# The first job is a dummy job that just asks for approval to use the `fork-worklows` environment.
34+
integration_tests_fork_approve:
35+
if: github.event.pull_request.base.repo.owner != 'apify'
36+
name: Approve integration tests from fork
37+
needs: [lint_and_type_checks, unit_tests]
38+
environment: fork-pr-integration-tests
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Dummy step
42+
run: true
43+
44+
# The second job is the actual integration tests job.
45+
integration_tests_fork:
46+
name: Run integration tests from fork
47+
needs: [integration_tests_fork_approve]
48+
uses: ./.github/workflows/integration_tests.yaml
49+
secrets: inherit

.github/workflows/unit_tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ jobs:
1313
runs-on: ${{ matrix.os }}
1414

1515
steps:
16+
# We need to check out the head commit in case of PRs,
17+
# and the default ref otherwise (during release).
1618
- name: Checkout repository
1719
uses: actions/checkout@v4
20+
with:
21+
ref: "${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || '' }}"
1822

1923
- name: Set up Python ${{ matrix.python-version }}
2024
uses: actions/setup-python@v4

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## [1.5.1](../../releases/tag/v1.5.1) - Unreleased
44

5-
...
5+
### Internal changes
6+
7+
- Allowed running integration tests from PRs from forks, after maintainer approval
68

79
## [1.5.0](../../releases/tag/v1.5.0) - 2024-01-03
810

0 commit comments

Comments
 (0)