Badges: code metrics and coverage
🌐 English · Dansk · Bahasa Melayu
A badge is a small SVG image that shows a number, such as the lines of code of
a repository or how much of that code its tests cover. You get the badges from
one shared workflow of TirSystem/git-workflows, in the README and in the
wiki, without a service outside the git host.
What you get
The workflow code-metrics.yml counts the code of your repository, reads the
coverage report of your tests and commits SVG files to a badges branch of the
same repository:
| File | Shows |
|---|---|
code-lines.svg |
The lines of code (comment and blank lines are not counted) |
coverage.svg |
The share of those lines that the tests cover; green, yellow or red |
code-files.svg |
The number of code files (optional to show) |
A README and a wiki page link to these files, so they show the numbers of the
last run on the default branch. The workflow also writes code-metrics.json
with the numbers per language.
Add the workflow
The workflow cannot see the files of your test job, so the tests run in a job
of your own, upload the Cobertura XML report as an artifact, and the metrics
job runs after it. Copy this to .gitea/workflows/code-metrics.yml (or
.github/workflows/ on GitHub) and change the test command:
name: Code metrics
on:
push:
branches: [ main ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2
- run: python3 -m pip install pytest pytest-cov && python3 -m pytest --cov --cov-report=xml
- uses: actions/upload-artifact@v4.6.2
with:
name: coverage
path: coverage.xml
metrics:
needs: tests
permissions:
contents: write
uses: TirSystem/git-workflows/.gitea/workflows/code-metrics.yml@v2
with:
coverage-artifact: coverage
coverage-report: coverage.xml
- The call is the opt-in: a repository that does not copy it gets no badges.
- It needs no secret. The run's own token writes the
badgesbranch, which holds nothing else. - Start it on pushes to the default branch only. The badge commit carries
[skip ci], so it does not start another run. - The workflow is in release
v2.1.1ofTirSystem/git-workflowsand later (release 2.1.0 has the workflow but a defaulttools-refthat namesv1, so use 2.1.1 or later), so call it at the major tag (@v2) or an exact tag such as@v2.1.1, never atmain, and not at@v1, which does not contain it.
The report must be Cobertura XML. coverage.py writes it (coverage xml),
gcovr too (--cobertura), and so does coverlet (the cobertura format).
Without coverage-report the run passes and writes no coverage badge. A report
that is missing or malformed fails the run with the file named; a bad report
never becomes a 0% badge.
Show the badges in the README
Link the raw files of the badges branch. Replace OWNER and REPO:


Add  the same way if you want the third. On
the GitHub mirror use
https://raw.githubusercontent.com/OWNER/REPO/badges/code-lines.svg; the push
mirror copies the branch.
Show the badges in the wiki
A wiki page is a Markdown file in docs/wiki/, so the same image line works
there. Put it at the top of Home.md, or on any page, and publish the wiki as
usual (see Install). The image is fetched from the badges branch
when a reader opens the page, so a new badge shows without a wiki publish.
Good to know
- The numbers can lag. The instance caches a raw file for up to six hours; the mirror for five minutes.
- A private repository does not show its badges to a reader who is not signed in.
- The counter reads Python, shell, PowerShell, C, C++, C#, Java, JavaScript,
TypeScript, Go, Rust and SQL. Markdown, YAML, JSON and generated code are
not counted; use
excludefor generated code. - Colour follows
coverage-warn(default 60) andcoverage-good(default 80): red below the first, green from the second, yellow in between. - The full list of inputs, the rules of what is counted and the error
messages are in the guide
guides/code-metrics.mdand in ADR-0010 ofTirSystem/git-workflows.
Related
- Description: how the framework fits together
- Install: add the framework and publish the wiki
SQA and QC Framework
- Home
- Description
- Artifacts
- Install
- Badges
- GUI Testing
- Candidate Stories
- Security and operations
- Prompt Log
- AI Prompt Template
- Example: Football Club
- Example: Prime Numbers
- Appendix
Related
Languages