pr_clang_format.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. name: Code Format with Clang-Format
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. exclude_patterns:
  6. description: "排除文件/目录 (以逗号间隔)\n Files/Directories to exclude(comma-separated)"
  7. required: false
  8. default: ''
  9. branch:
  10. description: "要格式化的分支 | Branch to format"
  11. required: true
  12. default: ''
  13. pr_number:
  14. description: "PR编号 | PR Number"
  15. required: true
  16. default: ''
  17. concurrency:
  18. group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  19. cancel-in-progress: true
  20. permissions:
  21. contents: write
  22. pull-requests: read
  23. jobs:
  24. format-code:
  25. if: |
  26. github.repository_owner != 'RT-Thread'
  27. runs-on: ubuntu-latest
  28. steps:
  29. - name: Checkout code
  30. uses: actions/checkout@v4
  31. with:
  32. ref: ${{ github.event.inputs.branch }}
  33. fetch-depth: 0
  34. token: ${{ secrets.GITHUB_TOKEN }}
  35. lfs: false
  36. - name: Install clang-format
  37. run: sudo apt-get update && sudo apt-get install -y clang-format
  38. - name: Check clang-format version
  39. run: |
  40. echo "📋 clang-format version information:"
  41. clang-format --version
  42. echo "📋 Detailed version info:"
  43. clang-format -version
  44. # 检查支持的功能
  45. echo "📋 Checking supported features..."
  46. clang-format --help | grep -i "align\|consecutive" || echo "No align/consecutive options found"
  47. - name: Get PR info (files and author)
  48. id: get-pr-info
  49. run: |
  50. max_retries=3
  51. retry_count=0
  52. changed_files=""
  53. api_response=""
  54. # 获取PR编号(workflow_dispatch时需要手动输入)
  55. PR_NUMBER="${{ github.event.inputs.pr_number }}"
  56. if [ -z "$PR_NUMBER" ]; then
  57. echo "Error: PR number is required"
  58. exit 1
  59. fi
  60. echo "Fetching PR info for #$PR_NUMBER..."
  61. # 获取PR的详细信息(包括作者)
  62. pr_response=$(curl -s \
  63. -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
  64. -H "Accept: application/vnd.github.v3+json" \
  65. "https://api.github.com/repos/RT-Thread/rt-thread/pulls/$PR_NUMBER")
  66. # 获取PR作者的GitHub用户名
  67. PR_AUTHOR=$(jq -r '.user.login' <<<"$pr_response")
  68. echo "PR Author: $PR_AUTHOR"
  69. # 使用GitHub noreply邮箱格式
  70. PR_AUTHOR_EMAIL="${PR_AUTHOR}+github[bot]@noreply.github.com"
  71. echo "pr_author=$PR_AUTHOR" >> $GITHUB_OUTPUT
  72. echo "pr_author_email=$PR_AUTHOR_EMAIL" >> $GITHUB_OUTPUT
  73. echo "Fetching changed files for PR #$PR_NUMBER..."
  74. while [ $retry_count -lt $max_retries ]; do
  75. # 使用一个curl调用同时获取响应内容和状态码
  76. api_response=$(curl -s -w "\n%{http_code}" \
  77. -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
  78. -H "Accept: application/vnd.github.v3+json" \
  79. "https://api.github.com/repos/RT-Thread/rt-thread/pulls/$PR_NUMBER/files")
  80. # 分离HTTP状态码和响应内容
  81. http_status=$(echo "$api_response" | tail -1)
  82. api_response=$(echo "$api_response" | sed '$d')
  83. echo "HTTP Status: $http_status"
  84. # 检查HTTP状态码
  85. if [ "$http_status" -ne 200 ]; then
  86. echo "Retry $((retry_count+1)): HTTP $http_status - API response error"
  87. echo "API Response: $api_response"
  88. sleep 5
  89. ((retry_count++))
  90. continue
  91. fi
  92. # 验证响应是否为有效JSON且包含文件数组
  93. if jq -e 'if type=="array" then .[0].filename else empty end' <<<"$api_response" >/dev/null 2>&1; then
  94. changed_files=$(jq -r '.[].filename' <<<"$api_response")
  95. break
  96. else
  97. echo "Retry $((retry_count+1)): API response not ready or invalid format"
  98. echo "API Response: $api_response"
  99. sleep 5
  100. ((retry_count++))
  101. fi
  102. done
  103. if [ -z "$changed_files" ]; then
  104. echo "Error: Failed to get changed files after $max_retries attempts"
  105. echo "Final API Response: $api_response"
  106. exit 1
  107. fi
  108. # 将文件列表转换为逗号分隔格式
  109. changed_files_comma=$(echo "$changed_files" | tr '\n' ',' | sed 's/,$//')
  110. echo "Successfully fetched $(echo "$changed_files" | wc -l) changed files"
  111. # 设置输出
  112. echo "all_changed_files=$changed_files_comma" >> $GITHUB_OUTPUT
  113. echo "changed_files_count=$(echo "$changed_files" | wc -l)" >> $GITHUB_OUTPUT
  114. - name: Find source files to format
  115. id: find-files
  116. run: |
  117. # 获取PR中修改的文件
  118. CHANGED_FILES="${{ steps.get-pr-info.outputs.all_changed_files }}"
  119. # 将逗号分隔的文件列表转换为换行分隔
  120. CHANGED_FILES_LINES=$(echo "$CHANGED_FILES" | tr ',' '\n')
  121. # 美化打印PR中修改的文件
  122. echo "📋 PR中修改的文件列表:"
  123. echo "┌───────────────────────────────────────────────────────"
  124. count=1
  125. while IFS= read -r file; do
  126. if [ -n "$file" ]; then
  127. echo "│ $count. $file"
  128. ((count++))
  129. fi
  130. done <<< "$CHANGED_FILES_LINES"
  131. echo "└───────────────────────────────────────────────────────"
  132. echo "总共修改了 $((count-1)) 个文件"
  133. # 如果没有修改的文件,退出
  134. if [ -z "$CHANGED_FILES" ]; then
  135. echo "❌ PR中没有修改的文件"
  136. echo "files_count=0" >> $GITHUB_OUTPUT
  137. exit 0
  138. fi
  139. # 继续使用CHANGED_FILES进行后续处理
  140. CHANGED_FILES="$CHANGED_FILES_LINES"
  141. # 过滤出需要格式化的源文件(扩展clang-format支持的文件类型)
  142. FILES=""
  143. while IFS= read -r file; do
  144. if [ -n "$file" ] && [[ "$file" =~ \.(cpp|h|c|hpp|cc|hh|C|H|cp|cxx|hxx|inc|inl|ipp|tpp|txx)$ ]]; then
  145. FILES="$FILES$file"$'\n'
  146. fi
  147. done <<< "$CHANGED_FILES"
  148. FILES=$(echo "$FILES" | sort | uniq)
  149. # 处理排除模式
  150. EXCLUDE_PATTERNS="${{ github.event.inputs.exclude_patterns }}"
  151. if [ -n "$EXCLUDE_PATTERNS" ] && [ -n "$FILES" ]; then
  152. IFS=',' read -ra PATTERNS <<< "$EXCLUDE_PATTERNS"
  153. for pattern in "${PATTERNS[@]}"; do
  154. pattern=$(echo "$pattern" | xargs) # 去除空格
  155. if [ -n "$pattern" ]; then
  156. # 去除末尾的斜杠(如果有)
  157. pattern=${pattern%/}
  158. echo "排除模式: $pattern"
  159. # 使用 grep 过滤排除模式
  160. FILES=$(echo "$FILES" | grep -v "$pattern" || echo "$FILES")
  161. fi
  162. done
  163. fi
  164. if [ -z "$FILES" ]; then
  165. echo "❌ 没有需要格式化的文件(可能都被排除了)"
  166. echo "files_count=0" >> $GITHUB_OUTPUT
  167. exit 0
  168. fi
  169. # 显示找到的文件用于调试
  170. echo "🎯 需要格式化的文件:"
  171. echo "┌───────────────────────────────────────────────────────"
  172. count=1
  173. while IFS= read -r file; do
  174. if [ -n "$file" ]; then
  175. echo "│ $count. $file"
  176. ((count++))
  177. fi
  178. done <<< "$FILES"
  179. echo "└───────────────────────────────────────────────────────"
  180. FILE_COUNT=$(echo "$FILES" | wc -l)
  181. echo "找到 $FILE_COUNT 个需要格式化的文件"
  182. echo "files_count=$FILE_COUNT" >> $GITHUB_OUTPUT
  183. # 将文件列表保存为多行输出
  184. echo "files_list<<EOF" >> $GITHUB_OUTPUT
  185. echo "$FILES" >> $GITHUB_OUTPUT
  186. echo "EOF" >> $GITHUB_OUTPUT
  187. - name: Clean up temporary files
  188. run: |
  189. rm -f changed_files.txt
  190. rm -f format_files.sh
  191. echo "✅ 临时文件清理完成"
  192. - name: Format code with clang-format
  193. if: steps.find-files.outputs.files_count != '0'
  194. run: |
  195. echo "开始格式化代码..."
  196. FILES="${{ steps.find-files.outputs.files_list }}"
  197. # 使用clang-format批量格式化文件
  198. echo "$FILES" | xargs -I {} sh -c '
  199. file="{}"
  200. if [ -f "$file" ]; then
  201. echo "📝 格式化: $file"
  202. clang-format -style=file -i "$file"
  203. if [ $? -eq 0 ]; then
  204. echo "✅ 格式化成功: $file"
  205. else
  206. echo "❌ 格式化失败: $file"
  207. exit 1
  208. fi
  209. else
  210. echo "⚠️ 文件不存在: $file"
  211. fi
  212. '
  213. echo "✅ 代码格式化完成"
  214. - name: Check for changes
  215. id: check-changes
  216. run: |
  217. if git diff --quiet; then
  218. echo "✅ 代码无需格式化"
  219. echo "has_changes=false" >> $GITHUB_OUTPUT
  220. else
  221. echo "📋 检测到格式化更改:"
  222. git diff --name-only
  223. echo "has_changes=true" >> $GITHUB_OUTPUT
  224. fi
  225. - name: Commit and push changes
  226. if: steps.check-changes.outputs.has_changes == 'true'
  227. run: |
  228. # 使用PR作者作为commit author,避免CLA检查失败
  229. # CLA检查只识别PR作者(已签署CLA),而不识别github-actions[bot]
  230. PR_AUTHOR="${{ steps.get-pr-info.outputs.pr_author }}"
  231. PR_AUTHOR_EMAIL="${{ steps.get-pr-info.outputs.pr_author_email }}"
  232. git config --local user.email "$PR_AUTHOR_EMAIL"
  233. git config --local user.name "$PR_AUTHOR"
  234. # 使用GIT_AUTHOR环境变量让commit以贡献者名义提交
  235. export GIT_AUTHOR_NAME="$PR_AUTHOR"
  236. export GIT_AUTHOR_EMAIL="$PR_AUTHOR_EMAIL"
  237. git add -A
  238. git commit -m "style: format code with clang-format [skip ci]"
  239. git push origin HEAD:${{ github.event.inputs.branch }}
  240. echo "✅ 代码格式化完成并已推送到分支 ${{ github.event.inputs.branch }}"
  241. - name: Summary
  242. run: |
  243. echo "=== 格式化总结 ==="
  244. echo "分支: ${{ github.event.inputs.branch }}"
  245. echo "排除模式: ${{ github.event.inputs.exclude_patterns || '无' }}"
  246. echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
  247. echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
  248. echo "clang-format 版本: $(clang-format --version | head -1)"