Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/scripts/generate_coverage_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
import os
import subprocess
import datetime

covered = 0
total = 0
with open("coverage.out", "r", encoding="utf-8") as f:
next(f) # skip mode line
for line in f:
line = line.strip()
if not line:
continue
try:
_, num_stmts, num_executed = line.rsplit(" ", 2)
num_stmts = int(num_stmts)
num_executed = int(num_executed)
except ValueError:
continue
total += num_stmts
if num_executed > 0:
covered += num_stmts

# Use go tool cover -func for line_rate so it matches the CI display value.
# It uses AST function boundaries and excludes non-function-level blocks,
# which is why it differs from the raw block count above.
out = subprocess.check_output(["go", "tool", "cover", "-func=coverage.out"], text=True)
line_rate = 0.0
for row in out.splitlines():
if row.startswith("total:"):
line_rate = float(row.split()[-1].rstrip("%"))
break

# Derive lines_total so that lines_covered / lines_total == line_rate.
lines_total = round(covered / (line_rate / 100)) if line_rate > 0 else total

repo = os.environ["GITHUB_REPOSITORY"]
report = {
"repo": repo,
"language": "go",
"date": datetime.date.today().isoformat(),
"commit": os.environ.get("GITHUB_SHA", "")[:7],
"branch": os.environ.get("GITHUB_REF_NAME", "main"),
"run_url": f"https://github.com/{repo}/actions/runs/{os.environ['GITHUB_RUN_ID']}",
"line_rate": line_rate,
"lines_covered": covered,
"lines_total": lines_total,
}

with open("coverage-report.json", "w", encoding="utf-8") as f:
json.dump(report, f, indent=2)
42 changes: 42 additions & 0 deletions .github/workflows/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ jobs:
- name: display coverage report
run: go tool cover -func=coverage.out

update-coverage:
name: update coverage report
runs-on: ubuntu-latest
needs: golang-test
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

- name: Install Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
with:
go-version-file: '.go-version'

- name: Download coverage artifact
Comment thread
mohanmanikanta2299 marked this conversation as resolved.
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
with:
name: coverage-report

# This step generates coverage-report.json which is committed back to this
# repo and consumed by the shared OSS Core Library metrics dashboard.
# The dashboard aggregates coverage data across multiple repos — individual
# repos do not need to interact with the dashboard directly.
#
# Dashboard repo: oss-core-library-dashboard-metrics
# Dashboard link: https://oss-core-libs-metrics-dashboard.prod.ipcomp.hashicorp.services/coverage/
- name: Generate coverage-report.json
Comment thread
mohanmanikanta2299 marked this conversation as resolved.
run: python3 .github/scripts/generate_coverage_report.py

- name: Create Pull Request
if: github.event_name == 'push'
uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # v7
with:
commit-message: "chore: update coverage report"
branch: chore/update-coverage-report
title: "chore: update coverage report"
body: "Automated PR to update `coverage-report.json` after test run on `main`."
add-paths: coverage-report.json
delete-branch: true

go-mod-tidy:
name: tidy
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ copywrite

# Ignore local configs
.env

# Go test coverage profiles
coverage.out
11 changes: 11 additions & 0 deletions coverage-report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"repo": "hashicorp/copywrite",
"language": "go",
"date": "2026-07-01",
"commit": "8f0d577",
"branch": "feature/add_coverage_file",
"run_url": "https://github.com/hashicorp/copywrite/actions/runs/28511185693",
"line_rate": 81.8,
Comment thread
mohanmanikanta2299 marked this conversation as resolved.
"lines_covered": 1017,
"lines_total": 1414
}
Loading