|
1 | | -name: CI Tests with Detailed Report |
| 1 | +name: CI Test Report Publisher |
| 2 | +description: 'Publishes detailed test reports as GitHub comments with smart updates' |
2 | 3 |
|
3 | 4 | inputs: |
4 | | - commentBody: |
5 | | - type: string |
6 | | - default: '' |
7 | | - description: Path to summary report html file |
8 | | - |
| 5 | + comment-body: |
| 6 | + description: 'Path to the summary report HTML file' |
| 7 | + required: true |
| 8 | + default: 'test-results/report.html' |
| 9 | + report-title: |
| 10 | + description: 'Title for the test report comment' |
| 11 | + required: false |
| 12 | + default: '🧪 CI Test Summary Report' |
| 13 | + max-comment-length: |
| 14 | + description: 'Maximum comment length to avoid API limits' |
| 15 | + required: false |
| 16 | + default: '65536' |
9 | 17 |
|
10 | 18 | runs: |
11 | 19 | using: composite |
12 | 20 | steps: |
13 | | - - uses: actions/checkout@v4 |
14 | | - - name: Post readable test report |
15 | | - shell: bash -xe {0} |
| 21 | + - name: Validate inputs |
| 22 | + shell: bash |
| 23 | + run: | |
| 24 | + if [[ ! -f "${{ inputs.comment-body }}" ]]; then |
| 25 | + echo "❌ Report file not found: ${{ inputs.comment-body }}" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +
|
| 29 | + - name: Post test report |
| 30 | + shell: bash |
| 31 | + env: |
| 32 | + REPORT_FILE: "${{ inputs.comment-body }}" |
| 33 | + MAX_LENGTH: "${{ inputs.max-comment-length }}" |
| 34 | + run: | |
| 35 | + # Read and sanitize report content |
| 36 | + report_content=$(< "$REPORT_FILE") |
| 37 | +
|
| 38 | + # Truncate if exceeding GitHub comment limits |
| 39 | + if [ ${#report_content} -gt $MAX_LENGTH ]; then |
| 40 | + echo "⚠️ Report content too long, truncating..." |
| 41 | + report_content="${report_content:0:$MAX_LENGTH}" |
| 42 | + report_content+="\n\n---\n*Report truncated due to length limits*" |
| 43 | + fi |
| 44 | +
|
| 45 | + # Escape content for JSON |
| 46 | + escaped_content=$(jq -R -s -c '.' <<< "$report_content") |
| 47 | +
|
| 48 | + echo "escaped_content<<EOF" >> $GITHUB_ENV |
| 49 | + echo "$escaped_content" >> $GITHUB_ENV |
| 50 | + echo "EOF" >> $GITHUB_ENV |
| 51 | +
|
| 52 | + - name: Publish or update comment |
16 | 53 | uses: actions/github-script@v7 |
| 54 | + env: |
| 55 | + COMMENT_BODY: ${{ env.comment_body }} |
17 | 56 | with: |
18 | 57 | script: | |
19 | | - // Read the comment content from a file |
20 | | - const fs = require('fs'); |
21 | | - const commentBody = fs.readFileSync('${{ inputs.commentBody }}', 'utf8'); |
22 | | - // Get existing comments |
23 | | - const { data: comments } = await github.rest.issues.listComments({ |
24 | | - owner: context.repo.owner, |
25 | | - repo: context.repo.repo, |
26 | | - issue_number: context.issue.number, |
27 | | - }); |
28 | | - // Find existing bot comment |
29 | | - const existingComment = comments.find(comment => |
30 | | - comment.user.type === 'Bot' && |
31 | | - comment.body.includes('# 🧪 CI Test Summary Report') |
32 | | - ); |
33 | | - if (existingComment) { |
34 | | - await github.rest.issues.updateComment({ |
35 | | - owner: context.repo.owner, |
36 | | - repo: context.repo.repo, |
37 | | - comment_id: existingComment.id, |
38 | | - body: commentBody |
39 | | - }); |
40 | | - } else { |
41 | | - await github.rest.issues.createComment({ |
| 58 | + const { COMMENT_BODY } = process.env; |
| 59 | +
|
| 60 | + // Helper function to find bot comments |
| 61 | + const findBotComment = async () => { |
| 62 | + const { data: comments } = await github.rest.issues.listComments({ |
42 | 63 | owner: context.repo.owner, |
43 | 64 | repo: context.repo.repo, |
44 | 65 | issue_number: context.issue.number, |
45 | | - body: commentBody |
46 | 66 | }); |
| 67 | +
|
| 68 | + return comments.find(comment => |
| 69 | + comment.user.type === 'Bot' && |
| 70 | + comment.body.includes('${{ inputs.report-title }}') |
| 71 | + ); |
| 72 | + }; |
| 73 | +
|
| 74 | + try { |
| 75 | + const existingComment = await findBotComment(); |
| 76 | +
|
| 77 | + if (existingComment) { |
| 78 | + console.log('📝 Updating existing test report comment'); |
| 79 | + await github.rest.issues.updateComment({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + comment_id: existingComment.id, |
| 83 | + body: COMMENT_BODY |
| 84 | + }); |
| 85 | + } else { |
| 86 | + console.log('💬 Creating new test report comment'); |
| 87 | + await github.rest.issues.createComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + issue_number: context.issue.number, |
| 91 | + body: COMMENT_BODY |
| 92 | + }); |
| 93 | + } |
| 94 | +
|
| 95 | + console.log('✅ Test report published successfully'); |
| 96 | + } catch (error) { |
| 97 | + console.error('❌ Failed to publish test report:', error.message); |
| 98 | + throw error; |
47 | 99 | } |
0 commit comments