4848
4949 echo "--- Alert #$NUMBER [$SEC_LEVEL] ---"
5050
51+ # 检查是否已有 autofix
5152 EXISTING=$(gh api \
5253 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \
5354 --jq '.status' 2>/dev/null || echo "none")
@@ -77,14 +78,14 @@ jobs:
7778 continue
7879 fi
7980
81+ # 检查分支是否已存在
8082 BRANCH_STATUS=$(gh api \
8183 /repos/$OWNER/$REPO/git/refs/heads/$BRANCH \
8284 --silent 2>/dev/null && echo "exists" || echo "not_found")
8385 echo "DEBUG branch status: $BRANCH_STATUS"
8486
85- HAS_CODE_FIX=false
86-
8787 if [ "$BRANCH_STATUS" = "not_found" ]; then
88+ # 创建分支
8889 SHA=$(gh api /repos/$OWNER/$REPO/git/refs/heads/$DEFAULT_BRANCH \
8990 --jq '.object.sha')
9091
@@ -93,19 +94,22 @@ jobs:
9394 -f sha="$SHA" 2>/dev/null || true
9495 echo "🌿 Created branch: $BRANCH"
9596
97+ # 提交 fix
9698 COMMIT_RESULT=$(gh api -X POST \
9799 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix/commits \
98100 -f target_ref="$BRANCH" 2>&1)
99101 echo "DEBUG commit result: $COMMIT_RESULT"
100102
101103 if echo "$COMMIT_RESULT" | grep -q "target_ref"; then
102104 echo "✅ Committed fix to branch: $BRANCH"
103- HAS_CODE_FIX=true
104105 else
105- echo "⚠️ No code changes generated, advisory PR only"
106- HAS_CODE_FIX=false
106+ echo "⚠️ No code changes generated, deleting branch and skipping"
107+ gh api -X DELETE \
108+ /repos/$OWNER/$REPO/git/refs/heads/$BRANCH 2>/dev/null || true
109+ continue
107110 fi
108111 else
112+ # 分支已存在,检查是否已有 open PR
109113 EXISTING_PR=$(gh pr list \
110114 --repo "$OWNER/$REPO" \
111115 --head "$BRANCH" \
@@ -119,9 +123,9 @@ jobs:
119123 fi
120124
121125 echo "🌿 Branch exists, creating PR with existing branch"
122- HAS_CODE_FIX=true
123126 fi
124127
128+ # 获取 alert 详情
125129 ALERT_INFO=$(gh api \
126130 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER)
127131
@@ -137,12 +141,7 @@ jobs:
137141 /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \
138142 --jq '.description // "暂无 AI 修复说明"')
139143
140- if [ "$HAS_CODE_FIX" = "true" ]; then
141- FIX_NOTE="> 此 PR 包含 Copilot Autofix 自动生成的代码修复,请审核后 merge。"
142- else
143- FIX_NOTE="> ⚠️ **此 alert 没有自动代码修复**,AI 仅提供了修复建议,需要**手动按照上方 AI 修复思路修改代码**后再 merge。"
144- fi
145-
144+ # 创建 Draft PR
146145 gh pr create \
147146 --repo "$OWNER/$REPO" \
148147 --base "$DEFAULT_BRANCH" \
@@ -189,8 +188,8 @@ jobs:
189188
190189 ---
191190
192- $FIX_NOTE " && \
193- echo "🎉 PR created for alert #$NUMBER" || \
194- echo "❌ Failed to create PR for alert #$NUMBER"
195-
196- done
191+ > 此 PR 由 GitHub Copilot Autofix 自动生成,请仔细审核后再 merge。 " && \
192+ echo "🎉 PR created for alert #$NUMBER" || \
193+ echo "❌ Failed to create PR for alert #$NUMBER"
194+
195+ done
0 commit comments