|
| 1 | +name: Run Jest Tests on PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - synchronize |
| 8 | + |
| 9 | +permissions: |
| 10 | + pull-requests: write |
| 11 | + contents: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + test: |
| 15 | + name: Run Jest Tests |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout Repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 # Ensures full history is fetched |
| 23 | + |
| 24 | + - name: Configure Git User |
| 25 | + run: | |
| 26 | + git config --global user.email "[email protected]" |
| 27 | + git config --global user.name "GitHub Actions" |
| 28 | +
|
| 29 | + - name: Merge PR with Master (Simulated) |
| 30 | + run: | |
| 31 | + git fetch origin master |
| 32 | + git checkout -b test-merged-pr |
| 33 | + git merge origin/master --no-edit |
| 34 | +
|
| 35 | + - name: Setup Node.js |
| 36 | + uses: actions/setup-node@v4 |
| 37 | + with: |
| 38 | + node-version: '20' |
| 39 | + |
| 40 | + - name: Install Dependencies |
| 41 | + run: npm install |
| 42 | + |
| 43 | + - name: Run Jest Tests |
| 44 | + id: jest |
| 45 | + run: | |
| 46 | + npm test -- --json --outputFile=jest-results.json || echo "TESTS_FAILED=true" >> $GITHUB_ENV |
| 47 | +
|
| 48 | + - name: Read Jest Results |
| 49 | + id: results |
| 50 | + run: | |
| 51 | + if [ "$TESTS_FAILED" = "true" ]; then |
| 52 | + echo "TESTS_PASSED=false" >> $GITHUB_ENV |
| 53 | + FAILED_TESTS=$(jq -r '[.testResults[] | select(.status == "failed") | .name] | map(split("/") | last) | join("\n")' jest-results.json) |
| 54 | + echo "FAILED_TESTS<<EOF" >> $GITHUB_ENV |
| 55 | + echo "$FAILED_TESTS" >> $GITHUB_ENV |
| 56 | + echo "EOF" >> $GITHUB_ENV |
| 57 | + else |
| 58 | + echo "TESTS_PASSED=true" >> $GITHUB_ENV |
| 59 | + echo "FAILED_TESTS=None" >> $GITHUB_ENV |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Post Comment on PR using GitHub CLI |
| 63 | + env: |
| 64 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + run: | |
| 66 | + PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") |
| 67 | + COMMENT_FILE=$(mktemp) |
| 68 | + { |
| 69 | + echo "${{ env.TESTS_PASSED == 'true' && '✅ All Jest tests passed! This PR is ready to merge.' || '❌ Some Jest tests failed. Please check the logs and fix the issues before merging.' }}" |
| 70 | + echo "" |
| 71 | + echo "**Failed Tests:**" |
| 72 | + echo "" |
| 73 | + echo '```' |
| 74 | + echo "${{ env.FAILED_TESTS }}" |
| 75 | + echo '```' |
| 76 | + } > "$COMMENT_FILE" |
| 77 | + gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE" |
0 commit comments