Skip to content

Commit a63b946

Browse files
authored
Enable code suggestions with openrewrite (#512)
1 parent 061eea2 commit a63b946

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

.github/workflows/comment-pr.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Description: This workflow is triggered when the `receive-pr` 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: comment-pr
6+
7+
on:
8+
workflow_run:
9+
workflows: ["receive-pr"]
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+
env:
19+
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token
20+
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
timeout-minutes: 10
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
ref: ${{github.event.workflow_run.head_branch}}
26+
repository: ${{github.event.workflow_run.head_repository.full_name}}
27+
28+
# Download the patch
29+
- uses: actions/download-artifact@v4
30+
with:
31+
name: patch
32+
github-token: ${{ secrets.GITHUB_TOKEN }}
33+
run-id: ${{ github.event.workflow_run.id }}
34+
- name: Apply patch
35+
run: |
36+
git apply git-diff.patch --allow-empty
37+
rm git-diff.patch
38+
39+
# Download the PR number
40+
- uses: actions/download-artifact@v4
41+
with:
42+
name: pr_number
43+
github-token: ${{ secrets.GITHUB_TOKEN }}
44+
run-id: ${{ github.event.workflow_run.id }}
45+
- name: Read pr_number.txt
46+
run: |
47+
PR_NUMBER=$(cat pr_number.txt)
48+
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
49+
rm pr_number.txt
50+
51+
# Post suggestions as a comment on the PR
52+
- uses: googleapis/code-suggester@v4
53+
with:
54+
command: review
55+
pull_number: ${{ env.PR_NUMBER }}
56+
git_dir: '.'

.github/workflows/receive-pr.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Description: This workflow runs OpenRewrite recipes against 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: receive-pr
6+
7+
on:
8+
pull_request:
9+
types: [opened, synchronize]
10+
branches:
11+
- master
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+
timeout-minutes: 10
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
ref: ${{github.event.pull_request.head.ref}}
25+
repository: ${{github.event.pull_request.head.repo.full_name}}
26+
- uses: actions/setup-java@v4
27+
with:
28+
java-version: '21'
29+
distribution: 'temurin'
30+
cache: 'maven'
31+
32+
# Capture the PR number
33+
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow
34+
- name: Create pr_number.txt
35+
run: echo "${{ github.event.number }}" > pr_number.txt
36+
- uses: actions/upload-artifact@v4
37+
with:
38+
name: pr_number
39+
path: pr_number.txt
40+
- name: Remove pr_number.txt
41+
run: rm -f pr_number.txt
42+
43+
# Execute recipes
44+
- name: Apply OpenRewrite recipes
45+
run: ./mvnw -Dtoolchain.skip=true -Dlicense.skip=true -DskipTests=true -P openrewrite clean install
46+
47+
# Capture the diff
48+
- name: Create patch
49+
run: |
50+
git diff | tee git-diff.patch
51+
- uses: actions/upload-artifact@v4
52+
with:
53+
name: patch
54+
path: git-diff.patch

pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@
943943
</plugins>
944944
</build>
945945
</profile>
946+
946947
<profile>
947948
<id>no-databases</id>
948949

@@ -991,6 +992,68 @@
991992
</build>
992993
</profile>
993994

995+
<profile>
996+
<id>openrewrite</id>
997+
<build>
998+
<plugins>
999+
<plugin>
1000+
<groupId>org.openrewrite.maven</groupId>
1001+
<artifactId>rewrite-maven-plugin</artifactId>
1002+
<version>5.36.0</version>
1003+
1004+
<dependencies>
1005+
<dependency>
1006+
<groupId>org.openrewrite.recipe</groupId>
1007+
<artifactId>rewrite-testing-frameworks</artifactId>
1008+
<version>2.14.0</version>
1009+
</dependency>
1010+
<dependency>
1011+
<groupId>org.openrewrite.recipe</groupId>
1012+
<artifactId>rewrite-migrate-java</artifactId>
1013+
<version>2.20.0</version>
1014+
</dependency>
1015+
</dependencies>
1016+
1017+
<executions>
1018+
<execution>
1019+
<id>tests</id>
1020+
<goals>
1021+
<goal>runNoFork</goal>
1022+
</goals>
1023+
<phase>verify</phase>
1024+
<configuration>
1025+
<exportDatatables>true</exportDatatables>
1026+
<activeRecipes>
1027+
<recipe>org.openrewrite.java.testing.hamcrest.MigrateHamcrestToAssertJ</recipe>
1028+
<recipe>org.openrewrite.java.testing.assertj.JUnitToAssertj</recipe>
1029+
<recipe>org.openrewrite.java.testing.assertj.Assertj</recipe>
1030+
<recipe>org.openrewrite.java.migrate.UpgradeToJava21</recipe>
1031+
</activeRecipes>
1032+
<exclusions>
1033+
<exclusion>**/src/main/java/**</exclusion>
1034+
<exclusion>pom.xml</exclusion>
1035+
</exclusions>
1036+
</configuration>
1037+
</execution>
1038+
<execution>
1039+
<id>sources</id>
1040+
<goals>
1041+
<goal>runNoFork</goal>
1042+
</goals>
1043+
<phase>verify</phase>
1044+
<configuration>
1045+
<exportDatatables>true</exportDatatables>
1046+
<activeRecipes>
1047+
<recipe>org.openrewrite.java.migrate.UpgradeToJava8</recipe>
1048+
</activeRecipes>
1049+
</configuration>
1050+
</execution>
1051+
</executions>
1052+
</plugin>
1053+
</plugins>
1054+
</build>
1055+
</profile>
1056+
9941057
<profile>
9951058
<id>toolchain</id>
9961059
<activation>

0 commit comments

Comments
 (0)