|
| 1 | +name: Auto Copilot Autofix (High & Medium Only) |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + workflow_run: |
| 6 | + workflows: ["CodeQL Advanced"] |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +jobs: |
| 10 | + auto-fix: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }} |
| 13 | + permissions: |
| 14 | + security-events: read |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Trigger Autofix for High & Medium alerts |
| 20 | + env: |
| 21 | + GH_TOKEN: ${{ secrets.AUTOFIX_TOKEN }} |
| 22 | + OWNER: ${{ github.repository_owner }} |
| 23 | + REPO: ${{ github.event.repository.name }} |
| 24 | + run: | |
| 25 | + set +e |
| 26 | + DEFAULT_BRANCH=$(gh api /repos/$OWNER/$REPO --jq '.default_branch') |
| 27 | + echo "Default branch: $DEFAULT_BRANCH" |
| 28 | +
|
| 29 | + ALERTS=$(gh api "/repos/$OWNER/$REPO/code-scanning/alerts?state=open&per_page=100" \ |
| 30 | + --jq '[.[] | select(.rule.security_severity_level == "high" or .rule.security_severity_level == "medium" or .rule.severity == "warning") | {number: .number, level: (.rule.security_severity_level // .rule.severity)}]') |
| 31 | +
|
| 32 | + COUNT=$(echo $ALERTS | jq 'length') |
| 33 | + echo "Found $COUNT alerts with high / medium / warning" |
| 34 | + echo "$ALERTS" | jq -r '.[] | " Alert #\(.number) [\(.level)]"' |
| 35 | +
|
| 36 | + if [ "$COUNT" -eq 0 ]; then |
| 37 | + echo "No alerts to process, exiting." |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + for ROW in $(echo $ALERTS | jq -r '.[] | @base64'); do |
| 42 | + _jq() { echo "$ROW" | base64 -d | jq -r "$1"; } |
| 43 | +
|
| 44 | + NUMBER=$(_jq '.number') |
| 45 | + SEC_LEVEL=$(_jq '.level') |
| 46 | + BRANCH="autofix/${SEC_LEVEL}/alert-${NUMBER}" |
| 47 | +
|
| 48 | + echo "--- Alert #$NUMBER [$SEC_LEVEL] ---" |
| 49 | +
|
| 50 | + # 检查是否已有 autofix |
| 51 | + EXISTING=$(gh api \ |
| 52 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \ |
| 53 | + --jq '.status' 2>/dev/null || echo "none") |
| 54 | +
|
| 55 | + if [ "$EXISTING" = "success" ]; then |
| 56 | + echo "✅ Fix already exists" |
| 57 | + else |
| 58 | + echo "⏳ Generating fix..." |
| 59 | + gh api -X POST \ |
| 60 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix || { |
| 61 | + echo "⚠️ Failed to trigger autofix for #$NUMBER, skipping" |
| 62 | + continue |
| 63 | + } |
| 64 | +
|
| 65 | + for i in 1 2 3; do |
| 66 | + sleep 30 |
| 67 | + EXISTING=$(gh api \ |
| 68 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \ |
| 69 | + --jq '.status' 2>/dev/null || echo "none") |
| 70 | + echo " Attempt $i: status = $EXISTING" |
| 71 | + [ "$EXISTING" = "success" ] && break |
| 72 | + done |
| 73 | + fi |
| 74 | +
|
| 75 | + if [ "$EXISTING" != "success" ]; then |
| 76 | + echo "⚠️ Autofix not available for alert #$NUMBER (status: $EXISTING), skipping" |
| 77 | + continue |
| 78 | + fi |
| 79 | +
|
| 80 | + # 检查分支是否已存在 |
| 81 | + BRANCH_STATUS=$(gh api \ |
| 82 | + /repos/$OWNER/$REPO/git/refs/heads/$BRANCH \ |
| 83 | + --silent 2>/dev/null && echo "exists" || echo "not_found") |
| 84 | + echo "DEBUG branch status: $BRANCH_STATUS" |
| 85 | +
|
| 86 | + if [ "$BRANCH_STATUS" = "not_found" ]; then |
| 87 | + # 创建分支 |
| 88 | + SHA=$(gh api /repos/$OWNER/$REPO/git/refs/heads/$DEFAULT_BRANCH \ |
| 89 | + --jq '.object.sha') |
| 90 | +
|
| 91 | + gh api -X POST /repos/$OWNER/$REPO/git/refs \ |
| 92 | + -f ref="refs/heads/$BRANCH" \ |
| 93 | + -f sha="$SHA" 2>/dev/null || true |
| 94 | + echo "🌿 Created branch: $BRANCH" |
| 95 | +
|
| 96 | + # 提交 fix |
| 97 | + COMMIT_RESULT=$(gh api -X POST \ |
| 98 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix/commits \ |
| 99 | + -f target_ref="$BRANCH" 2>&1) |
| 100 | + echo "DEBUG commit result: $COMMIT_RESULT" |
| 101 | +
|
| 102 | + if echo "$COMMIT_RESULT" | grep -q "target_ref"; then |
| 103 | + echo "✅ Committed fix to branch: $BRANCH" |
| 104 | + else |
| 105 | + echo "⚠️ No code changes generated, deleting branch and skipping" |
| 106 | + gh api -X DELETE \ |
| 107 | + /repos/$OWNER/$REPO/git/refs/heads/$BRANCH 2>/dev/null || true |
| 108 | + continue |
| 109 | + fi |
| 110 | + else |
| 111 | + # 分支已存在,检查是否已有 open PR |
| 112 | + EXISTING_PR=$(gh pr list \ |
| 113 | + --repo "$OWNER/$REPO" \ |
| 114 | + --head "$BRANCH" \ |
| 115 | + --state open \ |
| 116 | + --json number \ |
| 117 | + --jq '.[0].number // empty') |
| 118 | +
|
| 119 | + if [ -n "$EXISTING_PR" ]; then |
| 120 | + echo "⏭️ PR #$EXISTING_PR already exists, skipping" |
| 121 | + continue |
| 122 | + fi |
| 123 | +
|
| 124 | + echo "🌿 Branch exists, creating PR with existing branch" |
| 125 | + fi |
| 126 | +
|
| 127 | + # 获取 alert 详情 |
| 128 | + ALERT_INFO=$(gh api \ |
| 129 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER) |
| 130 | +
|
| 131 | + ALERT_TITLE=$(echo $ALERT_INFO | jq -r '.rule.description') |
| 132 | + ALERT_HELP=$(echo $ALERT_INFO | jq -r '.rule.help // "暂无详细说明"' | head -c 800) |
| 133 | + ALERT_TAGS=$(echo $ALERT_INFO | jq -r '.rule.tags // [] | join(", ")') |
| 134 | + ALERT_FILE=$(echo $ALERT_INFO | jq -r '.most_recent_instance.location.path // "未知文件"') |
| 135 | + ALERT_LINE=$(echo $ALERT_INFO | jq -r '.most_recent_instance.location.start_line // "未知行"') |
| 136 | + ALERT_URL=$(echo $ALERT_INFO | jq -r '.html_url') |
| 137 | + CWE_TAGS=$(echo $ALERT_INFO | jq -r '[.rule.tags[] | select(startswith("external/cwe/"))] | join(", ")') |
| 138 | +
|
| 139 | + AUTOFIX_DESC=$(gh api \ |
| 140 | + /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \ |
| 141 | + --jq '.description // "暂无 AI 修复说明"') |
| 142 | +
|
| 143 | + # 创建 Draft PR |
| 144 | + gh pr create \ |
| 145 | + --repo "$OWNER/$REPO" \ |
| 146 | + --base "$DEFAULT_BRANCH" \ |
| 147 | + --head "$BRANCH" \ |
| 148 | + --draft \ |
| 149 | + --title "[Autofix][$SEC_LEVEL] Alert #$NUMBER: $ALERT_TITLE" \ |
| 150 | + --body "## 🤖 Copilot Autofix 自动修复报告 |
| 151 | +
|
| 152 | + --- |
| 153 | +
|
| 154 | + ### 📋 基本信息 |
| 155 | +
|
| 156 | + | 字段 | 内容 | |
| 157 | + |------|------| |
| 158 | + | **Alert ID** | [#$NUMBER]($ALERT_URL) | |
| 159 | + | **安全级别** | $SEC_LEVEL | |
| 160 | + | **规则名称** | $ALERT_TITLE | |
| 161 | + | **问题文件** | \`$ALERT_FILE\` 第 $ALERT_LINE 行 | |
| 162 | + | **CWE 分类** | $CWE_TAGS | |
| 163 | + | **规则标签** | $ALERT_TAGS | |
| 164 | +
|
| 165 | + --- |
| 166 | +
|
| 167 | + ### 🔍 问题说明 |
| 168 | +
|
| 169 | + $ALERT_HELP |
| 170 | +
|
| 171 | + --- |
| 172 | +
|
| 173 | + ### 🤖 AI 修复思路 |
| 174 | +
|
| 175 | + $AUTOFIX_DESC |
| 176 | +
|
| 177 | + --- |
| 178 | +
|
| 179 | + ### ✅ Review 检查清单 |
| 180 | +
|
| 181 | + - [ ] 理解了漏洞的成因和影响范围 |
| 182 | + - [ ] 确认 AI 修复逻辑正确,没有遗漏边界情况 |
| 183 | + - [ ] 确认修复没有改变原有业务逻辑 |
| 184 | + - [ ] 确认没有引入新的安全问题 |
| 185 | + - [ ] CI / 单元测试全部通过 |
| 186 | + - [ ] 如有必要,已补充对应的测试用例 |
| 187 | +
|
| 188 | + --- |
| 189 | +
|
| 190 | + > 此 PR 由 GitHub Copilot Autofix 自动生成,请仔细审核后再 merge。" && \ |
| 191 | + echo "🎉 PR created for alert #$NUMBER" || \ |
| 192 | + echo "❌ Failed to create PR for alert #$NUMBER" |
| 193 | +
|
| 194 | + done |
0 commit comments