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