Skip to content

Commit

Permalink
Apply best practices on pull requests (#935)
Browse files Browse the repository at this point in the history
* Apply subset of OpenRewrite best practices

* Run recipes on PRs

* Correctly exclude .txt file for license header
  • Loading branch information
timtebeek authored Jan 20, 2025
1 parent 0ee3261 commit dd0093d
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 10 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/comment-pr.yml
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: '.'
57 changes: 57 additions & 0 deletions .github/workflows/receive-pr.yml
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@
<exclude>suppressions.xml</exclude>
<exclude>**/README.md</exclude>
<exclude>**/.sdkmanrc</exclude>
<exclude>LICENSE/apache-license-v2.txt</exclude>
<exclude>src/test/resources-its/**</exclude>
<exclude>src/test/resources/**</exclude>
<exclude>src/main/resources/**</exclude>
Expand Down
37 changes: 37 additions & 0 deletions rewrite.yml
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public void testMethod() {
String s = null;
assertTrue(s == null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sample;

import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

public class EmptyBlockSample {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ static boolean isOddMillis() {
return true;
}
}
}
}

0 comments on commit dd0093d

Please sign in to comment.