From 31e29e1aed572c401dc1e932e98936166e24ff8a Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 17 Jul 2026 15:08:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(ci):=20P1-3=20=E5=A2=9E=E9=87=8F?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=8E=87=E9=97=A8=E7=A6=81=20-=20diff-cover?= =?UTF-8?q?=E6=8E=A5=E5=85=A5+=E5=85=A8=E9=87=8F=E9=97=A8=E6=A7=9B?= =?UTF-8?q?=E6=8F=90=E8=87=B365%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增diff-cover增量行覆盖率检查,PR改动代码需>=60%覆盖率 - 增量测试模式下门槛调低至40%(覆盖不全) - 全量覆盖率门槛从60%提升至65%(当前68%,留3%缓冲) - 覆盖率不达标直接阻塞合并,倒逼新代码补单元测试 --- .gitea/workflows/ci-cd.yml | 46 +++++++++++++++++++++++++++++++++++++- requirements-dev.txt | 1 + 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml index b7ea51473..5596c1a4e 100755 --- a/.gitea/workflows/ci-cd.yml +++ b/.gitea/workflows/ci-cd.yml @@ -264,7 +264,51 @@ jobs: ' - name: Run unit tests with coverage shell: sh - run: "set -eu\nPYTHONPATH=\"$PWD/apps/api:$PWD\" python3 -m coverage run \\\n --source=apps/api/app,packages \\\n --omit=\"*/migrations/*,*/tests/*,*/test_*.py,*/site-packages/*\" \\\n --branch \\\n -m pytest tests/unit -q\npython3 -m coverage report --show-missing\npython3 -m coverage xml -o coverage.xml\npython3 -m coverage report --fail-under=60 > /dev/null\n" + run: "set -eu\nPYTHONPATH=\"$PWD/apps/api:$PWD\" python3 -m coverage run \\\n --source=apps/api/app,packages \\\n --omit=\"*/migrations/*,*/tests/*,*/test_*.py,*/site-packages/*\" \\\n --branch \\\n -m pytest tests/unit -q\npython3 -m coverage report --show-missing\npython3 -m coverage xml -o coverage.xml\npython3 -m coverage report --fail-under=65 > /dev/null\n" + - name: Diff coverage check (增量行覆盖率) + if: github.event_name == 'pull_request' + shell: sh + env: + GITHUB_TOKEN: ${{ github.token }} + run: | + set -eu + PR_NUMBER=$(echo "$GITHUB_REF" | sed 's|refs/pull/||; s|/.*||') + + # 获取base分支 + BASE_BRANCH="${{ github.base_ref }}" + echo "Base branch: $BASE_BRANCH" + + # 拉取base分支用于对比 + git fetch origin $BASE_BRANCH --depth=100 2>/dev/null || true + + # 根据模式设置门槛 + if [ "${UNIT_TEST_MODE:-full}" = "incremental" ]; then + # 增量测试模式覆盖不全,门槛设低一些 + THRESHOLD=40 + echo "增量测试模式,增量覆盖率门槛: ${THRESHOLD}%" + else + THRESHOLD=60 + echo "全量测试模式,增量覆盖率门槛: ${THRESHOLD}%" + fi + + # 运行diff-cover + set +e + python3 -m diff_cover.diff_cover_tool coverage.xml --compare-branch="origin/$BASE_BRANCH" --fail-under=$THRESHOLD --html-report diff_coverage.html 2>&1 + DIFF_EXIT=$? + set -e + + if [ $DIFF_EXIT -ne 0 ]; then + echo "" + echo "❌ 增量覆盖率未达到门槛 (${THRESHOLD}%)" + echo " 请为改动的代码添加单元测试后再提交" + echo "" + echo "=== 覆盖率报告 ===" + python3 -m diff_cover.diff_cover_tool coverage.xml --compare-branch="origin/$BASE_BRANCH" 2>&1 | tail -30 + exit 1 + fi + + echo "✅ 增量覆盖率达标" + - name: CI failure notification if: failure() shell: sh diff --git a/requirements-dev.txt b/requirements-dev.txt index 585f62d07..e0959a8cc 100755 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,3 +11,4 @@ pytest==8.3.3 pytest-asyncio==0.24.0 pytest-cov==6.0.0 pytest-timeout==2.3.1 +diff-cover>=8.0 -- 2.54.0 From 84f383c6cb994e3df1074693b0de28c30ad3b2f4 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 17 Jul 2026 17:09:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(ci):=20diff-cover=E5=89=8D=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96git=E4=BB=93=E5=BA=93(CI=20tarball=20checkout?= =?UTF-8?q?=E6=97=A0.git)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci-cd.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml index 5596c1a4e..35b46253d 100755 --- a/.gitea/workflows/ci-cd.yml +++ b/.gitea/workflows/ci-cd.yml @@ -278,8 +278,16 @@ jobs: BASE_BRANCH="${{ github.base_ref }}" echo "Base branch: $BASE_BRANCH" + # 初始化git (CI tarball checkout没有.git目录) + git init > /dev/null 2>&1 + git remote add origin https://git.xiaoxiajianji.com/xiaoxia/xiaoxia-saas.git > /dev/null 2>&1 + git config user.email "ci@local" + git config user.name "CI" # 拉取base分支用于对比 - git fetch origin $BASE_BRANCH --depth=100 2>/dev/null || true + git fetch origin $BASE_BRANCH --depth=100 + # 提交当前代码 + git add -A > /dev/null 2>&1 + git commit -m "ci-tmp" > /dev/null 2>&1 # 根据模式设置门槛 if [ "${UNIT_TEST_MODE:-full}" = "incremental" ]; then -- 2.54.0 From eb7eb6f31730ca21c19e1bbd099335d4239cc1dc Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 17 Jul 2026 17:26:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?perf(ci):=20ci-l1=E2=86=92ci-check=20?= =?UTF-8?q?=E5=85=85=E5=88=86=E5=88=A9=E7=94=A8=E5=85=A8=E9=83=A8runner?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci-cd.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml index 35b46253d..4719cbad5 100755 --- a/.gitea/workflows/ci-cd.yml +++ b/.gitea/workflows/ci-cd.yml @@ -24,7 +24,7 @@ concurrency: jobs: check-frontend-only: name: Check if frontend-only change - runs-on: ci-l1 + runs-on: ci-check if: github.event_name == 'pull_request' outputs: skip_backend: ${{ steps.check.outputs.skip_backend }} @@ -60,7 +60,7 @@ jobs: needs: check-frontend-only if: always() && needs.check-frontend-only.outputs.skip_backend != 'true' name: Validate Code Quality And Tests - runs-on: ci-l1 + runs-on: ci-check timeout-minutes: 10 env: DATABASE_URL: postgresql+psycopg://postgres:postgres@127.0.0.1:5432/xiaoxia_saas @@ -464,7 +464,7 @@ jobs: ' frontend-lint: name: Frontend Lint - runs-on: ci-l1 + runs-on: ci-check timeout-minutes: 10 steps: - name: Checkout code -- 2.54.0