From 2adb35a19330794ef31a735cf6332112594a8c2b Mon Sep 17 00:00:00 2001 From: SeanErn Date: Sun, 22 Dec 2024 20:53:03 -0500 Subject: [PATCH] use spotless --- .github/workflows/check_javadocs.yml | 100 +++++++++++++++------------ 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/.github/workflows/check_javadocs.yml b/.github/workflows/check_javadocs.yml index 8041221..3c2ac59 100644 --- a/.github/workflows/check_javadocs.yml +++ b/.github/workflows/check_javadocs.yml @@ -2,9 +2,8 @@ name: Javadoc Validation on: push: - branches: [ '*' ] pull_request: - branches: [ master ] + branches: [ '*' ] workflow_dispatch: inputs: createIssue: @@ -25,60 +24,76 @@ jobs: with: java-version: '17' distribution: 'temurin' - cache: maven - - name: Check Javadoc presence with Checkstyle + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + + - name: Add Spotless Config run: | - cat > checkstyle.xml << 'EOF' - - - - - - - - - - - - - - - - - - - - - - - EOF + cat >> build.gradle << 'EOF' + + spotless { + java { + // Enforce Javadoc on public methods and classes + custom 'Enforce Javadoc', { + def publicMethodPattern = ~/^\s*public\s+(?!class)(?!interface)(?!enum)/ + def publicClassPattern = ~/^\s*public\s+(class|interface|enum)/ - mvn checkstyle:check@validate-javadoc -Dcheckstyle.config.location=checkstyle.xml + if (it.contains("public")) { + def lines = it.split('\n') + def issues = [] + + for (i = 0; i < lines.size(); i++) { + def line = lines[i] + if (publicMethodPattern.matcher(line).find()) { + // Look for Javadoc before method + if (i == 0 || !lines[i-1].trim().endsWith('*/')) { + issues.add("Missing Javadoc for public method at line ${i+1}") + } + } else if (publicClassPattern.matcher(line).find()) { + // Look for Javadoc before class + if (i == 0 || !lines[i-1].trim().endsWith('*/')) { + issues.add("Missing Javadoc for public class/interface/enum at line ${i+1}") + } + } + } + + if (!issues.isEmpty()) { + throw new IllegalStateException(issues.join('\n')) + } + } + return it + } + } + } + EOF + + - name: Run Spotless Check + id: spotless + continue-on-error: true + run: ./gradlew spotlessCheck - name: Generate Javadoc Report - if: failure() - run: mvn javadoc:javadoc + if: steps.spotless.outcome == 'failure' + run: ./gradlew javadoc - name: Upload Javadoc Report - if: failure() + if: steps.spotless.outcome == 'failure' uses: actions/upload-artifact@v3 with: name: javadoc-report - path: target/site/apidocs/ + path: build/docs/javadoc/ - name: Create Issue on Failure - if: failure() + if: steps.spotless.outcome == 'failure' uses: actions/github-script@v7 with: script: | const fs = require('fs'); - const checkstyleOutput = fs.readFileSync('target/checkstyle-result.xml', 'utf8'); - // Parse the checkstyle XML to get violation details - const violations = checkstyleOutput.match(//g) || []; + // Get Spotless output from the build log + const buildLog = fs.readFileSync('/home/runner/.gradle/daemon/*/daemon-*.out', 'utf8'); + const violations = buildLog.match(/Missing Javadoc.*$/gm) || []; const violationCount = violations.length; const issueBody = `## Javadoc Validation Failed @@ -86,12 +101,7 @@ jobs: ${violationCount} Javadoc issues were found in the codebase. ### Details - ${violations.map(v => { - const file = v.match(/file="([^"]+)"/)[1]; - const line = v.match(/line="([^"]+)"/)[1]; - const message = v.match(/message="([^"]+)"/)[1]; - return `- ${file}:${line} - ${message}`; - }).join('\n')} + ${violations.map(v => `- ${v}`).join('\n')} [View full Javadoc report](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) `;