summaryrefslogtreecommitdiff
path: root/.github/workflows/post_ci_status.yml
blob: 1f24592c8003dbde05f5516f1440ba352a0197a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# Copyright (c) 2025, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date           Author       Notes
# 2025-10-27     GitHub Copilot  Reusable workflow to post CI status

name: Post CI Status Comment

# on:
#   workflow_call:
#     inputs:
#       workflow_name:
#         description: 'Name of the workflow'
#         required: true
#         type: string
#       workflow_status:
#         description: 'Status of the workflow (success/failure)'
#         required: true
#         type: string
#       pr_number:
#         description: 'Pull request number'
#         required: true
#         type: number

permissions:
  pull-requests: write
  issues: write

jobs:
  post-comment:
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'RT-Thread'
    steps:
      - name: Post or update CI status comment
        uses: actions/github-script@v7
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          script: |
            const prNumber = ${{ inputs.pr_number }};
            const workflowName = '${{ inputs.workflow_name }}';
            const workflowStatus = '${{ inputs.workflow_status }}';
            
            // Status emoji mapping
            const statusEmoji = workflowStatus === 'success' ? '✅' : '❌';
            const timestamp = new Date().toISOString();
            
            // Try to find existing comment
            const comments = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: prNumber
            });
            
            const botComment = comments.data.find(comment => 
              comment.user.login === 'github-actions[bot]' && 
              comment.body.includes('<!-- CI Status Comment -->')
            );
            
            // Get all workflow runs for this PR to build comprehensive status
            let allStatuses = {};
            
            if (botComment) {
              // Parse existing statuses from comment
              const statusRegex = /- (✅|❌|🟡) \*\*(.+?)\*\*/g;
              let match;
              while ((match = statusRegex.exec(botComment.body)) !== null) {
                allStatuses[match[2]] = match[1];
              }
            }
            
            // Update current workflow status
            allStatuses[workflowName] = statusEmoji;
            
            // Build comment body
            let commentBody = '<!-- CI Status Comment -->\n';
            commentBody += '## 🤖 CI Test Results\n\n';
            commentBody += `**Last Updated:** ${timestamp}\n\n`;
            commentBody += '### Workflow Status:\n\n';
            
            for (const [name, emoji] of Object.entries(allStatuses)) {
              commentBody += `- ${emoji} **${name}**\n`;
            }
            
            commentBody += '\n---\n';
            commentBody += '✅ Success | ❌ Failure | 🟡 In Progress\n\n';
            commentBody += '*This comment is automatically updated as CI workflows complete.*\n';
            
            if (botComment) {
              // Update existing comment
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: botComment.id,
                body: commentBody
              });
              console.log(`Updated comment ${botComment.id} on PR #${prNumber}`);
            } else {
              // Create new comment
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: prNumber,
                body: commentBody
              });
              console.log(`Created new comment on PR #${prNumber}`);
            }