Skip to content

Commit 25907ce

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

1 file changed

Lines changed: 109 additions & 6 deletions

File tree

.github/workflows/test.yml

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

3-
on: [push, pull_request]
3+
on: [pull_request]
4+
permissions:
5+
contents: read
46

57
jobs:
68
test:
@@ -41,17 +43,118 @@ jobs:
4143
run: |
4244
sudo apt-get update
4345
sudo apt-get install imagemagick
44-
- name: Check out code
46+
- name: Check out code on current branch
4547
uses: actions/checkout@v4
46-
- name: Install Ruby & gems
48+
- name: Install Ruby & gems on current branch
4749
uses: ruby/setup-ruby@v1
4850
with:
4951
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
50-
- name: Configure and initialize database
52+
- name: Configure and initialize database on current branch
5153
run: |
5254
cp test/config/test_tess.yml config/tess.yml
5355
cp config/secrets.github.yml config/secrets.yml
5456
cp config/ingestion.example.yml config/ingestion.yml
5557
bundle exec rake db:test:prepare
56-
- run: bundle exec rails test
57-
name: Run tests
58+
- name: Get all changed rb files
59+
id: changed-rb-files
60+
uses: tj-actions/changed-files@24d32ffd492484c1d75e0c0b894501ddb9d30d62 # v47
61+
with:
62+
files_ignore: 'test/**'
63+
files: '**.rb'
64+
- name: List all changed files rb files on current branch
65+
if: steps.changed-rb-files.outputs.any_changed == 'true'
66+
env:
67+
ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }}
68+
run: |
69+
for file in ${ALL_CHANGED_FILES}; do
70+
echo "$file was changed"
71+
done
72+
echo "ALL_CHANGED_FILES=${ALL_CHANGED_FILES}" >> $GITHUB_ENV
73+
# CURRENT branch tests
74+
- name: Run tests and extract current coverage on current branch
75+
id: current_coverage
76+
run: |
77+
bundle install
78+
bundle exec rails test
79+
80+
if [ -f coverage/.last_run.json ]; then
81+
CURRENT_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line')
82+
else
83+
CURRENT_COVERAGE="0.0"
84+
fi
85+
TABLE=$(echo -e "| File | Current coverage (%) |\n|------|--------------|")
86+
87+
echo "\n********** CODE COVERAGE **********"
88+
echo "\n*** Current coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $CURRENT_COVERAGE%"
89+
echo "*** ALL_CHANGED_FILES=${{ env.ALL_CHANGED_FILES }}"
90+
91+
for file in ${{ env.ALL_CHANGED_FILES }}; do
92+
COV=$(cat coverage/index.html| grep "\"$file\"" -A 1 | grep t-file__coverage | awk -F '>|%' '{print $2}')
93+
TABLE=$(echo -e "$TABLE\n| $file | $COV |")
94+
done
95+
echo "CURRENT_COV=$CURRENT_COVERAGE" >> $GITHUB_ENV
96+
echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
97+
{
98+
echo "TABLE<<EOF"
99+
echo -e "$TABLE"
100+
echo "EOF"
101+
} >> "$GITHUB_ENV"
102+
# BASE branch tests (usually master, but can be something else)
103+
- name: Checkout base branch
104+
uses: actions/checkout@v4
105+
if: steps.changed-rb-files.outputs.any_changed == 'true'
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+
if: steps.changed-rb-files.outputs.any_changed == 'true'
112+
with:
113+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
114+
- name: Configure and initialize database on base
115+
working-directory: base_branch
116+
if: steps.changed-rb-files.outputs.any_changed == 'true'
117+
run: |
118+
cp test/config/test_tess.yml config/tess.yml
119+
cp config/secrets.github.yml config/secrets.yml
120+
cp config/ingestion.example.yml config/ingestion.yml
121+
bundle install
122+
bundle exec rake db:test:prepare
123+
- name: Run tests and extract current coverage on base
124+
id: base_coverage
125+
working-directory: base_branch
126+
if: steps.changed-rb-files.outputs.any_changed == 'true'
127+
env:
128+
ALL_CHANGED_FILES: ${{ steps.changed-rb-files.outputs.all_changed_files }}
129+
run: |
130+
bundle install
131+
bundle exec rails test
132+
133+
if [ -f coverage/.last_run.json ]; then
134+
BASE_COVERAGE=$(cat coverage/.last_run.json | jq -r '.result.line')
135+
else
136+
BASE_COVERAGE="0.0"
137+
fi
138+
139+
echo "\n********** CODE COVERAGE **********"
140+
echo "\n*** Base coverage (branch: $(git rev-parse --abbrev-ref HEAD)): $BASE_COVERAGE%"
141+
142+
echo "BASE_COV=$BASE_COVERAGE" >> $GITHUB_ENV
143+
echo "BASE_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
144+
# Compare coverages
145+
- name: Compare coverage
146+
id: compare
147+
if: steps.changed-rb-files.outputs.any_changed == 'true'
148+
run: |
149+
DIFF=$(echo "$CURRENT_COV - $BASE_COV" | bc)
150+
echo "DIFF=$DIFF" >> $GITHUB_ENV
151+
152+
echo "Current ($CURRENT_BRANCH)= $CURRENT_COV%"
153+
echo "Base ($BASE_BRANCH)= $BASE_COV%"
154+
if (( $(echo "$CURRENT_COV < $BASE_COV" | bc -l) )); then
155+
echo "❌ Coverage decreased by $DIFF%"
156+
echo "RESULT=0" >> $GITHUB_ENV
157+
else
158+
echo "✅ Coverage maintained or improved by $DIFF%"
159+
echo "RESULT=1" >> $GITHUB_ENV
160+
fi

0 commit comments

Comments
 (0)