Skip to content

ci(coverage): integrate Codecov for backend coverage reporting - #168

Merged
nighca merged 6 commits into
1024XEngineer:mainfrom
xiaocheny214:ci/github-actions-coverage
Aug 11, 2026
Merged

ci(coverage): integrate Codecov for backend coverage reporting#168
nighca merged 6 commits into
1024XEngineer:mainfrom
xiaocheny214:ci/github-actions-coverage

Conversation

@xiaocheny214

@xiaocheny214 xiaocheny214 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

概述

为后端测试接入 Codecov 覆盖率上传,替换原有自造的 GitHub Actions PR 评论方案。

改动清单

文件 操作 说明
backend/pyproject.toml 编辑 +pytest-cov>=5.0 依赖 + pytest 配置
.github/workflows/backend.yml 编辑 pytest 生成覆盖率报告 + Codecov 上传
.github/workflows/coverage-comment.yml 删除 自造轮子,Codecov 原生 PR 评论接管
codecov.yml 新建 覆盖率状态 informational + flag 配置

实现细节

backend.yml

  • pytest 步骤生成 Cobertura XML (coverage.xml) 和 HTML 报告
  • 使用 codecov/codecov-action@v5 上传覆盖率数据,flag 设为 backend
  • 保留 HTML artifact 上传(30 天),方便下载查看
  • 删除自造的 coverage-xml artifact 和 PR 评论机制

codecov.yml

  • project/patch 覆盖率状态设为 informational(不阻塞 PR 合并)
  • 配置 backend flag 指向 backend/ 路径

关联

验证

  • CI 跑完后 Codecov 收到后端覆盖率数据
  • PR 页面展示 Codecov 覆盖率评论
  • README 徽章可后续添加

@vercel

vercel Bot commented Aug 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
windup Ignored Ignored Preview Aug 11, 2026 4:32am

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coverage-comment path is broken for forked PR runs as written: the workflow only grants pull-requests: write, but the script uses github.rest.issues.* and needs write access to issues/comments. On pull_request from a fork, the token is read-only anyway, so this comment/update step will fail for the exact PR type this workflow is meant to support.

Comment thread .github/workflows/backend.yml Outdated
@xiaocheny214

Copy link
Copy Markdown
Contributor Author

@fennoai 帮我看看有什么需要改动的吗?

@fennoai

fennoai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Result

我看了固定范围 55f742d...3eecc8a 的 diff,建议合并前改两处:

  1. backend/pyproject.toml:17 新增了 pytest-cov>=5.0,但 backend/uv.lock 没有更新。现有 CI 在 .github/workflows/backend.yml:36 用的是 uv sync --frozen,锁文件和 pyproject.toml 不一致时会直接失败。建议在 backend/ 下重新生成并提交 uv.lock

  2. .github/workflows/coverage-comment.yml:59-64 查找 PR 时用了 head: ${context.repo.owner}:${context.payload.workflow_run.head_branch}。这个 PR 的实际 head 是 xiaocheny214:ci/github-actions-coverage,base repo owner 是 1024XEngineer,所以当前查询返回 0 个 PR,覆盖率评论会被跳过。建议优先用 context.payload.workflow_run.pull_requests[0].number,或查询时使用 workflow run 里的 head repository owner,而不是 context.repo.owner

其它小点:.github/workflows/coverage-comment.yml:29-31 没找到 artifact 时只 return 当前 github-script 步骤,后续 unzip 步骤仍会运行并失败;如果想优雅跳过,可以给后续步骤加条件,或在同一个脚本里完成下载解析。

验证情况:已检查固定 SHA diff、确认 backend/uv.lock 在 PR 中未变化,并用 GitHub API 确认 1024XEngineer:ci/github-actions-coverage 查询为 0、xiaocheny214:ci/github-actions-coverage 查询为 1。由于当前容器没有 uvactionlint,未能本地跑 uv lock --check 或 workflow lint。

View job run

@xiaocheny214

Copy link
Copy Markdown
Contributor Author

我看了固定范围 55f742d...3eecc8a 的 diff,建议合并前改两处:

  1. backend/pyproject.toml:17 新增了 pytest-cov>=5.0,但 backend/uv.lock 没有更新。现有 CI 在 .github/workflows/backend.yml:36 用的是 uv sync --frozen,锁文件和 pyproject.toml 不一致时会直接失败。建议在 backend/ 下重新生成并提交 uv.lock
  2. .github/workflows/coverage-comment.yml:59-64 查找 PR 时用了 head: ${context.repo.owner}:${context.payload.workflow_run.head_branch}。这个 PR 的实际 head 是 xiaocheny214:ci/github-actions-coverage,base repo owner 是 1024XEngineer,所以当前查询返回 0 个 PR,覆盖率评论会被跳过。建议优先用 context.payload.workflow_run.pull_requests[0].number,或查询时使用 workflow run 里的 head repository owner,而不是 context.repo.owner

