Skip to content

ci: fix coverage comment on fork PRs, switch README to dynamic badge - #31

Merged
PabloPardoGarcia merged 2 commits into
mainfrom
pablo/fix-fork-pr-coverage-comment
May 29, 2026
Merged

PabloPardoGarcia merged 2 commits into
mainfrom
pablo/fix-fork-pr-coverage-comment

Conversation

@PabloPardoGarcia

@PabloPardoGarcia PabloPardoGarcia commented May 29, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Splits commenting out of the Test workflow so it works for PRs from forks (which only get a read-only GITHUB_TOKEN).
  • Adds a new Coverage Comment workflow that runs on workflow_run and posts the coverage comment with pull-requests: write from the base-repo context.
  • Adds a new Coverage Badge workflow that runs on push to main, generates a shields.io endpoint JSON, and publishes it to a dedicated badges orphan branch. README now references that JSON so the badge updates dynamically on every merge to main.
  • Removes the in-PR README auto-push step entirely. It could never work on fork PRs and was a CI-loop / contributor-branch-noise risk on internal PRs.

Why

All three currently open PRs from external contributors (#22, #24, #30) fail the same way: the coverage-comment step calls the GitHub API with a read-only token, and the README auto-push step tries to push to a fork branch. Both are impossible from a pull_request-triggered workflow on a fork.

The fix is the canonical two-workflow pattern recommended by both MishaKav/pytest-coverage-comment and py-cov-action/python-coverage-comment-action:

  1. test.yml runs on pull_request, has read-only perms, and only generates + uploads a coverage.xml artifact (plus pr_number.txt).
  2. coverage-comment.yml runs on workflow_run after Test completes. Because workflow_run always runs in the base-repo context, it has the pull-requests: write permission it needs. It downloads the artifact and posts the comment.

This is strictly safer than the pull_request_target alternative because coverage-comment.yml never checks out PR code, so a malicious fork cannot get its code executed with elevated permissions.

Dynamic README badge

To replace the previous "auto-commit a static badge to every PR branch" hack:

  • coverage-badge.yml runs on push to main, generates a shields.io endpoint JSON with the percentage and a threshold-based color, and force-pushes it to a badges orphan branch (single file, replaces previous on each run).
  • The README's <img> now points at https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/glassflow/glassgen/badges/coverage.json, which shields.io fetches on demand.

Net effect: no more bot commits to main, no more bot pushes to PR branches, and the badge stays current automatically.

Test plan

  • On merge: next fork PR sees the coverage comment posted after CI completes (currently fails outright).
  • On merge: next internal PR also sees the coverage comment (parity with current behaviour).
  • On merge: the Coverage Badge workflow runs against main, creates the badges branch, and the README badge resolves to a real percentage.
  • No README diff is generated on PR builds.

Notes

The Test workflow ran as a single pull_request job that needed
pull-requests:write to post the coverage comment and contents:write to
push a README badge update back to the PR branch. Both fail on fork
PRs: GITHUB_TOKEN is read-only and you can't push to a fork branch
from the base repo's CI.

Split into two workflows:

- test.yml runs on pull_request with read-only perms, generates
  coverage.xml, and uploads it (plus the PR number) as an artifact.

- coverage-comment.yml runs on workflow_run with pull-requests:write,
  downloads the artifact, and posts the comment on the PR. This runs
  in the base-repo context so it works for fork PRs without checking
  out untrusted code.

Also drops the auto-push of a coverage badge into README.md from
PR builds. That step pushed bot commits to contributor branches,
risked CI loops, and could never work on forks anyway. If we want a
coverage badge on main, that belongs in a separate push-to-main
workflow or a dynamic Shields.io endpoint, not in PR CI.
Adds a Coverage Badge workflow that runs on push to main, generates a
shields.io endpoint JSON from coverage.xml, and force-pushes it to a
dedicated `badges` orphan branch. The README now references a shields.io
endpoint URL that resolves through raw.githubusercontent.com, so the
badge updates automatically on every merge to main with no commits to
main itself.

This replaces the previous static <img> tag in the README and removes
the need for the deleted README auto-push step in PR CI.

Note: the badge will appear broken until this branch lands on main and
the Coverage Badge workflow runs for the first time, creating the
`badges` branch and seeding coverage.json.
@vercel

vercel Bot commented May 29, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
glassgen Ready Ready Preview, Comment May 29, 2026 8:20am

Request Review

@PabloPardoGarcia
PabloPardoGarcia merged commit 8da05fa into main May 29, 2026
7 checks passed

This branch was successfully deployed

1 active deployment
Preview — 67974190 Deployed May 29, 2026 by vercel[bot]
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.

1 participant