-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply best practices on pull requests (#935)
* Apply subset of OpenRewrite best practices * Run recipes on PRs * Correctly exclude .txt file for license header
- Loading branch information
Showing
14 changed files
with
162 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Description: This workflow is triggered when the `receive-pr` workflow completes to post suggestions on the PR. | ||
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code. | ||
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
--- | ||
name: comment-pr | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["receive-pr"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
post-suggestions: | ||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
runs-on: ubuntu-latest | ||
env: | ||
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token | ||
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{github.event.workflow_run.head_branch}} | ||
repository: ${{github.event.workflow_run.head_repository.full_name}} | ||
|
||
# Download the patch | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: patch | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
run-id: ${{ github.event.workflow_run.id }} | ||
- name: Apply patch | ||
run: | | ||
git apply git-diff.patch --allow-empty | ||
rm git-diff.patch | ||
# Download the PR number | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: pr_number | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
run-id: ${{ github.event.workflow_run.id }} | ||
- name: Read pr_number.txt | ||
run: | | ||
PR_NUMBER=$(cat pr_number.txt) | ||
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | ||
rm pr_number.txt | ||
# Post suggestions as a comment on the PR | ||
- uses: googleapis/code-suggester@v4 | ||
with: | ||
command: review | ||
pull_number: ${{ env.PR_NUMBER }} | ||
git_dir: '.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Description: This workflow runs OpenRewrite recipes against opened pull request and upload the patch. | ||
# Since this pull request receives untrusted code, we should **NOT** have any secrets in the environment. | ||
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
--- | ||
name: receive-pr | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: '${{ github.workflow }} @ ${{ github.ref }}' | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
upload-patch: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{github.event.pull_request.head.ref}} | ||
repository: ${{github.event.pull_request.head.repo.full_name}} | ||
- uses: actions/setup-java@v4 | ||
with: | ||
java-version: '21' | ||
distribution: 'temurin' | ||
cache: 'maven' | ||
|
||
# Capture the PR number | ||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow | ||
- name: Create pr_number.txt | ||
run: echo "${{ github.event.number }}" > pr_number.txt | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: pr_number | ||
path: pr_number.txt | ||
- name: Remove pr_number.txt | ||
run: rm -f pr_number.txt | ||
|
||
# Execute recipes | ||
- name: Apply OpenRewrite recipes | ||
run: | | ||
./mvnw org.openrewrite.maven:rewrite-maven-plugin:LATEST:run \ | ||
-Drewrite.activeRecipes=org.openrewrite.recipes.OpenRewriteBestPracticesSubset \ | ||
-Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-static-analysis:RELEASE | ||
# Capture the diff | ||
- name: Create patch | ||
run: | | ||
git diff | tee git-diff.patch | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: patch | ||
path: git-diff.patch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# | ||
# Copyright 2020 the original author or authors. | ||
# <p> | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# <p> | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# <p> | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
#./mvnw org.openrewrite.maven:rewrite-maven-plugin:LATEST:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-static-analysis:RELEASE -Drewrite.activeRecipes=org.openrewrite.recipes.OpenRewriteBestPracticesSubset | ||
--- | ||
type: specs.openrewrite.org/v1beta/recipe | ||
name: org.openrewrite.recipes.OpenRewriteBestPracticesSubset | ||
displayName: OpenRewrite best practices | ||
description: Best practices for OpenRewrite recipe development. | ||
recipeList: | ||
- org.openrewrite.java.OrderImports: | ||
removeUnused: true | ||
- org.openrewrite.java.format.EmptyNewlineAtEndOfFile | ||
- org.openrewrite.java.format.RemoveTrailingWhitespace | ||
- org.openrewrite.maven.BestPractices | ||
# - org.openrewrite.staticanalysis.OperatorWrap: | ||
# wrapOption: EOL | ||
# - org.openrewrite.staticanalysis.CommonStaticAnalysis | ||
# - org.openrewrite.staticanalysis.CompareEnumsWithEqualityOperator | ||
# - org.openrewrite.staticanalysis.MissingOverrideAnnotation | ||
# - org.openrewrite.staticanalysis.RemoveSystemOutPrintln | ||
# - org.openrewrite.staticanalysis.RemoveUnusedLocalVariables | ||
# - org.openrewrite.staticanalysis.RemoveUnusedPrivateFields | ||
# - org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ public void testMethod() { | |
String s = null; | ||
assertTrue(s == null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
.../openrewrite/maven/RewriteRunIT/single_project/src/main/java/sample/EmptyBlockSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ static boolean isOddMillis() { | |
return true; | ||
} | ||
} | ||
} | ||
} |