|
| 1 | +# |
| 2 | +# Copyright (c) 2025, RT-Thread Development Team |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# Change Logs: |
| 7 | +# Date Author Notes |
| 8 | +# 2025-10-27 GitHub Copilot Reusable workflow to post CI status |
| 9 | + |
| 10 | +name: Post CI Status Comment |
| 11 | + |
| 12 | +on: |
| 13 | + workflow_call: |
| 14 | + inputs: |
| 15 | + workflow_name: |
| 16 | + description: 'Name of the workflow' |
| 17 | + required: true |
| 18 | + type: string |
| 19 | + workflow_status: |
| 20 | + description: 'Status of the workflow (success/failure)' |
| 21 | + required: true |
| 22 | + type: string |
| 23 | + pr_number: |
| 24 | + description: 'Pull request number' |
| 25 | + required: true |
| 26 | + type: number |
| 27 | + |
| 28 | +permissions: |
| 29 | + pull-requests: write |
| 30 | + issues: write |
| 31 | + |
| 32 | +jobs: |
| 33 | + post-comment: |
| 34 | + runs-on: ubuntu-22.04 |
| 35 | + if: github.repository_owner == 'RT-Thread' |
| 36 | + steps: |
| 37 | + - name: Post or update CI status comment |
| 38 | + uses: actions/github-script@v7 |
| 39 | + with: |
| 40 | + script: | |
| 41 | + const prNumber = ${{ inputs.pr_number }}; |
| 42 | + const workflowName = '${{ inputs.workflow_name }}'; |
| 43 | + const workflowStatus = '${{ inputs.workflow_status }}'; |
| 44 | + |
| 45 | + // Status emoji mapping |
| 46 | + const statusEmoji = workflowStatus === 'success' ? '✅' : '❌'; |
| 47 | + const timestamp = new Date().toISOString(); |
| 48 | + |
| 49 | + // Try to find existing comment |
| 50 | + const comments = await github.rest.issues.listComments({ |
| 51 | + owner: context.repo.owner, |
| 52 | + repo: context.repo.repo, |
| 53 | + issue_number: prNumber |
| 54 | + }); |
| 55 | + |
| 56 | + const botComment = comments.data.find(comment => |
| 57 | + comment.user.login === 'github-actions[bot]' && |
| 58 | + comment.body.includes('<!-- CI Status Comment -->') |
| 59 | + ); |
| 60 | + |
| 61 | + // Get all workflow runs for this PR to build comprehensive status |
| 62 | + let allStatuses = {}; |
| 63 | + |
| 64 | + if (botComment) { |
| 65 | + // Parse existing statuses from comment |
| 66 | + const statusRegex = /- (✅|❌|🟡) \*\*(.+?)\*\*/g; |
| 67 | + let match; |
| 68 | + while ((match = statusRegex.exec(botComment.body)) !== null) { |
| 69 | + allStatuses[match[2]] = match[1]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Update current workflow status |
| 74 | + allStatuses[workflowName] = statusEmoji; |
| 75 | + |
| 76 | + // Build comment body |
| 77 | + let commentBody = '<!-- CI Status Comment -->\n'; |
| 78 | + commentBody += '## 🤖 CI Test Results\n\n'; |
| 79 | + commentBody += `**Last Updated:** ${timestamp}\n\n`; |
| 80 | + commentBody += '### Workflow Status:\n\n'; |
| 81 | + |
| 82 | + for (const [name, emoji] of Object.entries(allStatuses)) { |
| 83 | + commentBody += `- ${emoji} **${name}**\n`; |
| 84 | + } |
| 85 | + |
| 86 | + commentBody += '\n---\n'; |
| 87 | + commentBody += '✅ Success | ❌ Failure | 🟡 In Progress\n\n'; |
| 88 | + commentBody += '*This comment is automatically updated as CI workflows complete.*\n'; |
| 89 | + |
| 90 | + if (botComment) { |
| 91 | + // Update existing comment |
| 92 | + await github.rest.issues.updateComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + comment_id: botComment.id, |
| 96 | + body: commentBody |
| 97 | + }); |
| 98 | + console.log(`Updated comment ${botComment.id} on PR #${prNumber}`); |
| 99 | + } else { |
| 100 | + // Create new comment |
| 101 | + await github.rest.issues.createComment({ |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + issue_number: prNumber, |
| 105 | + body: commentBody |
| 106 | + }); |
| 107 | + console.log(`Created new comment on PR #${prNumber}`); |
| 108 | + } |
0 commit comments