pr_format_bot.yml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. name: PR Format Notification
  2. on:
  3. pull_request_target:
  4. types: [opened, synchronize]
  5. permissions:
  6. pull-requests: write
  7. contents: read
  8. jobs:
  9. notify-format:
  10. if: github.repository_owner == 'RT-Thread'
  11. runs-on: ubuntu-latest
  12. steps:
  13. - name: Check if first commit and add comment
  14. env:
  15. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  16. run: |
  17. echo "Event action: ${{ github.event.action }}"
  18. # 获取 PR 的提交信息
  19. commits=$(curl -s \
  20. -H "Accept: application/vnd.github.v3+json" \
  21. -H "Authorization: Bearer $GITHUB_TOKEN" \
  22. "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits")
  23. # 检查 API 响应是否为数组
  24. if echo "$commits" | jq -e 'type == "array"' > /dev/null; then
  25. commit_count=$(echo "$commits" | jq '. | length')
  26. echo "PR commit count: $commit_count"
  27. should_comment=false
  28. if [ "${{ github.event.action }}" = "opened" ]; then
  29. should_comment=true
  30. elif [ "${{ github.event.action }}" = "synchronize" ] && [ "$commit_count" -eq 1 ]; then
  31. should_comment=true
  32. fi
  33. if [ "$should_comment" = true ]; then
  34. echo "Adding format notification comment..."
  35. # 构建工作流链接
  36. branch="${{ github.event.pull_request.head.ref }}"
  37. fork_repo="${{ github.event.pull_request.head.repo.full_name }}"
  38. workflow_url="https://github.com/${fork_repo}/actions/workflows/clang-format.yml"
  39. direct_link="${workflow_url}?branch=${branch}"
  40. # 使用数组存储多行消息
  41. message_lines=(
  42. "**👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!**"
  43. ""
  44. "为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。"
  45. "To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below."
  46. ""
  47. "---"
  48. ""
  49. "### 🛠 操作步骤 | Steps"
  50. ""
  51. "1. **前往 Actions 页面 | Go to the Actions page**"
  52. "[点击进入工作流 → | Click to open workflow →](${direct_link})"
  53. ""
  54. "2. **点击 \`Run workflow\` | Click \`Run workflow\`**"
  55. "- 设置需排除的文件/目录(目录请以\"/\"结尾)"
  56. "Set files/directories to exclude (directories should end with \"/\")"
  57. "- 将目标分支设置为 \ Set the target branch to:**\`${branch}\`**"
  58. "- 设置PR number为 \ Set the PR number to:**\`${{ github.event.number }}\`**"
  59. ""
  60. "3. **等待工作流完成 | Wait for the workflow to complete**"
  61. "格式化后的代码将自动推送至你的分支。"
  62. "The formatted code will be automatically pushed to your branch."
  63. ""
  64. "完成后,提交将自动更新至 \`${branch}\` 分支,关联的 Pull Request 也会同步更新。"
  65. "Once completed, commits will be pushed to the \`${branch}\` branch automatically, and the related Pull Request will be updated."
  66. ""
  67. "如有问题欢迎联系我们,再次感谢您的贡献!💐"
  68. "If you have any questions, feel free to reach out. Thanks again for your contribution!"
  69. )
  70. # 拼接数组为多行字符串
  71. message=$(printf "%s\n" "${message_lines[@]}")
  72. echo "Message content:"
  73. echo "$message"
  74. # 使用 jq 安全地构建 JSON 负载
  75. json_payload=$(jq -n --arg body "$message" '{"body": $body}')
  76. # 发送评论到 PR
  77. response=$(curl -s -w "\n%{http_code}" \
  78. -X POST \
  79. -H "Accept: application/vnd.github.v3+json" \
  80. -H "Authorization: Bearer $GITHUB_TOKEN" \
  81. "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
  82. -d "$json_payload")
  83. # 提取 HTTP 状态码和响应体
  84. http_code=$(echo "$response" | tail -n1)
  85. response_body=$(echo "$response" | sed '$d')
  86. if [ "$http_code" -eq 201 ]; then
  87. echo "Format notification comment added successfully"
  88. echo "Comment URL: $(echo "$response_body" | jq -r '.html_url')"
  89. else
  90. echo "Failed to add comment. HTTP status: $http_code"
  91. echo "Response: $response_body"
  92. exit 1
  93. fi
  94. else
  95. echo "Not the first commit, skipping comment"
  96. fi
  97. else
  98. echo "Failed to get commits from GitHub API"
  99. echo "Response: $commits"
  100. exit 1
  101. fi