From 7731915393be46e20cf9f0452dbd361130e943dc Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 15:17:29 +0530 Subject: [PATCH 01/15] Add coverage report and update workflow --- .github/workflows/golangci.yml | 32 ++++++++++++++++++++++++++++++-- coverage-report.json | 11 +++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 coverage-report.json diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 06a8dd7..e273860 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - branches: - - main + # branches: + # - main pull_request: permissions: @@ -47,6 +47,34 @@ jobs: - name: run go test run: go test -v -coverprofile=coverage.out ./... + - name: Generate coverage-report.json + run: | + PCT=$(go tool cover -func=coverage.out | awk '/^total:/{print $3}' | tr -d '%') + python3 - < Date: Wed, 1 Jul 2026 15:31:40 +0530 Subject: [PATCH 02/15] Added permission to write. --- .github/workflows/golangci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index e273860..6321bc8 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -33,6 +33,8 @@ jobs: golang-test: name: test runs-on: ubuntu-latest + permissions: + contents: write steps: - name: Checkout code uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 From e25249ab51a4103c6a7b636ec803e1a6839e78b2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 1 Jul 2026 10:02:26 +0000 Subject: [PATCH 03/15] chore: update coverage report --- coverage-report.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/coverage-report.json b/coverage-report.json index a008d18..899b485 100644 --- a/coverage-report.json +++ b/coverage-report.json @@ -1,11 +1,11 @@ { - "repo": "copywrite", + "repo": "hashicorp/copywrite", "language": "go", - "date": "2026-06-24", - "commit": "", - "branch": "main", - "run_url": "", - "line_rate": 88.77, - "lines_covered": 332, - "lines_total": 374 -} + "date": "2026-07-01", + "commit": "2b6d046", + "branch": "feature/add_coverage_file", + "run_url": "https://github.com/hashicorp/copywrite/actions/runs/28509524302", + "line_rate": 81.8, + "lines_covered": 0, + "lines_total": 0 +} \ No newline at end of file From 66f3930f70c4150590a84b381713bc56053cba91 Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 15:39:14 +0530 Subject: [PATCH 04/15] revert: enable workflow to run on-push for main branch. --- .github/workflows/golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 6321bc8..febe0b8 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - # branches: - # - main + branches: + - main pull_request: permissions: From cc5a1dccda670f138105665f9fb4479e434cb15a Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 15:53:49 +0530 Subject: [PATCH 05/15] Commit coverage-report only on push. --- .github/workflows/golangci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index febe0b8..ed4d21e 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -71,11 +71,12 @@ jobs: EOF - name: Commit coverage-report.json + if: github.event_name == 'push' run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" git add coverage-report.json - git diff --cached --quiet || (git commit -m "chore: update coverage report" && git push) + git diff --cached --quiet || (git commit -m "chore: update coverage report" && git push origin HEAD:${{ github.head_ref || github.ref_name }}) - name: upload coverage report uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a From f906d285e9979952e53fe7de92b7a11d04d7747e Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 15:58:10 +0530 Subject: [PATCH 06/15] compute line coverage --- .github/workflows/golangci.yml | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index ed4d21e..cdd89d3 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - branches: - - main + # branches: + # - main pull_request: permissions: @@ -54,7 +54,28 @@ jobs: PCT=$(go tool cover -func=coverage.out | awk '/^total:/{print $3}' | tr -d '%') python3 - < 0: + covered += num_stmts + report = { "repo": repo, "language": "go", @@ -63,10 +84,10 @@ jobs: "branch": os.environ.get("GITHUB_REF_NAME", "main"), "run_url": f"https://github.com/{repo}/actions/runs/{os.environ['GITHUB_RUN_ID']}", "line_rate": float("${PCT}"), - "lines_covered": 0, - "lines_total": 0, + "lines_covered": covered, + "lines_total": total, } - with open("coverage-report.json", "w") as f: + with open("coverage-report.json", "w", encoding="utf-8") as f: json.dump(report, f, indent=2) EOF From 8f0d577f2643b60cc9a23ca8bb7ae51aa172e01a Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 16:01:49 +0530 Subject: [PATCH 07/15] Update script for line compute --- .github/workflows/golangci.yml | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index cdd89d3..986d2f0 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -58,23 +58,22 @@ jobs: repo = os.environ["GITHUB_REPOSITORY"] covered = 0 - total = 0 - with open("coverage.out", "r", encoding="utf-8") as f: - next(f) # mode: set - for line in f: - line = line.strip() - if not line: - continue - # format: file:startLine.startCol,endLine.endCol numStmts count - try: - _rng, num_stmts, count = line.rsplit(" ", 2) - num_stmts = int(num_stmts) - count = int(count) - except ValueError: - continue - total += num_stmts - if count > 0: - covered += num_stmts + total = 0 + with open("coverage.out", "r", encoding="utf-8") as f: + next(f) + for line in f: + line = line.strip() + if not line: + continue + try: + _rng, num_stmts, count = line.rsplit(" ", 2) + num_stmts = int(num_stmts) + count = int(count) + except ValueError: + continue + total += num_stmts + if count > 0: + covered += num_stmts report = { "repo": repo, From 33e9f739976de638f46f47594179084a87e02464 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 1 Jul 2026 10:32:34 +0000 Subject: [PATCH 08/15] chore: update coverage report --- coverage-report.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/coverage-report.json b/coverage-report.json index 899b485..492f960 100644 --- a/coverage-report.json +++ b/coverage-report.json @@ -2,10 +2,10 @@ "repo": "hashicorp/copywrite", "language": "go", "date": "2026-07-01", - "commit": "2b6d046", + "commit": "8f0d577", "branch": "feature/add_coverage_file", - "run_url": "https://github.com/hashicorp/copywrite/actions/runs/28509524302", + "run_url": "https://github.com/hashicorp/copywrite/actions/runs/28511185693", "line_rate": 81.8, - "lines_covered": 0, - "lines_total": 0 + "lines_covered": 1017, + "lines_total": 1414 } \ No newline at end of file From 73343eaa1cf51b447e74edc01348623f1e554a0f Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 16:03:54 +0530 Subject: [PATCH 09/15] enable on-push for main branch only --- .github/workflows/golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 986d2f0..3b3b529 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - # branches: - # - main + branches: + - main pull_request: permissions: From 8243405996958147a7b8871272c8a28c6421a661 Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 16:24:07 +0530 Subject: [PATCH 10/15] Create a separate job for updating coverage-report --- .github/workflows/golangci.yml | 44 ++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 3b3b529..a6ee50e 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -33,8 +33,6 @@ jobs: golang-test: name: test runs-on: ubuntu-latest - permissions: - contents: write steps: - name: Checkout code uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 @@ -49,6 +47,35 @@ jobs: - name: run go test run: go test -v -coverprofile=coverage.out ./... + - name: upload coverage report + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a + with: + path: coverage.out + name: coverage-report + + - 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 + 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 + uses: actions/download-artifact@v4 + with: + name: coverage-report + - name: Generate coverage-report.json run: | PCT=$(go tool cover -func=coverage.out | awk '/^total:/{print $3}' | tr -d '%') @@ -56,7 +83,7 @@ jobs: import json, os, datetime repo = os.environ["GITHUB_REPOSITORY"] - + covered = 0 total = 0 with open("coverage.out", "r", encoding="utf-8") as f: @@ -96,16 +123,7 @@ jobs: git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" git add coverage-report.json - git diff --cached --quiet || (git commit -m "chore: update coverage report" && git push origin HEAD:${{ github.head_ref || github.ref_name }}) - - - name: upload coverage report - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a - with: - path: coverage.out - name: coverage-report - - - name: display coverage report - run: go tool cover -func=coverage.out + git diff --cached --quiet || (git commit -m "chore: update coverage report" && git push origin HEAD:${{ github.ref_name }}) go-mod-tidy: name: tidy From f51144b78d55ba710cb265826f225376163a2545 Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 17:34:46 +0530 Subject: [PATCH 11/15] refactor: python script, raise PR for coverage report update. --- .github/scripts/generate_coverage_report.py | 39 ++++++++++++++ .github/workflows/golangci.yml | 57 ++++----------------- 2 files changed, 50 insertions(+), 46 deletions(-) create mode 100644 .github/scripts/generate_coverage_report.py diff --git a/.github/scripts/generate_coverage_report.py b/.github/scripts/generate_coverage_report.py new file mode 100644 index 0000000..3e07bb7 --- /dev/null +++ b/.github/scripts/generate_coverage_report.py @@ -0,0 +1,39 @@ +import json +import os +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 + +line_rate = round(covered / total * 100, 1) if total > 0 else 0.0 + +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": total, +} + +with open("coverage-report.json", "w", encoding="utf-8") as f: + json.dump(report, f, indent=2) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index a6ee50e..835575a 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -62,6 +62,7 @@ jobs: needs: golang-test permissions: contents: write + pull-requests: write steps: - name: Checkout code uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 @@ -77,53 +78,17 @@ jobs: name: coverage-report - name: Generate coverage-report.json - run: | - PCT=$(go tool cover -func=coverage.out | awk '/^total:/{print $3}' | tr -d '%') - python3 - < 0: - covered += num_stmts - - 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": float("${PCT}"), - "lines_covered": covered, - "lines_total": total, - } - with open("coverage-report.json", "w", encoding="utf-8") as f: - json.dump(report, f, indent=2) - EOF - - - name: Commit coverage-report.json + run: python3 .github/scripts/generate_coverage_report.py + + - name: Create Pull Request if: github.event_name == 'push' - run: | - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - git add coverage-report.json - git diff --cached --quiet || (git commit -m "chore: update coverage report" && git push origin HEAD:${{ github.ref_name }}) + uses: peter-evans/create-pull-request@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`." + delete-branch: true go-mod-tidy: name: tidy From 7c67a18a763ea83d4efefe40a806ef2cbfd8a785 Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 17:38:51 +0530 Subject: [PATCH 12/15] Test:PR generation on push in non-main branch --- .github/workflows/golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 835575a..01537f6 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - branches: - - main + # branches: + # - main pull_request: permissions: From 340c48fe2a851a03716bbb9f9849daf21ee2474a Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 18:06:03 +0530 Subject: [PATCH 13/15] minor fixes to compute coverage, ignore coverage.out file from commits --- .github/scripts/generate_coverage_report.py | 16 ++++++++++++++-- .github/workflows/golangci.yml | 1 + .gitignore | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/scripts/generate_coverage_report.py b/.github/scripts/generate_coverage_report.py index 3e07bb7..9e485ae 100644 --- a/.github/scripts/generate_coverage_report.py +++ b/.github/scripts/generate_coverage_report.py @@ -1,5 +1,6 @@ import json import os +import subprocess import datetime covered = 0 @@ -20,7 +21,18 @@ if num_executed > 0: covered += num_stmts -line_rate = round(covered / total * 100, 1) if total > 0 else 0.0 +# 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 = { @@ -32,7 +44,7 @@ "run_url": f"https://github.com/{repo}/actions/runs/{os.environ['GITHUB_RUN_ID']}", "line_rate": line_rate, "lines_covered": covered, - "lines_total": total, + "lines_total": lines_total, } with open("coverage-report.json", "w", encoding="utf-8") as f: diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 01537f6..7425091 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -88,6 +88,7 @@ jobs: 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: diff --git a/.gitignore b/.gitignore index 4ab4b73..9ad3b93 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ copywrite # Ignore local configs .env + +# Go test coverage profiles +coverage.out From eff94f14a3ea16f35f96d08b70ed6b544263eb43 Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 18:08:41 +0530 Subject: [PATCH 14/15] revert test changes --- .github/workflows/golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index 7425091..dcfdb02 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -2,8 +2,8 @@ name: golangci on: push: - # branches: - # - main + branches: + - main pull_request: permissions: From a60593137caf4d99487bc345e989b90210abdb9d Mon Sep 17 00:00:00 2001 From: Mohan Manikanta Date: Wed, 1 Jul 2026 19:45:31 +0530 Subject: [PATCH 15/15] pinned gha commit hash and added comments. --- .github/workflows/golangci.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml index dcfdb02..54d9501 100644 --- a/.github/workflows/golangci.yml +++ b/.github/workflows/golangci.yml @@ -73,16 +73,23 @@ jobs: go-version-file: '.go-version' - name: Download coverage artifact - uses: actions/download-artifact@v4 + 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 run: python3 .github/scripts/generate_coverage_report.py - name: Create Pull Request if: github.event_name == 'push' - uses: peter-evans/create-pull-request@v7 + uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # v7 with: commit-message: "chore: update coverage report" branch: chore/update-coverage-report