Repository navigation
Add checkstyle auto-fix tooling #9017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sercuzz8
wants to merge
9
commits into
apache:master
Choose a base branch
from
sercuzz8:sketch-solution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ae1027
Add OpenRewrite + Groovy checkstyle auto-fix tooling (run on demand)
sercuzz8 cf1554b
Add a workflow_dispatch GitHub Action to run the checkstyle auto-fix …
sercuzz8 4a31d24
add checkstyle step before run and upload artifact after
sercuzz8 73e7346
open new branch per autofix run
sercuzz8 3e82d9f
silent no-op in case of missing plugin
sercuzz8 2aed910
accept both header forms (no legal gain in contrary)
sercuzz8 833546c
do not override accepted tokens
sercuzz8 eb0b731
preserve octal escapes
sercuzz8 9ed670f
Reinstate pom
sercuzz8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,121 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You 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 | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # 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. | ||
|
|
||
| # Runs the checkstyle auto-fix (storm-checkstyle/README.md) on one module, pushes the result to a | ||
| # new branch checkstyle-fix/<module>-<run id>-<attempt> (one per execution) and opens a pull request | ||
| # from it against the branch the workflow ran on. The Checkstyle report of the module before and | ||
| # after the fix is attached to the run as an artifact; the violations left after the fix are listed | ||
| # in the pull request. | ||
| name: Checkstyle auto-fix | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| module: | ||
| description: 'Maven module to fix, as its path in the repository (e.g. storm-client or external/storm-hdfs)' | ||
| required: true | ||
| type: string | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| autofix: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 120 | ||
| env: | ||
| MODULE: ${{ inputs.module }} | ||
| steps: | ||
| - name: Validate the module input | ||
| run: | | ||
| if ! [[ "$MODULE" =~ ^[A-Za-z0-9._-]+(/[A-Za-z0-9._-]+)*$ ]] || [[ "$MODULE" == *..* ]]; then | ||
| echo "::error::'$MODULE' is not a valid module path" | ||
| exit 1 | ||
| fi | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| - name: Check that the module exists | ||
| run: test -f "$MODULE/pom.xml" || { echo "::error::$MODULE/pom.xml not found"; exit 1; } | ||
| - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 | ||
| with: | ||
| path: ~/.m2/repository | ||
| key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-maven- | ||
| - name: Set up JDK 25 | ||
| uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1 | ||
| with: | ||
| distribution: temurin | ||
| java-version: 25 | ||
| - name: Check that the module declares the auto-fix plugins | ||
| run: | | ||
| # without them the auto-fix executions would run with no configuration and silently fix nothing | ||
| mvn --batch-mode --quiet -pl "$MODULE" help:effective-pom -Doutput="$RUNNER_TEMP/effective-pom.xml" | ||
| python3 - "$RUNNER_TEMP/effective-pom.xml" <<'EOF' | ||
| import sys | ||
| import xml.etree.ElementTree as ET | ||
| ns = {'m': 'http://maven.apache.org/POM/4.0.0'} | ||
| plugins = {p.text for p in ET.parse(sys.argv[1]).getroot().findall('m:build/m:plugins/m:plugin/m:artifactId', ns)} | ||
| missing = {'rewrite-maven-plugin', 'gmavenplus-plugin'} - plugins | ||
| if missing: | ||
| sys.exit(f"::error::the module does not declare {', '.join(sorted(missing))} in its build plugins") | ||
| EOF | ||
| - name: Build the module and the modules it depends on (the Groovy stage resolves its classpath) | ||
| run: mvn --batch-mode install -DskipTests -Dcheckstyle.skip=true -pl "$MODULE" -am | ||
| - name: Checkstyle report before the fix | ||
| run: ./dev-tools/gitact/checkstyle-report.sh before | ||
| - name: Auto-fix the module (OpenRewrite, Groovy, OpenRewrite re-indent) | ||
| run: | | ||
| mvn --batch-mode -pl "$MODULE" -Dcheckstyle.skip=true \ | ||
| org.openrewrite.maven:rewrite-maven-plugin:run@checkstyle-autofix-openrewrite \ | ||
| org.codehaus.gmavenplus:gmavenplus-plugin:execute@checkstyle-autofix \ | ||
| org.openrewrite.maven:rewrite-maven-plugin:run@checkstyle-reindent-openrewrite | ||
| - name: Normalize the license headers | ||
| run: mvn --batch-mode -pl "$MODULE" -Dcheckstyle.skip=true org.codehaus.gmavenplus:gmavenplus-plugin:execute@normalize-license-headers | ||
| - name: Checkstyle report after the fix | ||
| run: ./dev-tools/gitact/checkstyle-report.sh after | ||
| - name: Upload the Checkstyle reports | ||
| if: ${{ !cancelled() }} | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: checkstyle-reports | ||
| path: ${{ runner.temp }}/checkstyle-reports/ | ||
| if-no-files-found: ignore | ||
| - name: Push the result to a new branch and open a pull request | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: | | ||
| # one branch per execution, so a run never overwrites the branch of an earlier one (or its open PR) | ||
| BRANCH="checkstyle-fix/$MODULE-$GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT" | ||
| # only the module itself (build output is git-ignored), never the tooling files | ||
| git add -A -- "$MODULE" | ||
| if git diff --cached --quiet; then | ||
| echo "Nothing to fix in $MODULE" | tee -a "$GITHUB_STEP_SUMMARY" | ||
| exit 0 | ||
| fi | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
| git checkout -b "$BRANCH" | ||
| git commit -m "Checkstyle auto-fix of $MODULE" | ||
| git push origin "$BRANCH" | ||
| REPORTS="$RUNNER_TEMP/checkstyle-reports" | ||
| { | ||
| echo "Checkstyle auto-fix of \`$MODULE\`, made by [this workflow run]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID) (the full reports are in its \`checkstyle-reports\` artifact)." | ||
| echo | ||
| cat "$REPORTS/before.md" "$REPORTS/after.md" | ||
| } > "$RUNNER_TEMP/pr-body.md" | ||
| PR=$(gh pr create --base "$GITHUB_REF_NAME" --head "$BRANCH" \ | ||
| --title "Checkstyle auto-fix of $MODULE" --body-file "$RUNNER_TEMP/pr-body.md") | ||
| echo "Opened $PR from $BRANCH" | tee -a "$GITHUB_STEP_SUMMARY" | ||
Empty file.
This file contains hidden or 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,11 @@ | ||
| --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED | ||
| --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED | ||
| --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED | ||
| --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED |
This file contains hidden or 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,61 @@ | ||
| #!/bin/bash | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You 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 | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # 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. | ||
|
|
||
| # Runs the build's Checkstyle check on $MODULE and saves its report as | ||
| # $RUNNER_TEMP/checkstyle-reports/<label>.xml (and the console output as <label>.txt), plus | ||
| # <label>.md: the violation counts and a table of the violations per check, which is also added to | ||
| # the job summary. Used by .github/workflows/checkstyle-autofix.yaml. | ||
| # The check failing on violations is expected: only a missing report fails this script. | ||
|
|
||
| set -u | ||
|
|
||
| LABEL=$1 | ||
| OUT_DIR="$RUNNER_TEMP/checkstyle-reports" | ||
| REPORT="$MODULE/target/checkstyle-violation.xml" | ||
| mkdir -p "$OUT_DIR" | ||
| rm -f "$REPORT" | ||
|
|
||
| mvn --batch-mode -pl "$MODULE" org.apache.maven.plugins:maven-checkstyle-plugin:check@validate \ | ||
| > "$OUT_DIR/$LABEL.txt" 2>&1 | ||
|
|
||
| if [[ ! -f "$REPORT" ]]; then | ||
| tail -n 50 "$OUT_DIR/$LABEL.txt" | ||
| echo "::error::Checkstyle did not write $REPORT" | ||
| exit 1 | ||
| fi | ||
| cp "$REPORT" "$OUT_DIR/$LABEL.xml" | ||
|
|
||
| python3 - "$REPORT" "$LABEL" > "$OUT_DIR/$LABEL.md" <<'EOF' | ||
| import collections | ||
| import sys | ||
| import xml.etree.ElementTree as ET | ||
|
|
||
| report, label = sys.argv[1:] | ||
| violations = list(ET.parse(report).getroot().iter('error')) | ||
| severities = collections.Counter(v.get('severity') for v in violations) | ||
| print(f"### Checkstyle {label} the fix: {severities['error']} errors, {severities['warning']} warnings") | ||
| if violations: | ||
| print() | ||
| print('| Check | Severity | Violations |') | ||
| print('|---|---|---|') | ||
| checks = collections.Counter((v.get('source').rsplit('.', 1)[-1].removesuffix('Check'), v.get('severity')) | ||
| for v in violations) | ||
| for (check, severity), count in checks.most_common(): | ||
| print(f'| {check} | {severity} | {count} |') | ||
| print() | ||
| EOF | ||
| cat "$OUT_DIR/$LABEL.md" | ||
| cat "$OUT_DIR/$LABEL.md" >> "$GITHUB_STEP_SUMMARY" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I advice to execute a checkstyle before and after including the results inside the job artifact of GitHub action.