diff --git a/.github/workflows/on-pull-request.yml b/.github/workflows/on-pull-request.yml index 44b7227e7..b4802c406 100644 --- a/.github/workflows/on-pull-request.yml +++ b/.github/workflows/on-pull-request.yml @@ -34,7 +34,32 @@ 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 @@ -42,6 +67,24 @@ jobs: 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