-
-
Notifications
You must be signed in to change notification settings - Fork 974
Step 1 (PR-G): Add grails-code-analysis convention plugin + violation aggregation #15686
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
Draft
jamesfredley
wants to merge
10
commits into
8.0.x
Choose a base branch
from
feat/grails-code-analysis-plugin
base: 8.0.x
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.
Draft
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
29a2464
Add grails-code-analysis convention plugin and violation aggregation
jdaugherty 0ca655d
Address Copilot review feedback and fix codenarcFix corruption
jdaugherty b18a523
Make JaCoCo aggregation class-exclusion filter configurable
jdaugherty d863c32
Merge remote-tracking branch 'origin/8.0.x' into feat/grails-code-ana…
jamesfredley 20d73d8
fix(ci): pin Code Analysis workflow actions to ASF-approved SHAs
jamesfredley 57ae6d6
Make code-analysis surface violations: PMD 7.25.0 + report-only CI
jamesfredley e88abad
Refactor createOrLoad method to remove project parameter
borinquenkid 876a26c
Exclude build directory from Checkstyle task sources to ignore genera…
borinquenkid 837292b
Align Spotless config with Checkstyle rules in grails-forge
borinquenkid 4149f44
Fix grails-forge code style: drop conflicting bespoke Spotless
borinquenkid 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,233 @@ | ||
| --- | ||
| name: violation-fixer | ||
| description: Guide for running, interpreting, and fixing code style and analysis violations in grails-core using GrailsCodeStylePlugin, GrailsCodeAnalysisPlugin, and GrailsViolationAggregationPlugin — covering CodeNarc, Checkstyle, PMD, SpotBugs, and JaCoCo | ||
| license: Apache-2.0 | ||
| --- | ||
| <!-- | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements; and to You under the Apache License, Version 2.0. | ||
| --> | ||
|
|
||
| ## What I Do | ||
|
|
||
| - Explain how `GrailsCodeStylePlugin`, `GrailsCodeAnalysisPlugin`, and `GrailsViolationAggregationPlugin` enforce code quality across all 60+ modules. | ||
| - Guide you through running style and analysis checks, interpreting the per-tool Markdown violation reports, and fixing each class of violation. | ||
| - Describe which tools are always-on vs. opt-in, how to configure them via Gradle properties, and which violations can be auto-fixed. | ||
|
|
||
| ## When to Use Me | ||
|
|
||
| Activate this skill when: | ||
|
|
||
| - Running `./gradlew aggregateViolations` and interpreting the resulting `*_VIOLATIONS.md` files. | ||
| - Fixing CodeNarc, Checkstyle, PMD, SpotBugs, or Spotless violations reported in those files. | ||
| - Configuring code style or analysis tools across the repo (enabling/disabling tools or adjusting rule files). | ||
| - Preparing a commit — the plugin output must be clean before merging. | ||
|
|
||
| --- | ||
|
|
||
| ## Plugin Overview | ||
|
|
||
| | Plugin | Applied to | Responsibility | | ||
| |--------|-----------|----------------| | ||
| | `org.apache.grails.gradle.grails-code-style` | Every subproject | Applies Checkstyle and CodeNarc; registers per-project `codeStyle` task; redirects XML reports to root `build/reports/codestyle/` | | ||
| | `org.apache.grails.gradle.grails-code-analysis` | Every subproject | Applies PMD and SpotBugs (both opt-in); registers per-project `codeAnalysis` task; redirects XML reports to root `build/reports/codeanalysis/` | | ||
| | `org.apache.grails.gradle.grails-jacoco` | Every subproject | Applies JaCoCo; wires `jacocoTestReport` to run after each `test` task | | ||
| | `org.apache.grails.gradle.grails-violation-aggregation` | **Root project only** | Registers `aggregateViolations` and `aggregateJacocoCoverage` tasks; writes Markdown summaries to `build/reports/violations/` | | ||
|
|
||
| --- | ||
|
|
||
| ## Key Tasks | ||
|
|
||
| | Task | Scope | Description | | ||
| |------|-------|-------------| | ||
| | `./gradlew codeStyle` | per-project | Runs Checkstyle and CodeNarc for that project | | ||
| | `./gradlew codeAnalysis` | per-project | Runs PMD and/or SpotBugs for that project (when enabled) | | ||
| | `./gradlew aggregateViolations` | root | Runs all checks across every module, then writes `*_VIOLATIONS.md` to `build/reports/violations/` | | ||
| | `./gradlew aggregateJacocoCoverage` | root | Runs JaCoCo reports across every module, then writes `JACOCO_COVERAGE.md` to `build/reports/violations/` | | ||
| | `./gradlew codenarcFix` | per-project | Auto-fixes a subset of CodeNarc violations | | ||
|
|
||
| ### Quick commands | ||
|
|
||
| ```bash | ||
| # Check a single module (style only) | ||
| ./gradlew :grails-core:codeStyle | ||
|
|
||
| # Check a single module (analysis — must be enabled via properties) | ||
| ./gradlew :grails-core:codeAnalysis -Pgrails.codeanalysis.enabled.pmd=true | ||
|
|
||
| # Full multi-module check + report | ||
| ./gradlew aggregateViolations | ||
|
|
||
| # Include test sources in style checks | ||
| ./gradlew aggregateViolations -Pgrails.codestyle.enabled.tests=true | ||
|
|
||
| # Include test sources in analysis | ||
| ./gradlew aggregateViolations -Pgrails.codeanalysis.enabled.tests=true | ||
|
|
||
| # Ignore failures (collect reports without failing the build) | ||
| ./gradlew aggregateViolations -Pgrails.codestyle.ignoreFailures=true -Pgrails.codeanalysis.ignoreFailures=true | ||
|
|
||
| # Auto-fix some CodeNarc violations before running checks | ||
| ./gradlew codenarcFix codeStyle | ||
|
|
||
| # JaCoCo coverage report | ||
| ./gradlew aggregateJacocoCoverage | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Output Files | ||
|
|
||
| After running `aggregateViolations`, these files appear under `build/reports/violations/` in the **root project build directory**: | ||
|
|
||
| | File | Tool | Always generated | | ||
| |------|------|-----------------| | ||
| | `build/reports/violations/CODENARC_VIOLATIONS.md` | CodeNarc | Yes | | ||
| | `build/reports/violations/CHECKSTYLE_VIOLATIONS.md` | Checkstyle | Yes | | ||
| | `build/reports/violations/PMD_VIOLATIONS.md` | PMD | Yes — contains `No violations found!` when PMD is disabled | | ||
| | `build/reports/violations/SPOTBUGS_VIOLATIONS.md` | SpotBugs | Yes — contains `No violations found!` when SpotBugs is disabled | | ||
|
|
||
| After running `aggregateJacocoCoverage`: | ||
|
|
||
| | File | Tool | Generated | | ||
| |------|------|-----------| | ||
| | `build/reports/violations/JACOCO_COVERAGE.md` | JaCoCo | Only when at least one subproject has a JaCoCo CSV report | | ||
|
|
||
| All reports are inside `build/` and are excluded from version control via `.gitignore`. A clean run produces `No violations found! 🎉` in each style file. **The build must be clean before committing.** | ||
|
|
||
| Each file is a Markdown table grouped by module, with columns: **Class**, **Tool**, **Violation**, **Line**, **Message**. | ||
|
|
||
| --- | ||
|
|
||
| ## Tool Details | ||
|
|
||
| ### CodeNarc (Groovy — always enabled) | ||
|
|
||
| Rule file: `build/codestyle/codenarc/codenarc.groovy` (generated by the plugin during setup; not intended to be edited directly). | ||
|
|
||
| Most common violations and how to fix them: | ||
|
|
||
| | Rule | Fix | | ||
| |------|-----| | ||
| | `UnnecessaryGString` | Replace `"plain string"` with `'plain string'` | | ||
| | `UnnecessarySemicolon` | Remove trailing `;` | | ||
| | `SpaceBeforeOpeningBrace` | Add space before `{` → `method() {` | | ||
| | `SpaceAroundMapEntryColon` | `[key: value]` not `[key:value]` | | ||
| | `ConsecutiveBlankLines` | Collapse 3+ blank lines to 2 | | ||
| | `ClassStartsWithBlankLine` | Remove blank line right after `class Foo {` | | ||
| | `NoWildcardImports` | Expand `import org.foo.*` to explicit imports | | ||
| | `UnusedImport` | Remove imports not referenced in the file | | ||
| | `MethodName` | Method names must be camelCase (not `snake_case`) | | ||
| | `VariableName` | Variable names must be camelCase | | ||
| | `LineLength` | Keep lines ≤ 200 chars (default) | | ||
|
|
||
| Auto-fixable via `codenarcFix`: `ClassStartsWithBlankLine`, `SpaceAroundMapEntryColon`, `UnnecessaryGString`, `UnnecessarySemicolon`, `SpaceBeforeOpeningBrace`, `ConsecutiveBlankLines`. | ||
|
|
||
| ### Checkstyle (Java — always enabled) | ||
|
|
||
| Rule file: `build/codestyle/checkstyle/checkstyle.xml`. | ||
|
|
||
| Common violations: | ||
|
|
||
| | Rule | Fix | | ||
| |------|-----| | ||
| | `ImportOrder` | Re-order imports: `java|javax`, then `groovy`, then `jakarta`, then blank, then `io.spring|org.springframework`, then `grails|org.apache.grails|org.grails`, then static imports | | ||
| | `AvoidStarImport` | Use explicit class imports | | ||
| | `UnusedImports` | Remove unused imports | | ||
| | `WhitespaceAround` | Add spaces around operators and keywords | | ||
| | `NeedBraces` | Add `{}` to single-statement `if`/`for`/`while` | | ||
| | `FileTabCharacter` | Replace tabs with 4 spaces | | ||
| | `NewlineAtEndOfFile` | Ensure file ends with `\n` | | ||
|
|
||
| ### PMD (Java/Groovy — opt-in) | ||
|
|
||
| Enable: `-Pgrails.codeanalysis.enabled.pmd=true` | ||
|
|
||
| Rule file: `build/codeanalysis/pmd/pmd.xml`. | ||
|
|
||
| ### SpotBugs (Java bytecode — opt-in) | ||
|
|
||
| Enable: `-Pgrails.codeanalysis.enabled.spotbugs=true` | ||
|
|
||
| Runs at `Effort.MAX` / `Confidence.HIGH`. Only high-confidence bugs are reported. | ||
|
|
||
| ### Spotless (Java auto-formatting — opt-in) | ||
|
|
||
| Enable: `-Pgrails.codestyle.enabled.spotless=true` | ||
|
|
||
| Uses Palantir Java Format. Can auto-fix by running: | ||
| ```bash | ||
| ./gradlew spotlessApply | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Configuration Properties | ||
|
|
||
| All properties can be set in `gradle.properties` or passed as `-P` flags: | ||
|
|
||
| ### `grails-code-style` plugin (Checkstyle + CodeNarc) | ||
|
|
||
| | Property | Default | Description | | ||
| |----------|---------|-------------| | ||
| | `grails.codestyle.enabled.checkstyle` | `true` | Enable Checkstyle | | ||
| | `grails.codestyle.enabled.codenarc` | `true` | Enable CodeNarc | | ||
| | `grails.codestyle.enabled.spotless` | `false` | Enable Spotless | | ||
| | `grails.codestyle.enabled.tests` | `false` | Also check test source sets | | ||
| | `grails.codestyle.ignoreFailures` | `false` | Collect reports without failing build | | ||
| | `grails.codestyle.codenarc.fix` | `false` | Run `codenarcFix` before CodeNarc tasks | | ||
| | `grails.codestyle.dir.checkstyle` | (auto) | Custom path to Checkstyle config dir | | ||
| | `grails.codestyle.dir.codenarc` | (auto) | Custom path to CodeNarc config dir | | ||
| | `skipCodeStyle` | unset | If present, all style tasks are skipped | | ||
|
|
||
| ### `grails-code-analysis` plugin (PMD + SpotBugs) | ||
|
|
||
| | Property | Default | Description | | ||
| |----------|---------|-------------| | ||
| | `grails.codeanalysis.enabled.pmd` | `false` | Enable PMD | | ||
| | `grails.codeanalysis.enabled.spotbugs` | `false` | Enable SpotBugs | | ||
| | `grails.codeanalysis.enabled.tests` | `false` | Also analyse test source sets | | ||
| | `grails.codeanalysis.ignoreFailures` | `false` | Collect reports without failing build | | ||
| | `grails.codeanalysis.dir.pmd` | (auto) | Custom path to PMD config dir | | ||
| | `skipCodeStyle` | unset | If present, all analysis tasks are also skipped | | ||
|
|
||
| --- | ||
|
|
||
| ## Fixing Violations Workflow | ||
|
|
||
| 1. Run `./gradlew aggregateViolations -Pgrails.codestyle.ignoreFailures=true -Pgrails.codeanalysis.ignoreFailures=true` | ||
| 2. Open `build/reports/violations/CODENARC_VIOLATIONS.md` and `build/reports/violations/CHECKSTYLE_VIOLATIONS.md` to see all issues by module | ||
| 3. For CodeNarc, run `./gradlew codenarcFix` to auto-fix what it can | ||
| 4. Fix remaining violations manually using the table above | ||
| 5. Re-run `./gradlew aggregateViolations` and confirm files contain `No violations found! 🎉` | ||
| 6. The reports are inside `build/` and do not need to be deleted before committing | ||
|
|
||
| --- | ||
|
|
||
| ## Reports Directory Structure | ||
|
|
||
| All XML reports are consolidated at: | ||
| ``` | ||
| build/reports/codestyle/ ← XML inputs for style aggregation | ||
| ├── checkstyle/ | ||
| │ ├── grails-core-checkstyleMain.xml | ||
| │ ├── grails-web-mvc-checkstyleMain.xml | ||
| │ └── ... | ||
| └── codenarc/ | ||
| ├── grails-core-codenarcMain.xml | ||
| └── ... | ||
|
|
||
| build/reports/codeanalysis/ ← XML inputs for analysis aggregation (if enabled) | ||
| ├── pmd/ | ||
| └── spotbugs/ | ||
|
|
||
| build/reports/violations/ ← Markdown summaries written by aggregateViolations | ||
| ├── CODENARC_VIOLATIONS.md | ||
| ├── CHECKSTYLE_VIOLATIONS.md | ||
| ├── PMD_VIOLATIONS.md | ||
| ├── SPOTBUGS_VIOLATIONS.md | ||
| └── JACOCO_COVERAGE.md ← written by aggregateJacocoCoverage | ||
| ``` | ||
|
|
||
| The module name is derived from the filename: everything before the last `-` (e.g. `grails-core-checkstyleMain.xml` → module `grails-core`). | ||
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,97 @@ | ||
| # 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 | ||
| # | ||
| # https://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. | ||
|
|
||
| name: "Code Analysis" | ||
| on: | ||
| push: | ||
| branches: | ||
| - '[0-9]+.[0-9]+.x' | ||
| - '8.0.x-hibernate7.*' | ||
| pull_request: | ||
| workflow_dispatch: | ||
| # queue jobs and only allow 1 run per branch due to the likelihood of hitting GitHub resource limits | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.ref }} | ||
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
| jobs: | ||
| check_core_projects: | ||
| name: "Core Projects" | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - name: "🌐 Output Agent IP" # in the event RAO blocks this agent, this can be used to debug it | ||
| run: curl -s https://api.ipify.org | ||
| - name: "📥 Checkout repository" | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
| - name: "☕️ Setup JDK" | ||
| uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5.2.0 | ||
| with: | ||
| distribution: liberica | ||
| java-version: 21 | ||
| - name: "🐘 Setup Gradle" | ||
| uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0 | ||
| with: | ||
| cache-provider: basic # 'basic' uses the MIT-licensed, open-source cache provider; the default 'enhanced' provider (v6+) is proprietary (Gradle commercial Terms of Use) | ||
| develocity-access-key: ${{ secrets.DEVELOCITY_ACCESS_KEY }} | ||
| - name: "🔎 Check Core Projects" | ||
| run: ./gradlew aggregateAnalysisViolations --continue -Pgrails.codeanalysis.enabled.pmd=true -Pgrails.codeanalysis.enabled.spotbugs=true -Pgrails.codeanalysis.ignoreFailures=true | ||
| - name: "📤 Upload Reports" | ||
| if: always() | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: core-reports | ||
| path: build/reports/violations/ | ||
| - name: "📋 Publish Code Analysis Report in Job Summary" | ||
| if: always() | ||
| run: | | ||
| echo "## 🔎 Code Analysis Report - Core Projects" >> $GITHUB_STEP_SUMMARY | ||
| for report in PMD_VIOLATIONS.md SPOTBUGS_VIOLATIONS.md; do | ||
| file="build/reports/violations/$report" | ||
| [ -f "$file" ] && cat "$file" >> $GITHUB_STEP_SUMMARY || true | ||
| done | ||
| check_gradle_plugin_projects: | ||
| name: "Gradle Plugin Projects" | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - name: "🌐 Output Agent IP" # in the event RAO blocks this agent, this can be used to debug it | ||
| run: curl -s https://api.ipify.org | ||
| - name: "📥 Checkout repository" | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | ||
| - name: "☕️ Setup JDK" | ||
| uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5.2.0 | ||
| with: | ||
| distribution: liberica | ||
| java-version: 21 | ||
| - name: "🐘 Setup Gradle" | ||
| uses: gradle/actions/setup-gradle@50e97c2cd7a37755bbfafc9c5b7cafaece252f6e # v6.1.0 | ||
| with: | ||
| cache-provider: basic # 'basic' uses the MIT-licensed, open-source cache provider; the default 'enhanced' provider (v6+) is proprietary (Gradle commercial Terms of Use) | ||
| develocity-access-key: ${{ secrets.DEVELOCITY_ACCESS_KEY }} | ||
| - name: "🔎 Check Gradle Plugin Projects" | ||
| working-directory: grails-gradle | ||
| run: ./gradlew aggregateAnalysisViolations --continue -Pgrails.codeanalysis.enabled.pmd=true -Pgrails.codeanalysis.enabled.spotbugs=true -Pgrails.codeanalysis.ignoreFailures=true | ||
| - name: "📤 Upload Reports" | ||
| if: always() | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: gradle-plugin-reports | ||
| path: grails-gradle/build/reports/violations/ | ||
| - name: "📋 Publish Code Analysis Report in Job Summary" | ||
| if: always() | ||
| run: | | ||
| echo "## 🔎 Code Analysis Report - Gradle Plugin Projects" >> $GITHUB_STEP_SUMMARY | ||
| for report in PMD_VIOLATIONS.md SPOTBUGS_VIOLATIONS.md; do | ||
| file="grails-gradle/build/reports/violations/$report" | ||
| [ -f "$file" ] && cat "$file" >> $GITHUB_STEP_SUMMARY || true | ||
| done |
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.
Uh oh!
There was an error while loading. Please reload this page.