pr_clang_format.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 changed files from PR
  48. id: get-pr-files
  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 changed files for PR #$PR_NUMBER..."
  61. while [ $retry_count -lt $max_retries ]; do
  62. # 使用一个curl调用同时获取响应内容和状态码
  63. api_response=$(curl -s -w "\n%{http_code}" \
  64. -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
  65. -H "Accept: application/vnd.github.v3+json" \
  66. "https://api.github.com/repos/RT-Thread/rt-thread/pulls/$PR_NUMBER/files")
  67. # 分离HTTP状态码和响应内容
  68. http_status=$(echo "$api_response" | tail -1)
  69. api_response=$(echo "$api_response" | sed '$d')
  70. echo "HTTP Status: $http_status"
  71. # 检查HTTP状态码
  72. if [ "$http_status" -ne 200 ]; then
  73. echo "Retry $((retry_count+1)): HTTP $http_status - API response error"
  74. echo "API Response: $api_response"
  75. sleep 5
  76. ((retry_count++))
  77. continue
  78. fi
  79. # 验证响应是否为有效JSON且包含文件数组
  80. if jq -e 'if type=="array" then .[0].filename else empty end' <<<"$api_response" >/dev/null 2>&1; then
  81. changed_files=$(jq -r '.[].filename' <<<"$api_response")
  82. break
  83. else
  84. echo "Retry $((retry_count+1)): API response not ready or invalid format"
  85. echo "API Response: $api_response"
  86. sleep 5
  87. ((retry_count++))
  88. fi
  89. done
  90. if [ -z "$changed_files" ]; then
  91. echo "Error: Failed to get changed files after $max_retries attempts"
  92. echo "Final API Response: $api_response"
  93. exit 1
  94. fi
  95. # 将文件列表转换为逗号分隔格式
  96. changed_files_comma=$(echo "$changed_files" | tr '\n' ',' | sed 's/,$//')
  97. echo "Successfully fetched $(echo "$changed_files" | wc -l) changed files"
  98. # 设置输出
  99. echo "all_changed_files=$changed_files_comma" >> $GITHUB_OUTPUT
  100. echo "changed_files_count=$(echo "$changed_files" | wc -l)" >> $GITHUB_OUTPUT
  101. - name: Find source files to format
  102. id: find-files
  103. run: |
  104. # 获取PR中修改的文件
  105. CHANGED_FILES="${{ steps.get-pr-files.outputs.all_changed_files }}"
  106. # 将逗号分隔的文件列表转换为换行分隔
  107. CHANGED_FILES_LINES=$(echo "$CHANGED_FILES" | tr ',' '\n')
  108. # 美化打印PR中修改的文件
  109. echo "📋 PR中修改的文件列表:"
  110. echo "┌───────────────────────────────────────────────────────"
  111. count=1
  112. while IFS= read -r file; do
  113. if [ -n "$file" ]; then
  114. echo "│ $count. $file"
  115. ((count++))
  116. fi
  117. done <<< "$CHANGED_FILES_LINES"
  118. echo "└───────────────────────────────────────────────────────"
  119. echo "总共修改了 $((count-1)) 个文件"
  120. # 如果没有修改的文件,退出
  121. if [ -z "$CHANGED_FILES" ]; then
  122. echo "❌ PR中没有修改的文件"
  123. echo "files_count=0" >> $GITHUB_OUTPUT
  124. exit 0
  125. fi
  126. # 继续使用CHANGED_FILES进行后续处理
  127. CHANGED_FILES="$CHANGED_FILES_LINES"
  128. # 过滤出需要格式化的源文件(扩展clang-format支持的文件类型)
  129. FILES=""
  130. while IFS= read -r file; do
  131. if [ -n "$file" ] && [[ "$file" =~ \.(cpp|h|c|hpp|cc|hh|C|H|cp|cxx|hxx|inc|inl|ipp|tpp|txx)$ ]]; then
  132. FILES="$FILES$file"$'\n'
  133. fi
  134. done <<< "$CHANGED_FILES"
  135. FILES=$(echo "$FILES" | sort | uniq)
  136. # 处理排除模式
  137. EXCLUDE_PATTERNS="${{ github.event.inputs.exclude_patterns }}"
  138. if [ -n "$EXCLUDE_PATTERNS" ] && [ -n "$FILES" ]; then
  139. IFS=',' read -ra PATTERNS <<< "$EXCLUDE_PATTERNS"
  140. for pattern in "${PATTERNS[@]}"; do
  141. pattern=$(echo "$pattern" | xargs) # 去除空格
  142. if [ -n "$pattern" ]; then
  143. # 去除末尾的斜杠(如果有)
  144. pattern=${pattern%/}
  145. echo "排除模式: $pattern"
  146. # 使用 grep 过滤排除模式
  147. FILES=$(echo "$FILES" | grep -v "$pattern" || echo "$FILES")
  148. fi
  149. done
  150. fi
  151. if [ -z "$FILES" ]; then
  152. echo "❌ 没有需要格式化的文件(可能都被排除了)"
  153. echo "files_count=0" >> $GITHUB_OUTPUT
  154. exit 0
  155. fi
  156. # 显示找到的文件用于调试
  157. echo "🎯 需要格式化的文件:"
  158. echo "┌───────────────────────────────────────────────────────"
  159. count=1
  160. while IFS= read -r file; do
  161. if [ -n "$file" ]; then
  162. echo "│ $count. $file"
  163. ((count++))
  164. fi
  165. done <<< "$FILES"
  166. echo "└───────────────────────────────────────────────────────"
  167. FILE_COUNT=$(echo "$FILES" | wc -l)
  168. echo "找到 $FILE_COUNT 个需要格式化的文件"
  169. echo "files_count=$FILE_COUNT" >> $GITHUB_OUTPUT
  170. # 将文件列表保存为多行输出
  171. echo "files_list<<EOF" >> $GITHUB_OUTPUT
  172. echo "$FILES" >> $GITHUB_OUTPUT
  173. echo "EOF" >> $GITHUB_OUTPUT
  174. - name: Clean up temporary files
  175. run: |
  176. rm -f changed_files.txt
  177. rm -f format_files.sh
  178. echo "✅ 临时文件清理完成"
  179. - name: Format code with clang-format
  180. if: steps.find-files.outputs.files_count != '0'
  181. run: |
  182. echo "开始格式化代码..."
  183. FILES="${{ steps.find-files.outputs.files_list }}"
  184. # 使用clang-format批量格式化文件
  185. echo "$FILES" | xargs -I {} sh -c '
  186. file="{}"
  187. if [ -f "$file" ]; then
  188. echo "📝 格式化: $file"
  189. clang-format -style=file -i "$file"
  190. if [ $? -eq 0 ]; then
  191. echo "✅ 格式化成功: $file"
  192. else
  193. echo "❌ 格式化失败: $file"
  194. exit 1
  195. fi
  196. else
  197. echo "⚠️ 文件不存在: $file"
  198. fi
  199. '
  200. echo "✅ 代码格式化完成"
  201. - name: Check for changes
  202. id: check-changes
  203. run: |
  204. if git diff --quiet; then
  205. echo "✅ 代码无需格式化"
  206. echo "has_changes=false" >> $GITHUB_OUTPUT
  207. else
  208. echo "📋 检测到格式化更改:"
  209. git diff --name-only
  210. echo "has_changes=true" >> $GITHUB_OUTPUT
  211. fi
  212. - name: Commit and push changes
  213. if: steps.check-changes.outputs.has_changes == 'true'
  214. run: |
  215. git config --local user.email "github-actions[bot]@users.noreply.github.com"
  216. git config --local user.name "github-actions[bot]"
  217. git add -A
  218. git commit -m "style: format code with clang-format [skip ci]"
  219. git push origin HEAD:${{ github.event.inputs.branch }}
  220. echo "✅ 代码格式化完成并已推送到分支 ${{ github.event.inputs.branch }}"
  221. - name: Summary
  222. run: |
  223. echo "=== 格式化总结 ==="
  224. echo "分支: ${{ github.event.inputs.branch }}"
  225. echo "排除模式: ${{ github.event.inputs.exclude_patterns || '无' }}"
  226. echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
  227. echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
  228. echo "clang-format 版本: $(clang-format --version | head -1)"