2020 OWNER : ${{ github.repository_owner }}
2121 REPO : ${{ github.event.repository.name }}
2222 run : |
23+ # 获取默认分支名
24+ DEFAULT_BRANCH=$(gh api /repos/$OWNER/$REPO --jq '.default_branch')
25+ echo "Default branch: $DEFAULT_BRANCH"
26+
2327 for SEVERITY in "critical" "error" "warning"; do
2428 echo "====== Processing severity: $SEVERITY ======"
2529
4145 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \
4246 --jq '.status' 2>/dev/null || echo "none")
4347
44- if [ "$EXISTING" = "success" ]; then
48+ if [ "$EXISTING" = "success" ]; then
4549 echo "✅ Fix already exists, committing directly..."
4650 else
4751 echo "⏳ Generating fix..."
@@ -57,17 +61,31 @@ jobs:
5761 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \
5862 --jq '.status' 2>/dev/null || echo "none")
5963 echo " Attempt $i: status = $EXISTING"
60- [ "$EXISTING" = "success" ] && break
64+ [ "$EXISTING" = "success" ] && break
6165 done
6266 fi
6367
64- if [ "$EXISTING" = "success" ]; then
68+ if [ "$EXISTING" = "success" ]; then
6569 BRANCH="autofix/${SEVERITY}/alert-${NUMBER}"
70+
71+ # 获取默认分支最新的 SHA
72+ SHA=$(gh api /repos/$OWNER/$REPO/git/refs/heads/$DEFAULT_BRANCH \
73+ --jq '.object.sha')
74+
75+ # 先创建分支
76+ gh api -X POST /repos/$OWNER/$REPO/git/refs \
77+ -f ref="refs/heads/$BRANCH" \
78+ -f sha="$SHA" 2>/dev/null && \
79+ echo "🌿 Created branch: $BRANCH" || \
80+ echo "🌿 Branch already exists: $BRANCH"
81+
82+ # 再提交 fix 到该分支
6683 gh api -X POST \
6784 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix/commits \
6885 -f target_ref="$BRANCH" && \
6986 echo "✅ Committed fix to branch: $BRANCH" || \
7087 echo "❌ Failed to commit fix for alert #$NUMBER"
88+
7189 else
7290 echo "⚠️ Autofix not available for alert #$NUMBER (status: $EXISTING), skipping"
7391 fi
0 commit comments