1- name : Auto Issue from CodeQL
1+ name : Auto Copilot Autofix
22
33on :
4- code_scanning_alert :
5- types : [created]
4+ workflow_run :
5+ workflows : ["CodeQL"]
6+ types : [completed]
67
78jobs :
8- create_issue :
9+ auto-fix :
910 runs-on : ubuntu-latest
1011 permissions :
11- issues : write
12+ security-events : read
13+ contents : write
14+ pull-requests : write
15+
1216 steps :
13- - name : Create GitHub Issue
14- uses : actions/github-script@v9
15- with :
16- script : |
17- const alert = context.payload.alert;
18-
19- // 1. 获取当前警报的严重级别 (转成小写以防大小写不统一)
20- const severity = (alert.rule.security_severity_level || alert.rule.severity || 'unknown').toLowerCase();
21-
22- // 2. 定义我们允许提 Issue 的白名单级别
23- // 注意:加入了 'critical' (极高) 以及 'error' (某些规则可能使用这个词)
24- const allowedSeverities = ['critical', 'high', 'medium', 'error'];
25-
26- // 3. 拦截:如果当前级别不在白名单里,直接退出脚本,不创建 Issue
27- if (!allowedSeverities.includes(severity)) {
28- console.log(`[跳过] 当前漏洞级别为 '${severity}',未达到提 Issue 的标准。`);
29- return; // 直接 return,后面的代码就不会执行了
30- }
31-
32- console.log(`[通过] 发现级别为 '${severity}' 的漏洞,准备创建 Issue...`);
33-
34- // 4. 组装 Issue 的标题和内容
35- const issueTitle = `[安全扫描 - ${severity.toUpperCase()}] ${alert.rule.description}`;
36- const issueBody = `
37- ### 🚨 CodeQL 发现新的安全警告
38-
39- **问题类型:** ${alert.rule.name}
40- **严重程度:** ${severity.toUpperCase()}
41- **文件路径:** \`${alert.most_recent_instance.location.path}\`
42- **代码行数:** 第 ${alert.most_recent_instance.location.start_line} 行
43-
44- [👉 点击此处查看详细报告与修复建议](${alert.html_url})
45- `;
46-
47- // 5. 调用 GitHub API 创建 Issue
48- await github.rest.issues.create({
49- owner: context.repo.owner,
50- repo: context.repo.repo,
51- title: issueTitle,
52- body: issueBody,
53- // 根据需要可以打上不同的 tag,比如高危漏洞打个紧急标签
54- labels: ['security', 'bug', 'codeql', `severity:${severity}`]
55- });
17+ - name : Get open CodeQL alerts and trigger Autofix
18+ env :
19+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
20+ OWNER : ${{ github.repository_owner }}
21+ REPO : ${{ github.event.repository.name }}
22+ run : |
23+ # 获取所有 open 的 code scanning alerts
24+ ALERTS=$(gh api /repos/$OWNER/$REPO/code-scanning/alerts \
25+ --jq '[.[] | select(.state=="open") | .number]')
26+
27+ echo "Found alerts: $ALERTS"
28+
29+ # 对每个 alert 触发 autofix
30+ for NUMBER in $(echo $ALERTS | jq -r '.[]'); do
31+ echo "Triggering autofix for alert #$NUMBER"
32+
33+ # 1. 生成 fix
34+ gh api -X POST \
35+ /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix
36+
37+ # 2. 等待 fix 生成(轮询)
38+ sleep 30
39+
40+ # 3. 获取 fix 状态
41+ STATUS=$(gh api \
42+ /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix \
43+ --jq '.status')
44+
45+ echo "Fix status: $STATUS"
46+
47+ if [ "$STATUS" = "succeeded" ]; then
48+ # 4. 提交 fix(创建新 branch + PR)
49+ gh api -X POST \
50+ /repos/$OWNER/$REPO/code-scanning/alerts/$NUMBER/autofix/commits \
51+ -f target_ref="autofix/alert-$NUMBER"
52+
53+ echo "✅ Fix committed for alert #$NUMBER"
54+ else
55+ echo "⚠️ Fix not available for alert #$NUMBER"
56+ fi
57+ done
0 commit comments