Skip to content

Commit 066c18c

Browse files
committed
feat(code-coverage): compares current and base branch code cov %
1 parent e1f4443 commit 066c18c

1 file changed

Lines changed: 124 additions & 6 deletions

File tree

.github/workflows/test.yml

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: CI
22

3-
on: [push, pull_request]
3+
on: [pull_request]
4+
permissions:
5+
pull-requests: write
6+
contents: read
47

58
jobs:
69
test:
@@ -41,17 +44,132 @@ jobs:
4144
run: |
4245
sudo apt-get update
4346
sudo apt-get install imagemagick
44-
- name: Check out code
47+
- name: Check out code on current branch
4548
uses: actions/checkout@v4
46-
- name: Install Ruby & gems
49+
- name: Install Ruby & gems on current branch
4750
uses: ruby/setup-ruby@v1
4851
with:
4952
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
50-
- name: Configure and initialize database
53+
- name: Configure and initialize database on current branch
5154
run: |
5255
cp test/config/test_tess.yml config/tess.yml
5356
cp config/secrets.github.yml config/secrets.yml
5457
cp config/ingestion.example.yml config/ingestion.yml
5558
bundle exec rake db:test:prepare
56-
- run: bundle exec rails test
57-
name: Run tests
59+
- name: Get all changed rb files
60+
id: changed-rb-files
61+
uses: tj-actions/changed-files@24d32ffd492484c1d75e0c0b894501ddb9d30d62 # v47
62+
with:
63+
files_ignore: 'test/**'
64+
files: '**.rb'
65+
- name: List all changed files rb files on current branch
66+
if: steps.changed-rb-files.outputs.any_changed == 'true'
67+
env:
68+
ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }}
69+
run: |
70+
for file in ${ALL_CHANGED_FILES}; do
71+
echo "$file was changed"
72+
done
73+
echo "ALL_CHANGED_FILES=${ALL_CHANGED_FILES}" >> $GITHUB_ENV
74+
# CURRENT branch tests
75+
- name: Run tests and extract current coverage on current branch
76+
id: current_coverage
77+
run: |
78+
bundle install
79+
bundle exec rails test
80+
81+
if [ -f coverage/.last_run.json ]; then
82+
CURRENT_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line')
83+
else
84+
CURRENT_COVERAGE="0.0"
85+
fi
86+
TABLE=$(echo -e "| File | Current coverage (%) |\n|------|--------------|")
87+
88+
echo "\n********** CODE COVERAGE **********"
89+
echo "\n*** Current coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $CURRENT_COVERAGE%"
90+
echo "*** ALL_CHANGED_FILES=${{ env.ALL_CHANGED_FILES }}"
91+
92+
for file in ${{ env.ALL_CHANGED_FILES }}; do
93+
COV=$(cat coverage/index.html| grep "\"$file\"" -A 1 | grep t-file__coverage | awk -F '>|%' '{print $2}')
94+
TABLE=$(echo -e "$TABLE\n| $file | $COV |")
95+
done
96+
echo "CURRENT_COV=$CURRENT_COVERAGE" >> $GITHUB_ENV
97+
echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
98+
{
99+
echo "TABLE<<EOF"
100+
echo -e "$TABLE"
101+
echo "EOF"
102+
} >> "$GITHUB_ENV"
103+
# BASE branch tests (usually master, but can be something else)
104+
- name: Checkout base branch
105+
uses: actions/checkout@v4
106+
with:
107+
ref: ${{ github.event.pull_request.base.ref }}
108+
path: base_branch
109+
- name: Install Ruby & gems on base
110+
uses: ruby/setup-ruby@v1
111+
with:
112+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
113+
- name: Configure and initialize database on base
114+
working-directory: base_branch
115+
run: |
116+
cp test/config/test_tess.yml config/tess.yml
117+
cp config/secrets.github.yml config/secrets.yml
118+
cp config/ingestion.example.yml config/ingestion.yml
119+
bundle install
120+
bundle exec rake db:test:prepare
121+
- name: Run tests and extract current coverage on base
122+
id: base_coverage
123+
working-directory: base_branch
124+
if: steps.changed-rb-files.outputs.any_changed == 'true'
125+
env:
126+
ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }}
127+
run: |
128+
bundle install
129+
bundle exec rails test
130+
131+
if [ -f coverage/.last_run.json ]; then
132+
BASE_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line')
133+
else
134+
BASE_COVERAGE="0.0"
135+
fi
136+
137+
echo "\n********** CODE COVERAGE **********"
138+
echo "\n*** Base coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $BASE_COVERAGE%"
139+
140+
echo "BASE_COV=$BASE_COVERAGE" >> $GITHUB_ENV
141+
echo "BASE_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
142+
# Compare coverages
143+
- name: Compare coverage
144+
id: compare
145+
run: |
146+
DIFF=$(echo "$CURRENT_COV - $BASE_COV" | bc)
147+
echo "DIFF=$DIFF" >> $GITHUB_ENV
148+
149+
echo "Current ($CURRENT_BRANCH)= $CURRENT_COV%"
150+
echo "Base ($BASE_BRANCH)= $BASE_COV%"
151+
if (( $(echo "$CURRENT_COV < $BASE_COV" | bc -l) )); then
152+
echo "❌ Coverage decreased by $DIFF%"
153+
echo "RESULT=0" >> $GITHUB_ENV
154+
else
155+
echo "✅ Coverage maintained or improved by $DIFF%"
156+
echo "RESULT=1" >> $GITHUB_ENV
157+
fi
158+
# Comment on PR
159+
- name: Post PR comment
160+
uses: thollander/actions-comment-pull-request@v3
161+
with:
162+
message: |
163+
Code Coverage Report
164+
165+
**Current (${{ env.CURRENT_BRANCH }}):** ${{ env.CURRENT_COV }}%
166+
**Base (${{ env.BASE_BRANCH }}):** ${{ env.BASE_COV }}%
167+
**Difference (current – base):** ${{ env.DIFF }}%
168+
**Result:** ✅ Code coverage has improved or is the same.
169+
170+
<details>
171+
<summary>Per-file coverage (only .rb files)</summary>
172+
173+
${{ env.TABLE }}
174+
175+
</details>

0 commit comments

Comments
 (0)