Last active
October 29, 2024 14:20
-
-
Save esoergel/45a8a647251ef36f25f127ff7c656aa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://docs.github.com/en/enterprise-cloud@latest/rest/code-scanning/code-scanning?apiVersion=2022-11-28#list-code-scanning-alerts-for-a-repository