Skip to content

Commit

Permalink
use spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanErn committed Dec 23, 2024
1 parent 8cb61b5 commit 2adb35a
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions .github/workflows/check_javadocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ name: Javadoc Validation

on:
push:
branches: [ '*' ]
pull_request:
branches: [ master ]
branches: [ '*' ]
workflow_dispatch:
inputs:
createIssue:
Expand All @@ -25,73 +24,84 @@ 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'
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="JavadocMethod">
<property name="severity" value="warning"/>
<property name="scope" value="public"/>
</module>
<module name="JavadocType">
<property name="severity" value="warning"/>
<property name="scope" value="public"/>
</module>
<module name="JavadocVariable">
<property name="severity" value="warning"/>
<property name="scope" value="public"/>
</module>
<module name="MissingJavadocMethod">
<property name="severity" value="warning"/>
<property name="scope" value="public"/>
</module>
</module>
</module>
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(/<error.*?\/>/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
${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})
`;
Expand Down

0 comments on commit 2adb35a

Please sign in to comment.