Skip to content

chore: add code coverage INTER-859 #11

chore: add code coverage INTER-859

chore: add code coverage INTER-859 #11

Workflow file for this run

name: 'coverage-diff'
on:
pull_request:
branches:
- main
- chore/code-coverage # remove this before merge to main
- develop # remove this before merge to main
jobs:
coverage-diff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231
with:
php-version: 8.2
coverage: none
tools: composer:v2
extensions: xdebug
- name: Install Dependencies
run: composer install -q --profile --ignore-platform-reqs --no-interaction --no-ansi --no-scripts --no-suggest --prefer-dist
- uses: KengoTODA/actions-setup-docker-compose@4677f0d86d41e623c9c6e11e1d910976da297bc0
with:
version: '2.14.2'
- name: Set Permissions for Coverage Directory
run: |
mkdir -p cov/json cov/xml cov/html
chmod -R 777 cov
- name: "Create Empty env File for Docker"
run: touch .env
- name: Clear previous coverage data
run: rm -f cov/xml/clover-pr.xml cov/xml/clover-base.xml cov/xml/clover.xml
- name: PHPUnit for PR
run: docker-compose run phpunit --coverage-clover=cov/xml/clover.xml
- name: Calculate coverage for PR branch
id: pr_coverage
run: |
COVERAGE_PR=$(awk -F'"' '
/<metrics/ {
covered_statements += $10;
total_statements += $8;
}
END {
if (total_statements > 0) {
coverage = (covered_statements / total_statements) * 100;
if (coverage > 100) {
coverage = 100;
}
print coverage;
} else {
print 0;
}
}' cov/xml/clover.xml)
echo "COVERAGE_PR=$COVERAGE_PR" >> $GITHUB_ENV
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
- uses: shivammathur/setup-php@c541c155eee45413f5b09a52248675b1a2575231
with:
php-version: 8.2
coverage: none
tools: composer:v2
extensions: xdebug
- name: Install Dependencies
run: composer install -q --profile --ignore-platform-reqs --no-interaction --no-ansi --no-scripts --no-suggest --prefer-dist
- uses: KengoTODA/actions-setup-docker-compose@4677f0d86d41e623c9c6e11e1d910976da297bc0
with:
version: '2.14.2'
- name: Set Permissions for Coverage Directory
run: |
mkdir -p cov/json cov/xml cov/html
chmod -R 777 cov
- name: "Create Empty env File for Docker"
run: touch .env
- name: PHPUnit for Base
run: docker-compose run phpunit --coverage-clover=cov/xml/clover.xml
- name: Calculate coverage for base branch
id: base_coverage
run: |
COVERAGE_BASE=$(if [ -f cov/xml/clover.xml ]; then
awk -F'"' '
/<metrics/ {
covered_statements += $10;
total_statements += $8;
}
END {
if (total_statements > 0) {
print (covered_statements / total_statements) * 100;
} else {
print 0;
}
}' cov/xml/clover.xml
else
echo 0
fi)
echo "COVERAGE_BASE=$COVERAGE_BASE" >> $GITHUB_ENV
- name: Compare coverage
run: |
COVERAGE_DIFF=$(echo "$COVERAGE_PR - $COVERAGE_BASE" | awk '{printf "%.2f", $0}')
echo "Coverage PR: $COVERAGE_PR%"
echo "Coverage Base: $COVERAGE_BASE%"
echo "Coverage Diff: $COVERAGE_DIFF%"
if (( $(echo "$COVERAGE_DIFF > 0" | awk '{print ($0 > 0)}') )); then
COVERAGE_MESSAGE=":green_circle: Coverage increased by $COVERAGE_DIFF%"
elif (( $(echo "$COVERAGE_DIFF < 0" | awk '{print ($0 < 0)}') )); then
COVERAGE_MESSAGE=":red_circle: Coverage decreased by ${COVERAGE_DIFF#-}%"
else
COVERAGE_MESSAGE=":yellow_circle: Coverage remained the same."
fi
echo "COVERAGE_MESSAGE=$COVERAGE_MESSAGE" >> $GITHUB_ENV
- name: Post coverage comment to PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERAGE_PR: ${{ env.COVERAGE_PR }}
COVERAGE_BASE: ${{ env.COVERAGE_BASE }}
COVERAGE_MESSAGE: ${{ env.COVERAGE_MESSAGE }}
run: |
PR_COMMENT=$(jq -n --arg pr_coverage "$COVERAGE_PR" \
--arg base_coverage "$COVERAGE_BASE" \
--arg coverage_message "$COVERAGE_MESSAGE" \
'{"body": "### Code Coverage Report\n\n**PR Coverage**: \($pr_coverage)%\n**Base Coverage**: \($base_coverage)%\n\n\($coverage_message)"}')
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-X POST -d "$PR_COMMENT" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"