name: 类变更检测
run-name: ${{ gitea.actor }}的类变更检测

#on: [push]
on:
  push:
    branches:
      - class-check
  workflow_dispatch:

jobs:
  class-change-check:
    if: ${{ gitea.ref != 'refs/heads/pre' && gitea.ref != 'refs/heads/dev' && gitea.ref != 'refs/heads/master-2.0' }}
    runs-on: jdk11

    steps:
      - name: 检出代码
        run: |
          git config --global http.sslVerify false
          git clone "https://${{ gitea.token }}@git.niujiekeji.com/${{ gitea.repository }}.git" .
          git checkout ${{ gitea.sha }}
#
#        run: |
#          git clone --depth=2 \
#            "http://oauth2:${{ gitea.token }}@host.docker.internal:3000/${{ gitea.repository }}.git" \
#            .
#          git checkout ${{ gitea.sha }}

      - name: 检查配置文件与预编译 jar
        run: |
          if [ ! -f .gitea/config.yaml ]; then
            echo "错误: 缺少 .gitea/config.yaml"
            exit 1
          fi
          if [ ! -f .gitea/workflows/class-checker.jar ]; then
            echo "错误: 缺少 .gitea/workflows/class-checker.jar"
            echo "请本地执行: powershell -File scripts/build-class-checker.ps1"
            exit 1
          fi

      - name: 验证 JDK
        run: |
          echo "Java: $(java -version 2>&1 | head -1)"

      - name: 执行类变更检测
        run: |
          OLD_SHA=$(git rev-parse HEAD~1 2>/dev/null || echo "")
          if [ -z "$OLD_SHA" ]; then
            echo "首次提交，跳过类变更检测"
            exit 0
          fi
          COMMIT_TIME=$(git log -1 --format=%cd --date=format:'%Y-%m-%d %H:%M:%S')
          java -jar .gitea/workflows/class-checker.jar \
            --config .gitea/config.yaml \
            --repo-root . \
            --old-sha "$OLD_SHA" \
            --new-sha "$(git rev-parse HEAD)" \
            --modifier "${{ gitea.actor }}" \
            --modify-time "$COMMIT_TIME"