其它小点:.github/workflows/coverage-comment.yml:29-31 没找到 artifact 时只 return 当前 github-script 步骤,后续 unzip 步骤仍会运行并失败;如果想优雅跳过,可以给后续步骤加条件,或在同一个脚本里完成下载解析。

验证情况:已检查固定 SHA diff、确认 backend/uv.lock 在 PR 中未变化,并用 GitHub API 确认 1024XEngineer:ci/github-actions-coverage 查询为 0、xiaocheny214:ci/github-actions-coverage 查询为 1。由于当前容器没有 uvactionlint,未能本地跑 uv lock --check 或 workflow lint。

相关已修改。

- Add pytest-cov>=5.0 to dev dependencies
- Add [tool.pytest.ini_options] with --cov=packages --cov-report=term-missing
- Pytest step: add --cov-report=xml and --cov-report=html flags
- Upload HTML coverage report as Artifact (30-day retention)
- PR auto-comment with line/branch coverage summary via github-script
- Update existing comment instead of creating duplicates
- Add pull-requests: write permission for PR comments

Refs: 1024XEngineer#167
- Move PR comment from backend.yml to new coverage-comment.yml
- Use workflow_run trigger to get write permissions on fork PRs
- Backend CI uploads coverage.xml as artifact (1-day retention)
- Coverage Comment workflow downloads artifact and posts PR comment
- Fixes 'Resource not accessible by integration' 403 error

Refs: 1024XEngineer#167
github.rest.issues.listComments/updateComment/createComment requires
issues:write, not just pull-requests:write.

Refs: 1024XEngineer#167
- Regenerate uv.lock to include pytest-cov (fixes uv sync --frozen)
- Use workflow_run.pull_requests[0].number for PR lookup (fixes fork PR)
- Merge download/parse/comment into single step (fixes artifact-not-found)
- Remove emoji from coverage comment

Refs: 1024XEngineer#167
@minorcell

Copy link
Copy Markdown
Member

@fennoai

  1. 是否可以换成codecov
  2. 是否覆盖了前端测试情况

@fennoai

fennoai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

结论

  1. 可以换成 Codecov。

    • 当前实现是 GitHub Actions + Artifact + 自定义 PR 评论;可以改为生成 coverage.xml 后使用 Codecov Action 上传,由 Codecov 负责趋势统计和 PR 评论。
    • 建议给后端、前端分别设置 coverage flag(例如 backend / frontend),这样可以同时显示总覆盖率和分项目覆盖率。
    • 如果仍需要可下载的 HTML 报告,可以保留现有 upload-artifact;换成 Codecov 不影响这部分。
    • Fork PR 需要特别确认 Codecov 对公开仓库的 tokenless 上传策略;如果需要 token,不应把 token 暴露给不受信任的 fork 工作流,通常应改用受信任上下文或 Codecov 推荐的 fork PR 配置。
  2. 当前 PR 没有覆盖前端覆盖率。

    • frontend-ci.yml 确实会执行 npm run test,所以前端测试会运行。
    • 但它没有启用 Vitest coverage,也没有生成或上传前端覆盖率报告。
    • PR ci(coverage): integrate Codecov for backend coverage reporting #168 新增的后端覆盖率范围是 --cov=packages,只统计 Python 后端;没有包含 frontend/
    • 前端已有不少 *.test.ts / *.test.tsx 测试,但“执行测试”和“统计覆盖率”是两件事。

如果目标是统一用 Codecov,下一步应在 frontend-ci.yml 增加 Vitest coverage(通常使用 @vitest/coverage-v8),再上传前端报告,并为前后端设置独立 flag。

@xiaocheny214

Copy link
Copy Markdown
Contributor Author

@fennoai

  1. 是否可以换成codecov
  2. 是否覆盖了前端测试情况

这个貌似需要拿到codecov的key还是什么来着,需要放到setting中。

@fennoai

fennoai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

结论

  • 已核对 Codecov 的 token / Settings 配置方式
  • 已结合本 PR 的 fork 工作流确认安全边界
  • 已确认前端仍需要单独开启 coverage

