Skip to content

Commit dd4ba9f

Browse files
committed
Post code suggestions on PRs with format issues
1 parent 9f4d9d7 commit dd4ba9f

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Description: This workflow applies the formatter against the opened pull request and upload the patch.
2+
# Since this pull request receives untrusted code, we should **NOT** have any secrets in the environment.
3+
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
4+
---
5+
name: pr-format-workflow
6+
7+
on:
8+
pull_request:
9+
types: [opened, synchronize]
10+
branches:
11+
- main
12+
13+
concurrency:
14+
group: '${{ github.workflow }} @ ${{ github.ref }}'
15+
cancel-in-progress: true
16+
17+
jobs:
18+
upload-patch:
19+
runs-on: ubuntu-latest
20+
if: ${{ github.repository == 'spring-projects/spring-security' }}
21+
timeout-minutes: 10
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
ref: ${{github.event.pull_request.head.ref}}
26+
repository: ${{github.event.pull_request.head.repo.full_name}}
27+
- name: Set up gradle
28+
uses: spring-io/spring-gradle-build-action@v2
29+
with:
30+
java-version: '17'
31+
distribution: 'temurin'
32+
33+
# Capture the PR number
34+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow
35+
- name: Create pr_number.txt
36+
run: echo "${{ github.event.number }}" > pr_number.txt
37+
- uses: actions/upload-artifact@v4
38+
with:
39+
name: pr_number
40+
path: pr_number.txt
41+
- name: Remove pr_number.txt
42+
run: rm -f pr_number.txt
43+
44+
# Format code
45+
- name: Format with Gradle
46+
run: ./gradlew format
47+
48+
# Capture the diff
49+
- name: Create patch
50+
run: |
51+
git diff | tee git-diff.patch
52+
- uses: actions/upload-artifact@v4
53+
with:
54+
name: patch
55+
path: git-diff.patch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Description: This workflow is triggered when the `pr-format-workflow` completes to post suggestions on the PR.
2+
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code.
3+
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
4+
---
5+
name: pr-suggestions-workflow
6+
7+
on:
8+
workflow_run:
9+
workflows: ["pr-format-workflow"]
10+
types:
11+
- completed
12+
13+
jobs:
14+
post-suggestions:
15+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow
16+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
17+
runs-on: ubuntu-latest
18+
permissions:
19+
actions: read
20+
pull-requests: write
21+
env:
22+
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token
23+
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
timeout-minutes: 10
25+
steps:
26+
- uses: actions/checkout@v4
27+
with:
28+
ref: ${{github.event.workflow_run.head_branch}}
29+
repository: ${{github.event.workflow_run.head_repository.full_name}}
30+
31+
# Download the patch
32+
- uses: actions/download-artifact@v4
33+
with:
34+
name: patch
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
run-id: ${{ github.event.workflow_run.id }}
37+
- name: Apply patch
38+
run: |
39+
git apply git-diff.patch --allow-empty
40+
rm git-diff.patch
41+
42+
# Download the PR number
43+
- uses: actions/download-artifact@v4
44+
with:
45+
name: pr_number
46+
github-token: ${{ secrets.GITHUB_TOKEN }}
47+
run-id: ${{ github.event.workflow_run.id }}
48+
- name: Read pr_number.txt
49+
run: |
50+
PR_NUMBER=$(cat pr_number.txt)
51+
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
52+
rm pr_number.txt
53+
54+
# Post suggestions as a comment on the PR
55+
- uses: googleapis/code-suggester@v4
56+
with:
57+
command: review
58+
pull_number: ${{ env.PR_NUMBER }}
59+
git_dir: '.'

0 commit comments

Comments
 (0)