Skip to content

fix:code check plugin(Checkstyle +PMD+SpotBugs) #118

fix:code check plugin(Checkstyle +PMD+SpotBugs)

fix:code check plugin(Checkstyle +PMD+SpotBugs) #118

Workflow file for this run

name: Checkstyle Code Quality
on:
push:
branches:
- develop
pull_request:
branches:
- develop
jobs:
checkstyle:
runs-on: ubuntu-24.04
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # 必须拉取完整历史,才能比较分支差异
- name: Set up JDK 17
uses: actions/setup-java@v5
with:
java-version: '17'
distribution: 'temurin'
# 缓存 Maven 依赖,加速构建
- name: Cache Maven dependencies
uses: actions/cache@v5
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
# 直接运行 Checkstyle 检查(不执行完整的 package)
- name: Run Checkstyle
run: bash .github/scripts/checkstyle-pr.sh
env:
GITHUB_BASE_REF: ${{ github.base_ref }}
- name: Debug - list all checkstyle reports
run: |
echo "当前工作目录: $(pwd)"
echo "=== 列出所有 target 目录 ==="
find . -type d -name "target" -exec echo "目录: {}" \; -exec ls -la {}/ \;
echo "=== 查找 checkstyle 文件 ==="
find . -name "checkstyle*.xml" -o -name "checkstyle*.html" | while read f; do echo "找到: $f"; done
- name: Debug - Check HTML existence
run: |
echo "Searching for checkstyle.html:"
find . -name "checkstyle.html" -type f
echo "Also check reports directory:"
ls -la base/target/reports/ || echo "base/target/reports not found"
# 如果检查失败,仍然上传报告供查看
- name: Upload Checkstyle report
if: always() # 即使失败也上传报告
uses: actions/upload-artifact@v7
with:
name: checkstyle-report
path: |
**/target/checkstyle-result.xml
**/target/checkstyle-checker.xml
**/target/reports/
if-no-files-found: warn
- name: Run PMD on changed Java files only
id: pmd
run: |
# 1. 确定目标分支(从环境变量获取)
if [ -n "$GITHUB_BASE_REF" ]; then
BASE_BRANCH="$GITHUB_BASE_REF"
else
# 如果是 push 事件,回退到 main 或 develop
BASE_BRANCH="develop" # 或 main,根据项目调整
fi
# 2. 确保目标分支的远程引用存在
git fetch origin "$BASE_BRANCH" --depth=1 || true
# 3. 获取变更的 Java 文件(比较当前 HEAD 与目标分支)
CHANGED_FILES=$(git diff --name-only "origin/$BASE_BRANCH" HEAD | grep '\.java$' || true)
echo "📝 变更的 Java 文件列表:"
if [ -n "$CHANGED_FILES" ]; then
echo "$CHANGED_FILES"
else
echo "(无)"
fi
echo "----------------------------------------"
if [ -z "$CHANGED_FILES" ]; then
echo "No Java files changed, skipping PMD."
echo "violations=0" >> $GITHUB_OUTPUT
exit 0
fi
# 4. 生成文件列表(绝对路径)
> changed-files.txt
for file in $CHANGED_FILES; do
echo "$PWD/$file" >> changed-files.txt
done
# 5. 下载 PMD
PMD_VERSION="6.55.0"
curl -L "https://github.com/pmd/pmd/releases/download/pmd_releases%2F${PMD_VERSION}/pmd-bin-${PMD_VERSION}.zip" -o pmd.zip
unzip -q pmd.zip
mv pmd-bin-${PMD_VERSION} pmd
PMD_CMD="$PWD/pmd/bin/run.sh"
chmod +x "$PMD_CMD"
# 6. 扫描变更的 Java 文件
"$PMD_CMD" pmd --no-cache \
--file-list changed-files.txt \
-f sarif \
-R rulesets/java/quickstart.xml \
-r pmd-report.sarif || true
# 7. 统计违规数
if [ -f pmd-report.sarif ]; then
violations=$(jq '.runs[0].results | length' pmd-report.sarif)
else
violations=0
fi
echo "violations=$violations" >> $GITHUB_OUTPUT
# 清理
rm -f changed-files.txt
- name: Install sarif-tools and convert to HTML
run: |
pip install sarif-tools
sarif html pmd-report.sarif --output pmd-report.html
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: pmd-report.sarif
- name: Upload SARIF file as artifact
uses: actions/upload-artifact@v7
with:
name: pmd-sarif-report # 给工件起一个有意义的名字
path: pmd-report.sarif
- name: Upload HTML report
uses: actions/upload-artifact@v7
with:
name: pmd-html-report
path: pmd-report.html
- name: Check PMD violations
run: |
if [[ ${{ steps.pmd.outputs.violations }} -eq 0 ]]; then
echo "✅ PMD 未发现代码问题,构建通过。"
exit 0
else
echo "❌ PMD 发现 ${{ steps.pmd.outputs.violations }} 个代码问题,构建失败。"
exit 1
fi