post_ci_status.yml 3.8 KB

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