|
38 | 38 | name: Unit Tests |
39 | 39 | path: test-results.json |
40 | 40 | reporter: flutter-json |
| 41 | + |
| 42 | + - name: Parse test results |
| 43 | + if: always() |
| 44 | + id: test_summary |
| 45 | + run: | |
| 46 | + if [ -f test-results.json ]; then |
| 47 | + # Count total tests and failures from the JSON output |
| 48 | + TOTAL=$(grep -c '"type":"testStart"' test-results.json || echo "0") |
| 49 | + FAILED=$(grep -c '"result":"error"' test-results.json || echo "0") |
| 50 | + PASSED=$((TOTAL - FAILED)) |
| 51 | +
|
| 52 | + # Calculate percentage |
| 53 | + if [ $TOTAL -gt 0 ]; then |
| 54 | + PASS_RATE=$(awk "BEGIN {printf \"%.1f\", ($PASSED/$TOTAL)*100}") |
| 55 | + else |
| 56 | + PASS_RATE="0.0" |
| 57 | + fi |
| 58 | +
|
| 59 | + echo "total=$TOTAL" >> $GITHUB_OUTPUT |
| 60 | + echo "passed=$PASSED" >> $GITHUB_OUTPUT |
| 61 | + echo "failed=$FAILED" >> $GITHUB_OUTPUT |
| 62 | + echo "pass_rate=$PASS_RATE" >> $GITHUB_OUTPUT |
| 63 | + else |
| 64 | + echo "total=0" >> $GITHUB_OUTPUT |
| 65 | + echo "passed=0" >> $GITHUB_OUTPUT |
| 66 | + echo "failed=0" >> $GITHUB_OUTPUT |
| 67 | + echo "pass_rate=0.0" >> $GITHUB_OUTPUT |
| 68 | + fi |
| 69 | +
|
| 70 | + - name: Comment test results |
| 71 | + if: always() |
| 72 | + uses: actions/github-script@v7 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const total = '${{ steps.test_summary.outputs.total }}'; |
| 76 | + const passed = '${{ steps.test_summary.outputs.passed }}'; |
| 77 | + const failed = '${{ steps.test_summary.outputs.failed }}'; |
| 78 | + const passRate = '${{ steps.test_summary.outputs.pass_rate }}'; |
| 79 | +
|
| 80 | + const emoji = failed === '0' ? '✅' : '❌'; |
| 81 | + const status = failed === '0' ? 'All tests passed!' : `${failed} test(s) failed`; |
| 82 | +
|
| 83 | + const body = `## ${emoji} Test Results |
| 84 | +
|
| 85 | + **Status:** ${status} |
| 86 | +
|
| 87 | + | Metric | Value | |
| 88 | + |--------|-------| |
| 89 | + | Total Tests | ${total} | |
| 90 | + | Passed | ✅ ${passed} | |
| 91 | + | Failed | ❌ ${failed} | |
| 92 | + | Pass Rate | ${passRate}% | |
| 93 | +
|
| 94 | + ${failed !== '0' ? '⚠️ Please check the **Unit Tests** check run for detailed failure information.' : ''} |
| 95 | +
|
| 96 | + <sub>🤖 Automated test results from [checks workflow](${context.payload.repository.html_url}/actions/runs/${context.runId})</sub>`; |
| 97 | +
|
| 98 | + // Find existing comment |
| 99 | + const comments = await github.rest.issues.listComments({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + issue_number: context.issue.number, |
| 103 | + }); |
| 104 | +
|
| 105 | + const botComment = comments.data.find(comment => |
| 106 | + comment.user.type === 'Bot' && |
| 107 | + comment.body.includes('## ✅ Test Results') || comment.body.includes('## ❌ Test Results') |
| 108 | + ); |
| 109 | +
|
| 110 | + if (botComment) { |
| 111 | + // Update existing comment |
| 112 | + await github.rest.issues.updateComment({ |
| 113 | + owner: context.repo.owner, |
| 114 | + repo: context.repo.repo, |
| 115 | + comment_id: botComment.id, |
| 116 | + body: body |
| 117 | + }); |
| 118 | + } else { |
| 119 | + // Create new comment |
| 120 | + await github.rest.issues.createComment({ |
| 121 | + owner: context.repo.owner, |
| 122 | + repo: context.repo.repo, |
| 123 | + issue_number: context.issue.number, |
| 124 | + body: body |
| 125 | + }); |
| 126 | + } |
0 commit comments