From a3b1077fdd330dc247718d5079c4a398cfb95611 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Wed, 22 Jul 2026 08:15:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ci:=20Vitest=E5=A2=9E=E9=87=8F=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=EF=BC=8CPR=E6=A8=A1=E5=BC=8F=E5=8F=AA=E8=B7=91?= =?UTF-8?q?=E6=94=B9=E5=8A=A8=E7=9B=B8=E5=85=B3=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/ci/vitest_incremental.sh | 78 ++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 scripts/ci/vitest_incremental.sh diff --git a/scripts/ci/vitest_incremental.sh b/scripts/ci/vitest_incremental.sh new file mode 100644 index 000000000..d367a07f4 --- /dev/null +++ b/scripts/ci/vitest_incremental.sh @@ -0,0 +1,78 @@ +#!/bin/bash +# Vitest 增量执行脚本 +# PR模式下只跑与改动文件相关的测试,大幅节省时间 +# 用法: bash scripts/ci/vitest_incremental.sh +set -eu + +cd apps/web + +# 如果不是PR事件,直接全量跑 +if [ "${GITHUB_EVENT_NAME:-}" != "pull_request" ]; then + echo "非PR模式,全量执行Vitest" + npx --no-install vitest run --coverage + exit $? +fi + +# 获取PR改动的文件列表 +PR_NUMBER=$(echo "${GITHUB_REF:-}" | sed 's|refs/pull/||; s|/.*||') +if [ -z "$PR_NUMBER" ]; then + echo "无法获取PR编号,全量执行Vitest" + npx --no-install vitest run --coverage + exit $? +fi + +API_URL="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files?limit=100" +CHANGED_FILES=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "$API_URL" | python3 -c " +import json, sys +try: + files = json.load(sys.stdin) + web_files = [] + for f in files: + fname = f['filename'] + # 只关注前端源码文件 + if fname.startswith('apps/web/src/') and fname.endswith(('.ts', '.tsx', '.js', '.jsx')) and f['status'] != 'removed': + # 去掉apps/web/前缀,变成相对路径 + web_files.append(fname.replace('apps/web/', '')) + print(' '.join(web_files)) +except Exception as e: + print('') +") + +if [ -z "$CHANGED_FILES" ]; then + echo "PR未改动前端源码文件,跳过Vitest" + echo "(如果配置了前端单测门禁,请确保至少有一个相关测试)" + exit 0 +fi + +FILE_COUNT=$(echo "$CHANGED_FILES" | wc -w) +echo "PR改动了 $FILE_COUNT 个前端文件" +echo "改动文件: $CHANGED_FILES" + +# 如果改动文件太多(超过30个),全量跑更可靠 +if [ "$FILE_COUNT" -gt 30 ]; then + echo "改动文件较多(>$FILE_COUNT),降级为全量执行以确保覆盖" + npx --no-install vitest run --coverage + exit $? +fi + +# 使用vitest --related 跑增量测试 +echo "" +echo "=== 增量执行 Vitest(只跑相关测试)===" +echo "相关源文件: $CHANGED_FILES" +echo "" + +set +e +npx --no-install vitest run --related $CHANGED_FILES +VITEST_EXIT=$? +set -e + +if [ "$VITEST_EXIT" -eq 0 ]; then + echo "" + echo "✅ 增量测试通过" + echo "(仅覆盖与改动相关的测试用例)" + exit 0 +else + echo "" + echo "❌ 增量测试失败" + exit $VITEST_EXIT +fi -- 2.54.0 From 839743babe0b6c09e6e468db5bed73d7e10f6fd7 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Wed, 22 Jul 2026 08:15:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ci:=20Vitest=E5=A2=9E=E9=87=8F=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=EF=BC=8CPR=E6=A8=A1=E5=BC=8F=E5=8F=AA=E8=B7=91?= =?UTF-8?q?=E6=94=B9=E5=8A=A8=E7=9B=B8=E5=85=B3=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci-pipeline.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/ci-pipeline.yml b/.gitea/workflows/ci-pipeline.yml index 64c9551ea..9d9753b85 100755 --- a/.gitea/workflows/ci-pipeline.yml +++ b/.gitea/workflows/ci-pipeline.yml @@ -387,9 +387,11 @@ jobs: [ $i -eq 3 ] && exit 1 sleep 5 done - - name: Run Vitest with coverage + - name: Run Vitest (incremental for PRs, full for main branches) shell: sh - run: bash scripts/ci/step_frontend_run.sh "npx --no-install vitest run --coverage" + env: + GITHUB_TOKEN: ${{ github.token }} + run: bash scripts/ci/vitest_incremental.sh - name: Job duration summary if: always() shell: sh -- 2.54.0