Skip to content

Commit e5cc4ab

Browse files
Merge pull request #227 from 1Password/vzt/fix-ok-to-test
Fork PR flow fixes
2 parents 5ccb0d8 + 36719a2 commit e5cc4ab

File tree

5 files changed

+111
-68
lines changed

5 files changed

+111
-68
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: E2E Tests
1+
name: E2E Tests [reusable]
22

33
on:
44
workflow_call:
@@ -14,7 +14,7 @@ on:
1414
required: true
1515

1616
jobs:
17-
e2e-test:
17+
run:
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Checkout code

.github/workflows/ok-to-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Write comments "/ok-to-test <hash>" on a pull request. This will emit a repository_dispatch event.
1+
# Write comments "/ok-to-test sha=<hash>" on a pull request. This will emit a repository_dispatch event.
22
name: Ok To Test
33

44
on:

.github/workflows/test-e2e-fork.yml

Lines changed: 0 additions & 56 deletions
This file was deleted.

.github/workflows/test-e2e.yml

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,97 @@ on:
1414
push:
1515
branches: [main]
1616
paths-ignore: *ignore_paths
17+
repository_dispatch:
18+
types: [ ok-to-test-command ]
1719

1820
concurrency:
19-
group: e2e-${{ github.event.pull_request.head.ref }}
21+
group: >-
22+
${{ github.event_name == 'pull_request' &&
23+
format('e2e-{0}', github.event.pull_request.head.ref) ||
24+
format('e2e-{0}', github.event.client_payload.pull_request.head.ref) }}
2025
cancel-in-progress: true # cancel previous job runs for the same branch
2126

2227
jobs:
2328
check-external-pr:
2429
runs-on: ubuntu-latest
25-
if: github.event_name == 'pull_request'
30+
outputs:
31+
condition: ${{ steps.check.outputs.condition }}
2632
steps:
2733
- name: Check if PR is from external contributor
34+
id: check
2835
run: |
29-
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
30-
echo "❌ External PR detected. This workflow requires approval from a maintainer."
31-
echo "Please ask a maintainer to run '/ok-to-test' command to trigger the fork workflow."
32-
exit 1
36+
echo "Event name: ${{ github.event_name }}"
37+
echo "Repository: ${{ github.repository }}"
38+
39+
if [ "${{ github.event_name }}" == "pull_request" ]; then
40+
# For pull_request events, check if PR is from external fork
41+
echo "PR head repo: ${{ github.event.pull_request.head.repo.full_name }}"
42+
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
43+
echo "condition=skip" >> $GITHUB_OUTPUT
44+
echo "Setting condition=skip (external fork PR creation)"
45+
else
46+
echo "condition=pr-creation-maintainer" >> $GITHUB_OUTPUT
47+
echo "Setting condition=pr-creation-maintainer (internal PR creation)"
48+
fi
49+
elif [ "${{ github.event_name }}" == "repository_dispatch" ]; then
50+
# For repository_dispatch events (ok-to-test), check if sha matches
51+
SHA_PARAM="${{ github.event.client_payload.slash_command.args.named.sha }}"
52+
PR_HEAD_SHA="${{ github.event.client_payload.pull_request.head.sha }}"
53+
54+
echo "Checking dispatch event conditions..."
55+
echo "SHA from command: $SHA_PARAM"
56+
echo "PR head SHA: $PR_HEAD_SHA"
57+
58+
if [ -n "$SHA_PARAM" ] && [[ "$PR_HEAD_SHA" == *"$SHA_PARAM"* ]]; then
59+
echo "condition=dispatch-event" >> $GITHUB_OUTPUT
60+
echo "Setting condition=dispatch-event (sha matches)"
61+
else
62+
echo "condition=skip" >> $GITHUB_OUTPUT
63+
echo "Setting condition=skip (sha does not match or empty)"
64+
fi
65+
else
66+
# Unknown event type
67+
echo "condition=skip" >> $GITHUB_OUTPUT
68+
echo "Setting condition=skip (unknown event type: ${{ github.event_name }})"
3369
fi
34-
echo "✅ Internal PR detected. Proceeding with tests."
3570
36-
e2e-test:
71+
# Run tests for:
72+
# 1. Internal PRs on pull_request events
73+
# 2. External PRs on repository_dispatch (ok-to-test) events
74+
e2e:
3775
needs: check-external-pr
38-
if: always() && (needs.check-external-pr.result == 'success' || github.event_name != 'pull_request')
76+
if: |
77+
(needs.check-external-pr.outputs.condition == 'pr-creation-maintainer')
78+
||
79+
(needs.check-external-pr.outputs.condition == 'dispatch-event')
3980
uses: ./.github/workflows/e2e-tests.yml
4081
secrets:
4182
OP_CONNECT_CREDENTIALS: ${{ secrets.OP_CONNECT_CREDENTIALS }}
4283
OP_CONNECT_TOKEN: ${{ secrets.OP_CONNECT_TOKEN }}
4384
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
85+
86+
comment-pr:
87+
needs: [check-external-pr, e2e]
88+
runs-on: ubuntu-latest
89+
if: always() && needs.check-external-pr.outputs.condition == 'dispatch-event'
90+
permissions:
91+
pull-requests: write
92+
steps:
93+
- name: Create URL to the run output
94+
id: vars
95+
run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT
96+
97+
- name: Create comment on PR
98+
uses: peter-evans/create-or-update-comment@v5
99+
with:
100+
issue-number: ${{ github.event.client_payload.pull_request.number }}
101+
body: |
102+
${{
103+
needs.e2e-test.result == 'success' && '✅ E2E tests passed.' ||
104+
needs.e2e-test.result == 'failure' && '❌ E2E tests failed.' ||
105+
'⚠️ E2E tests completed.'
106+
}}
107+
108+
[View test run output][1]
109+
110+
[1]: ${{ steps.vars.outputs.run-url }}

docs/fork-pr-testing.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Fork PR Testing Guide
2+
3+
This document explains how testing works for external pull requests from forks.
4+
5+
## Overview
6+
7+
The testing system consists of two main workflows:
8+
9+
1. **E2E Tests** (`test-e2e.yml`) - Runs automatically for internal PRs, need manual trigger on external PRs.
10+
2. **Ok To Test** (`ok-to-test.yml`) - Dispatches `repository_dispatch` event when maintainer puts the `/ok-to-test sha=<commit hash>` comment in the forked PR thread.
11+
12+
## How It Works
13+
14+
### 1. PR is created by maintainer:
15+
16+
For the PR created by maintainer `E2E Test` workflow starts automatically. The PR check will reflect the status of the job.
17+
18+
### 2. PR is created by external contributor:
19+
20+
For the PR created by external contributor `E2E Test` workflow **won't** start automatically.
21+
Maintainer should make a sanity check of the changes and run it manually by:
22+
1. Putting a comment `/ok-to-test sha=<latest commit hash>` in the PR thread.
23+
2. `E2E Test` workflow starts.
24+
3. After `E2E Test` workflow finishes, a comment with a link to the workflow, along with its status will be posted in the PR.
25+
4. Maintainer can merge PR or request the changes based on the `E2E Test` results.
26+
27+
28+
## Notes
29+
30+
- Only users with **write** permissions can trigger the `/ok-to-test` command.
31+
- External PRs are automatically detected and prevented from running e2e tests automatically.
32+
- Running e2e test on the external PR is optional. Maintainer can merge PR without running it. Maintainer decides whether it's needed to run an E2E test.

0 commit comments

Comments
 (0)