Skip to content

Commit 6eac3f1

Browse files
committed
workflows: move testing external PRs to own workflow
1 parent 7afed1b commit 6eac3f1

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test external PR
2+
on:
3+
push:
4+
branches: ["main"]
5+
pull_request_target:
6+
branches: ["main"]
7+
8+
jobs:
9+
go_test:
10+
if: ${{ github.event_name == 'pull_request_target' }}
11+
runs-on: ubuntu-latest
12+
environment: e2e # request confirmation
13+
steps:
14+
- uses: actions/[email protected]
15+
with:
16+
fetch-depth: 0 # for sonarcloud
17+
repository: ${{ github.event.pull_request.head.repo.full_name }}
18+
ref: ${{ github.event.pull_request.head.ref }}
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version-file: go.mod
22+
- run: "make verify"
23+
- run: "make test"
24+
- uses: SonarSource/[email protected]
25+
with:
26+
args: >
27+
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
28+
-Dsonar.scm.revision=${{ github.event.pull_request.head.sha }}
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/test.yml

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,50 @@ on:
33
push:
44
branches: ["main"]
55
pull_request_target:
6-
types: ["opened", "synchronize", "reopened"]
76
branches: ["main"]
87

98
jobs:
109
go_test:
10+
if: ${{ github.event_name == 'push' }}
1111
runs-on: ubuntu-latest
1212
steps:
13-
# If triggered by a push to **our** repository, we can directly checkout the code.
14-
- name: Checkout branch ${{ github.ref }}
15-
if: ${{ github.event_name == 'push' }}
16-
uses: actions/[email protected]
17-
with:
18-
# Disabling shallow clone is recommended for improving relevancy of reporting
19-
fetch-depth: 0
20-
21-
# If triggered by a PR, we have to check out the PR's source
22-
- name: Checkout (preview) merge commit for PR ${{ github.event.pull_request.number }}
23-
if: ${{ github.event_name == 'pull_request_target' }}
24-
uses: actions/[email protected]
13+
- uses: actions/[email protected]
2514
with:
26-
# Disabling shallow clone is recommended for improving relevancy of reporting
27-
fetch-depth: 0
28-
repository: ${{ github.event.pull_request.head.repo.full_name }}
29-
ref: ${{ github.event.pull_request.head.ref }}
30-
15+
fetch-depth: 0 # for sonarcloud
3116
- uses: actions/setup-go@v5
3217
with:
3318
go-version-file: go.mod
34-
35-
- name: Verify
36-
run: "make verify"
37-
38-
- name: Run tests
39-
run: "make test"
40-
41-
- name: SonarCloud Scan ${{ github.ref }}
42-
uses: SonarSource/[email protected]
19+
- run: "make verify"
20+
- run: "make test"
21+
- uses: SonarSource/[email protected]
4322
if: ${{ github.event_name == 'push' }}
4423
env:
45-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4625
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
4726

48-
# If triggered by a PR, we have to use the PR's source
49-
- name: SonarCloud Scan (preview) merge commit for PR ${{ github.event.pull_request.number }}
50-
uses: SonarSource/[email protected]
51-
if: ${{ github.event_name == 'pull_request_target' }}
27+
ext_pr:
28+
if: ${{ github.event_name == 'pull_request_target' }}
29+
runs-on: ubuntu-latest
30+
permissions:
31+
pull-requests: write
32+
steps:
33+
- uses: actions/[email protected]
5234
with:
53-
args: >
54-
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
55-
-Dsonar.scm.revision=${{ github.event.pull_request.head.sha }}
56-
env:
57-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
58-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
35+
fetch-depth: 2 # for diff
36+
repository: ${{ github.event.pull_request.head.repo.full_name }}
37+
ref: ${{ github.event.pull_request.head.ref }}
38+
39+
- name: generate comment message
40+
run: |
41+
printf '%s\n%s\n\n' '### External PR' 'Test runs on external PRs require manual approval.' >"${{ runner.temp }}/msg"
42+
git diff --name-only -z HEAD^1 HEAD | grep -Evz '\.go$|^docs/' | tr '\0' '\n' >"${{ runner.temp }}/diff"
43+
if [ -s '${{ runner.temp }}'/diff ]; then
44+
echo '**Note:** This PR changes the following non-go, non-docs files:' >>"${{ runner.temp }}/msg"
45+
cat "${{ runner.temp }}/diff" >>"${{ runner.temp }}/msg"
46+
fi
47+
48+
- uses: thollander/actions-comment-pull-request@v3
49+
with:
50+
comment_tag: test
51+
mode: recreate
52+
file-path: ${{ runner.temp }}/msg

0 commit comments

Comments
 (0)