use spotless #3
Workflow file for this run
This file contains 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
name: Javadoc Validation | |
on: | |
push: | |
pull_request: | |
branches: [ '*' ] | |
workflow_dispatch: | |
inputs: | |
createIssue: | |
description: 'Create an issue for missing Javadocs' | |
required: true | |
type: boolean | |
default: true | |
jobs: | |
check-javadoc: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Setup Gradle | |
uses: gradle/gradle-build-action@v2 | |
- name: Add Spotless Config | |
run: | | |
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)/ | |
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: steps.spotless.outcome == 'failure' | |
run: ./gradlew javadoc | |
- name: Upload Javadoc Report | |
if: steps.spotless.outcome == 'failure' | |
uses: actions/upload-artifact@v3 | |
with: | |
name: javadoc-report | |
path: build/docs/javadoc/ | |
- name: Create Issue on Failure | |
if: steps.spotless.outcome == 'failure' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
// 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 => `- ${v}`).join('\n')} | |
[View full Javadoc report](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) | |
`; | |
// Create the issue | |
await github.rest.issues.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: '📝 Missing Javadoc Documentation Detected', | |
body: issueBody, | |
labels: ['documentation', 'javadoc'] | |
}); |