Skip to content

Workflow refactored #41

Workflow refactored

Workflow refactored #41

Workflow file for this run

name: Run Jest Tests on PR
on:
pull_request:
types:
- opened
- synchronize
jobs:
test:
name: Run Jest Tests
runs-on: ubuntu-latest
permissions: write-all
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install Dependencies
run: npm install
- name: Run Jest Tests
id: jest
run: |
npm test -- --json --outputFile=jest-results.json || echo "TESTS_FAILED=true" >> $GITHUB_ENV
- name: Read Jest Results
id: results
run: |
# Default TESTS_FAILED to false
if [ -z "$TESTS_FAILED" ]; then
TESTS_FAILED=false
fi
# Set TESTS_PASSED properly
if [ "$TESTS_FAILED" = "true" ]; then
TESTS_PASSED=false
echo "TESTS_PASSED=false" >> $GITHUB_ENV
FAILED_TESTS=$(jq -r '[.testResults[] | select(.status == "failed") | .name] | map(split("/") | last) | join("\n")' jest-results.json)
echo "FAILED_TESTS<<EOF" >> $GITHUB_ENV
echo "$FAILED_TESTS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
TESTS_PASSED=true
echo "TESTS_PASSED=true" >> $GITHUB_ENV
echo "FAILED_TESTS=None" >> $GITHUB_ENV
fi
# Extract PR number
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
# Generate a temporary file for the comment
COMMENT_FILE=$(mktemp)
# Save variables to GITHUB_OUTPUT
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "COMMENT_FILE=$COMMENT_FILE" >> $GITHUB_OUTPUT
# Generate content for the comment file
{
if [ "$TESTS_PASSED" = "true" ]; then
echo "✅ All Jest tests passed! This PR is ready to merge."
else
echo "❌ Some Jest tests failed. Please check the logs and fix the issues before merging."
echo ""
echo "**Failed Tests:**"
echo ""
echo '```'
echo "$FAILED_TESTS"
echo '```'
fi
} > "$COMMENT_FILE"
- name: PR comment
if: steps.pr-changes.outputs.changes != '[]'
run: |
COMMENT_BODY=$(cat "${{ steps.results.outputs.COMMENT_FILE }}")
# Debug output to ensure correct comment body format
echo "Comment body:"
echo "$COMMENT_BODY"
ESCAPED_COMMENT_BODY=$(echo "$COMMENT_BODY" | jq -Rs .)
# Debug output to show the escaped comment body
echo "Escaped Comment body:"
echo "$ESCAPED_COMMENT_BODY"
PR_NUMBER="${{ steps.results.outputs.PR_NUMBER }}"
# Define the GitHub Token
GH_TOKEN="${{ secrets.GITHUB_TOKEN }}"
# Define the GitHub API URL for posting the comment on the PR
COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
# Check if the comment body is correctly formatted
if [ -z "$ESCAPED_COMMENT_BODY" ]; then
echo "Error: ESCAPED_COMMENT_BODY is empty"
exit 1
fi
# Make the POST request to the GitHub API to create the comment
RESPONSE=$(curl -s -H "Authorization: token ${GH_TOKEN}" -X POST -d "{\"body\":$ESCAPED_COMMENT_BODY}" $COMMENT_URL)
# Debug output to inspect the response
echo "Response from GitHub API:"
echo "$RESPONSE"
# Check if the response contains an error message
ERROR_MESSAGE=$(echo "$RESPONSE" | jq -r .message)
if [ "$ERROR_MESSAGE" != "null" ]; then
echo "Error posting comment: $ERROR_MESSAGE"
exit 1 # Fail the step if an error occurs
else
echo "Successfully posted comment"
fi