Skip to content

chore: add code coverage INTER-859 #5

chore: add code coverage INTER-859

chore: add code coverage INTER-859 #5

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: PHPUnit for PR
run: docker-compose run phpunit --coverage-clover=cov/xml/clover-pr.xml
- 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-base.xml
- name: Calculate coverage for PR branch
id: pr_coverage
run: |
COVERED_STATEMENTS=$(grep -oP 'coveredstatements="\K[0-9]+' clover-pr.xml | paste -sd+ | bc)
TOTAL_STATEMENTS=$(grep -oP 'statements="\K[0-9]+' clover-pr.xml | paste -sd+ | bc)
COVERAGE_PR=$(echo "scale=2; $COVERED_STATEMENTS/$TOTAL_STATEMENTS*100" | bc)
echo "COVERAGE_PR=$COVERAGE_PR" >> $GITHUB_ENV
- name: Calculate coverage for base branch
id: base_coverage
run: |
COVERED_STATEMENTS=$(grep -oP 'coveredstatements="\K[0-9]+' clover-base.xml | paste -sd+ | bc)
TOTAL_STATEMENTS=$(grep -oP 'statements="\K[0-9]+' clover-base.xml | paste -sd+ | bc)
COVERAGE_BASE=$(echo "scale=2; $COVERED_STATEMENTS/$TOTAL_STATEMENTS*100" | bc)
echo "COVERAGE_BASE=$COVERAGE_BASE" >> $GITHUB_ENV
- name: Compare coverage
run: |
COVERAGE_DIFF=$(echo "$COVERAGE_PR - $COVERAGE_BASE" | bc)
echo "Coverage PR: $COVERAGE_PR%"
echo "Coverage Base: $COVERAGE_BASE%"
echo "Coverage Diff: $COVERAGE_DIFF%"
if [ $(echo "$COVERAGE_DIFF > 0" | bc) -eq 1 ]; then
COVERAGE_MESSAGE=":green_circle: Coverage increased by $COVERAGE_DIFF%"
elif [ $(echo "$COVERAGE_DIFF < 0" | bc) -eq 1 ]; 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 }}
run: |
PR_COMMENT=$(jq -n --arg message "$COVERAGE_MESSAGE" \
'{"body": "### Code Coverage Report\n\n**PR Coverage**: '$COVERAGE_PR'%\n**Base Coverage**: '$COVERAGE_BASE'%\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"