你记得基本正确:通常需要把 Codecov token 配置到仓库的 GitHub Actions Secret,但要分情况:

  • 如果 Windup 是公开仓库,Codecov 对公开仓库的 GitHub Actions 上传通常可以采用 tokenless 模式;这种方式更适合 fork PR,不需要把密钥暴露给外部贡献者。
  • 如果 Codecov 项目或仓库配置要求 token,则由仓库维护者在 Settings → Secrets and variables → Actions 中添加 CODECOV_TOKEN。不能把 token 写进 workflow,也不应该把它放在 fork PR 能读取的普通配置里。
  • Fork PR 的 workflow 默认不能安全地读取上游仓库 Secrets,所以不能依赖“把 token 放在主仓库 Settings 后,fork PR 就一定能上传”。需要按 Codecov 对 fork PR 的推荐方式配置,或只让受信任上下文执行带 token 的上传。
  • 因此,换成 Codecov 是可行的,但最好由主仓库维护者完成 Codecov 项目启用、Secret(如确实需要)和 fork PR 策略配置;PR 本身只提交 workflow 配置,不提交任何 key。

另外,Codecov 不会自动覆盖前端。当前前端 CI 只是执行 npm run test,还没有生成 coverage 报告。若要纳入前端,需要:

  1. frontend 添加 Vitest coverage provider(例如 @vitest/coverage-v8);
  2. 将测试命令改为生成 lcov cobertura/xml 报告;
  3. frontend-ci.yml 上传前端报告,并设置独立的 frontend flag;
  4. 后端使用 backend flag,避免把 Python 和 TypeScript 覆盖率混成一个难以解释的数字。

所以当前建议是:可以迁移 Codecov,但需要仓库维护者处理 token / fork 策略;本 PR 当前只覆盖后端 coverage,尚未覆盖前端 coverage。

@nighca

nighca commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

如果可以,尽量用 Codecov?现在的版本看上去是自己造了个类似的轮子

@xiaocheny214

Copy link
Copy Markdown
Contributor Author

如果可以,尽量用 Codecov?现在的版本看上去是自己造了个类似的轮子

好的

- Add codecov/codecov-action@v5 to backend.yml for coverage upload
- Remove coverage-comment.yml (superseded by Codecov native PR comments)
- Add codecov.yml with informational status and backend flag config

Refs: 1024XEngineer#167
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@xiaocheny214 xiaocheny214 changed the title ci(coverage): add pure GitHub Actions coverage reporting ci(coverage): integrate Codecov for backend coverage reporting Aug 11, 2026
@minorcell

minorcell commented Aug 11, 2026

Copy link
Copy Markdown
Member

@xiaocheny214

补充一点:#193 前端上传时带了 flags: frontend,但 codecov.yml 里目前只定义了 backend,前端 flag 没配 paths。这样合并后 Codecov 的 PR 评论里前端覆盖率不会单独显示,建议在 codecov.yml 的 comment.flags 下补一个 frontend 定义(paths 指到 frontend/)。不然前端覆盖率数据进了 Codecov 但看不到分组明细,等于白传。

Comment thread .github/workflows/backend.yml Outdated
@xiaocheny214

Copy link
Copy Markdown
Contributor Author

已删除 HTML 报告上传步骤。既然已配置 Codecov,HTML artifact 不再需要,Codecov 提供更好的覆盖率可视化和趋势分析。

- Remove HTML coverage report artifact step (Codecov replaces it)
- Remove --cov-report=html:htmlcov from pytest command
- Add frontend flag to codecov.yml comment config
@xiaocheny214

Copy link
Copy Markdown
Contributor Author

已在 codecov.yml 中添加 frontend flag 配置:

comment:
  flags:
    backend:
      paths:
        - backend/
    frontend:
      paths:
        - frontend/

PR #193 前端覆盖率上传时带了 flags: frontend,现在 Codecov PR 评论会单独显示前端覆盖率分组明细。

@xiaocheny214

Copy link
Copy Markdown
Contributor Author

已删除 HTML 报告上传步骤。既然已配置 Codecov,HTML artifact 不再需要,Codecov 提供更好的覆盖率可视化和趋势分析。

@xiaocheny214

补充一点:#193 前端上传时带了 flags: frontend,但 codecov.yml 里目前只定义了 backend,前端 flag 没配 paths。这样合并后 Codecov 的 PR 评论里前端覆盖率不会单独显示,建议在 codecov.yml 的 comment.flags 下补一个 frontend 定义(paths 指到 frontend/)。不然前端覆盖率数据进了 Codecov 但看不到分组明细,等于白传。

#168 (comment)

@nighca
nighca merged commit 2872598 into 1024XEngineer:main Aug 11, 2026
7 checks passed
@xiaocheny214
xiaocheny214 deleted the ci/github-actions-coverage branch August 14, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ci: 集成测试覆盖率统计(Codecov)

3 participants