Skip to content

Commit

Permalink
Merge pull request #1670 from terrestris/fix-coverage-comment
Browse files Browse the repository at this point in the history
ci: fix coverage-comment for changed files
  • Loading branch information
TreffN committed Jun 28, 2024
2 parents 38ca0e5 + 9189880 commit 9149f51
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,57 @@ jobs:

- name: Run tests related to current changes ✅
run: |
npx jest --collectCoverageFrom='["**/*.{ts,tsx,js,jsx}"]' --coverage --coverageReporters json-summary --onlyChanged --coverageDirectory ./coverage/changed
git fetch origin main
git diff --name-only origin/main
npx jest --config=jest.config.js --coverage --coverageReporters json-summary --onlyChanged --coverageDirectory ./coverage/changed
- name: Check for test results ❓
id: check-results
run: |
if [ -f ./coverage/changed/coverage-summary.json ]; then
COVERAGE_CONTENT=$(cat ./coverage/changed/coverage-summary.json)
if echo "$COVERAGE_CONTENT" | jq '.total.lines.pct' | grep -q 'Unknown'; then
echo "no_tests_found=true" >> $GITHUB_ENV
else
echo "no_tests_found=false" >> $GITHUB_ENV
fi
else
echo "no_tests_found=true" >> $GITHUB_ENV
fi
- name: Construct jest coverage comment input 💬
id: construct-input
run: |
if [ "${{ env.no_tests_found }}" == "true" ]; then
echo "MULTIPLE_FILES=All, ./coverage/all/coverage-summary.json" >> $GITHUB_ENV
else
echo "MULTIPLE_FILES=All, ./coverage/all/coverage-summary.json\nonly changed, ./coverage/changed/coverage-summary.json" >> $GITHUB_ENV
fi
- name: Jest Coverage Comment 💬
uses: MishaKav/jest-coverage-comment@main
with:
hide-comment: false
create-new-comment: false
hide-summary: false
multiple-files: |
All, ./coverage/all/coverage-summary.json
only changed, ./coverage/changed/coverage-summary.json
multiple-files: ${{ env.MULTIPLE_FILES }}

- name: Get Coverage Comment Id ➡️
if: env.no_tests_found == 'true'
id: get-comment-id
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
COMMENTS_JSON=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments")
COVERAGE_COMMENT_ID=$(echo "${COMMENTS_JSON}" | jq -r 'map(select(.user.login == "github-actions[bot]")) | sort_by(.created_at) | .[0].id')
echo "COVERAGE_COMMENT_ID=${COVERAGE_COMMENT_ID}" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Post To No Tests Found Comment ⚠️
if: env.no_tests_found == 'true'
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ env.COVERAGE_COMMENT_ID }}
body: |
❌ No tests were found related to the files changed

0 comments on commit 9149f51

Please sign in to comment.