Skip to content

Commit 23075b9

Browse files
committed
Add script to generate autofixes for codeql
1 parent 51130ac commit 23075b9

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

github/codeql-autofix.ps1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<#
2+
.SYNOPSIS
3+
Fetch code scanning alerts in an org and attempt creating autofixes
4+
#>
5+
6+
param (
7+
[string] [Parameter(Mandatory=$false)] $org = "miljodir",
8+
[string] [Parameter(Mandatory=$false)] $tool = "CodeQL",
9+
[string] [Parameter(Mandatory=$false)] $ruleFilter = "cs/*"
10+
)
11+
12+
if ($org -eq "miljodir") {
13+
$env:jwt = (node ../../local-repo-sync/authapp/app.js $org | ConvertFrom-Json | Select-Object token -ExpandProperty token)
14+
}
15+
16+
#$criticalAlerts = gh api --method GET "/orgs/$org/code-scanning/alerts?state=open&tool_name=$tool&severity=critical" --paginate | ConvertFrom-Json
17+
$alerts = gh api --method GET "/orgs/$org/code-scanning/alerts?state=open&tool_name=$tool" --paginate | ConvertFrom-Json
18+
$filteredAlerts = $alerts | Where-Object { $_.rule.id -like $ruleFilter -and ($_.most_recent_instance.classifications -ne "generated" -or "" -eq $_.most_recent_instance.classifications) }
19+
$repofilter = "myrepo"
20+
#$filteredAlerts = $filteredAlerts | Where-Object { $_.repository.name -like $repofilter }
21+
22+
# Group alerts by repository
23+
$alertsByRepo = $filteredAlerts | Group-Object -Property { $_.repository.name }
24+
25+
26+
foreach ($alert in $filteredAlerts) {
27+
$alertNumber = $alert.number
28+
$repo = $alert.repository.name
29+
Write-Host "Attempting to create autofix for alert $alertNumber in repo $repo"
30+
gh api `
31+
--method POST `
32+
/repos/$org/$repo/code-scanning/alerts/$alertNumber/autofix
33+
}
34+
35+
36+
function CreateBranchFromDefault {
37+
param (
38+
[Parameter(Mandatory = $true)]
39+
[string]$org,
40+
[Parameter(Mandatory = $true)]
41+
[string]$repo,
42+
[Parameter(Mandatory = $true)]
43+
[string]$newBranch
44+
)
45+
46+
# Get the default branch
47+
$defaultBranch = gh api /repos/$org/$repo | ConvertFrom-Json | Select-Object -ExpandProperty default_branch
48+
49+
# Get the latest commit SHA from the default branch
50+
$latestCommitSha = gh api /repos/$org/$repo/git/ref/heads/$defaultBranch | ConvertFrom-Json | Select-Object -ExpandProperty object | Select-Object -ExpandProperty sha
51+
52+
# Create a new branch from the default branch
53+
gh api /repos/$org/$repo/git/refs -f ref="refs/heads/$newBranch" -f sha=$latestCommitSha
54+
}
55+
56+
57+
58+
# foreach ($folder in $filteredAlerts.repository.name | Sort-Object | get-unique) {
59+
# CreateBranchFromDefault -org $org -repo $folder -newBranch "codeql-autofixes"
60+
# }
61+
62+
# after fix is created, commit the fix to the branch
63+
#Start-Sleep 30
64+
$alerts = @()
65+
66+
# foreach ($alert in $filteredAlerts) {
67+
# $alertNumber = $alert.number
68+
# $repo = $alert.repository.name
69+
# $alerts += $alert.html_url
70+
# Write-Host "Attempting to create autofix for alert $alertNumber in repo $repo"
71+
# gh api `
72+
# --method POST `
73+
# /repos/$org/$repo/code-scanning/alerts/$alertNumber/autofix/commits `
74+
# -f "target_ref=refs/heads/codeql-autofixes" -f "message=AI-generated autofix for alert $alertNumber"
75+
# }
76+
77+
# finally - create a pull request with all the generated autofixes
78+
79+
80+
function New-PR {
81+
82+
param (
83+
[Parameter(Mandatory = $true)]
84+
[string]$defaultBranch
85+
)
86+
87+
gh api `
88+
--method POST `
89+
-H "Accept: application/vnd.github.v3+json" `
90+
"/repos/$org/$folder/pulls" `
91+
-f title="AI-generated CodeQL autofixes" `
92+
-f body="This PR batches together all C# AI-generated autofixable CodeQL alerts found in the repository. Consider testing these changes rather than blindly trusting the AI. This PR attempts to fix the following issues: $alerts" `
93+
-f head='codeql-autofixes' `
94+
-f base=$defaultBranch
95+
#-f draft='false'
96+
}
97+
98+
# foreach ($repoGroup in $alertsByRepo) {
99+
# $mostRecentAlert = $repoGroup.Group | Sort-Object -Property { $_.most_recent_instance.ref } -Descending | Select-Object -First 1
100+
101+
# # Create a new PR for the repository
102+
# New-PR -defaultBranch ($mostRecentAlert.most_recent_instance.ref).Split("/")[-1]
103+
# }

0 commit comments

Comments
 (0)