Skip to content

Instantly share code, notes, and snippets.

@esoergel
Last active October 29, 2024 14:20
Show Gist options
  • Save esoergel/45a8a647251ef36f25f127ff7c656aa0 to your computer and use it in GitHub Desktop.
Save esoergel/45a8a647251ef36f25f127ff7c656aa0 to your computer and use it in GitHub Desktop.
# https://docs.github.com/en/enterprise-cloud@latest/rest/code-scanning/code-scanning?apiVersion=2022-11-28#list-code-scanning-alerts-for-a-repository
import os, requests, csv
TOKEN = os.environ.get('TOKEN')
REPO = 'commcare-hq'
# REPO = 'commcare-cloud'
# REPO = 'Vellum'
def get_page(page):
print(f'fetching page {page}')
# To see only open alerts, add &state=open
res = requests.get(f'https://api.github.com/repos/dimagi/{REPO}/code-scanning/alerts?page={page}',
headers={'Authorization': f"Bearer {TOKEN}"})
for alert in res.json():
location = alert['most_recent_instance']['location']
yield {
'number': alert['number'],
'html_url': alert['html_url'],
'severity': alert['rule'].get('security_severity_level', alert['rule']['severity']),
'state': alert['state'],
'description': alert['rule']['description'],
'path': f"{location['path']}:{location['start_line']}",
}
with open('alerts.csv', 'w') as f:
columns = ['number', 'html_url', 'severity', 'state', 'description', 'path']
writer = csv.DictWriter(f, columns)
writer.writeheader()
page = 0
while True:
page += 1
alerts = list(get_page(page))
print(f"pulled {len(alerts)} alerts")
if not alerts:
break
for alert in alerts:
writer.writerow(alert)