Skip to content

Commit ab550c9

Browse files
talgalilimeta-codesync[bot]
authored andcommitted
Fix coverage badge workflow for protected branches
Summary: The coverage workflow was failing to push the badge to `main` due to branch protection rules (CLA check, import status check, PR requirement). ## Changes 1. **Push badge to separate `badges` branch**: Instead of pushing directly to the protected `main` branch, the workflow now pushes the coverage badge to an unprotected `badges` branch. This avoids the branch protection restrictions. 2. **Updated README badge URL**: Changed the badge endpoint from `main` to `badges` branch: ``` # Before .../balance/main/.github/badges/coverage.json # After .../balance/badges/.github/badges/coverage.json ``` 3. **Conditional execution**: The "Commit coverage badge" step only runs on push events to main (`github.event_name == 'push'`), while coverage extraction still runs on all main branch events (including PRs) for visibility. Fixes #271 Differential Revision: D90920671 fbshipit-source-id: d2cc0260ba5e84fab9ff1132a36f4b94d7a2bde6
1 parent 2c0a324 commit ab550c9

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

.github/workflows/coverage.yml

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,67 @@ jobs:
1313
steps:
1414
- name: Checkout
1515
uses: actions/checkout@v5
16-
16+
1717
- name: Set up Python
1818
uses: actions/setup-python@v5
1919
with:
2020
python-version: '3.12'
21-
21+
2222
- name: Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
2525
pip install .[dev]
26-
26+
2727
- name: Run tests with coverage
2828
run: |
2929
python -m pytest --cov=balance --cov-report=xml --cov-report=html --cov-report=term-missing
30-
30+
3131
- name: Upload coverage reports to artifacts
3232
if: always()
3333
uses: actions/upload-artifact@v4
3434
with:
3535
name: coverage-report
3636
path: htmlcov/
37-
37+
3838
- name: Upload coverage XML
3939
if: always()
4040
uses: actions/upload-artifact@v4
4141
with:
4242
name: coverage-xml
4343
path: coverage.xml
44-
44+
4545
- name: Display coverage percentage
4646
run: |
4747
coverage report --precision=2
48-
48+
4949
- name: Extract coverage percentage
5050
if: github.ref == 'refs/heads/main'
5151
run: |
5252
COVERAGE=$(coverage report --precision=2 | grep TOTAL | awk '{print $NF}' | sed 's/%//')
5353
echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV
5454
echo "Coverage: $COVERAGE%"
55-
56-
- name: Create coverage badge
57-
if: github.ref == 'refs/heads/main'
55+
56+
- name: Commit coverage badge
57+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
5858
run: |
59+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
60+
git config --local user.name "github-actions[bot]"
61+
62+
# Fetch the badges branch or create it as an orphan
63+
git fetch origin badges:badges 2>/dev/null || git checkout --orphan badges
64+
git checkout badges
65+
66+
# Create badge directory and file
5967
mkdir -p .github/badges
6068
python -c "
61-
import os
69+
import os, json
6270
coverage = float(os.environ['COVERAGE'])
6371
color = 'brightgreen' if coverage >= 80 else 'yellow' if coverage >= 60 else 'orange'
64-
badge = {
65-
'schemaVersion': 1,
66-
'label': 'coverage',
67-
'message': f'{coverage}%',
68-
'color': color
69-
}
70-
import json
72+
badge = {'schemaVersion': 1, 'label': 'coverage', 'message': f'{coverage}%', 'color': color}
7173
with open('.github/badges/coverage.json', 'w') as f:
7274
json.dump(badge, f)
73-
print(f'Created badge with {coverage}% coverage in {color}')
7475
"
75-
76-
- name: Commit coverage badge
77-
if: github.ref == 'refs/heads/main'
78-
run: |
79-
git config --local user.email "github-actions[bot]@users.noreply.github.com"
80-
git config --local user.name "github-actions[bot]"
76+
8177
git add .github/badges/coverage.json
8278
git diff --staged --quiet || git commit -m "Update coverage badge [skip ci]"
83-
git push
79+
git push origin badges

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Current Release](https://img.shields.io/github/release/facebookresearch/balance.svg)](https://github.com/facebookresearch/balance/releases)
88
[![Python 3.9+](https://img.shields.io/badge/Python-3.9+-fcbc2c.svg?logo=python&logoColor=white)](https://www.python.org/downloads/)
99
[![Build & Test](https://github.com/facebookresearch/balance/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/facebookresearch/balance/actions/workflows/build-and-test.yml?query=branch%3Amain)
10-
[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/facebookresearch/balance/main/.github/badges/coverage.json)](https://github.com/facebookresearch/balance/actions/workflows/coverage.yml?query=branch%3Amain)
10+
[![Coverage](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/facebookresearch/balance/badges/.github/badges/coverage.json)](https://github.com/facebookresearch/balance/actions/workflows/coverage.yml?query=branch%3Amain)
1111
[![CodeQL](https://github.com/facebookresearch/balance/actions/workflows/codeql.yml/badge.svg)](https://github.com/facebookresearch/balance/actions/workflows/codeql.yml?query=branch%3Amain)
1212
[![Deploy Website](https://github.com/facebookresearch/balance/actions/workflows/deploy-website.yml/badge.svg)](https://github.com/facebookresearch/balance/actions/workflows/deploy-website.yml?query=branch%3Amain)
1313
[![Release](https://github.com/facebookresearch/balance/actions/workflows/release.yml/badge.svg)](https://github.com/facebookresearch/balance/actions/workflows/release.yml?query=branch%3Amain)

0 commit comments

Comments
 (0)