Created
March 20, 2024 17:12
-
-
Save aurbano/87c4f4031a1f65c97e7c0e3aa7d255db to your computer and use it in GitHub Desktop.
Measure average lines of code and time to review of PRs
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
import requests | |
from datetime import datetime | |
import csv | |
GITHUB_TOKEN = "" | |
ORG = "" | |
REPO = "" | |
MAX_PAGES = 1 | |
final_dataset = [] | |
fmt = "%Y-%m-%dT%H:%M:%SZ" | |
page = 1 | |
url = (f"https://api.github.com/repos/{ORG}/{REPO}/pulls?state=closed&per_page=100&page=%d" % page) | |
res = requests.get(url, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"}) | |
repos = res.json() | |
print("Reading PRs") | |
while res.json() != []: | |
print(f"Page {page}") | |
page = page + 1 | |
url = (f"https://api.github.com/repos/{ORG}/{REPO}/pulls?state=closed&per_page=100&page=%d" % page) | |
res = requests.get(url, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"}) | |
repos.extend(res.json()) | |
if page > MAX_PAGES: | |
break | |
print("Fetching PR content, Please Wait...") | |
for data in repos: | |
result = {} | |
created_at = datetime.strptime(data['created_at'], fmt) | |
closed_at = datetime.strptime(data['closed_at'], fmt) | |
result['hours_to_review'] = round((closed_at - created_at).total_seconds() / 3600, 2) | |
url_for_pr = (f"https://api.github.com/repos/{ORG}/{REPO}/pulls/%d" % data['number']) | |
res = requests.get(url_for_pr, headers={"Authorization": f"Bearer {GITHUB_TOKEN}"}) | |
res_json = res.json() | |
result['line_of_code'] = res_json['additions'] if "additions" in res_json else -1 | |
result['user_name'] = res_json['user']['login'] | |
result['pr_number'] = data['number'] | |
final_dataset.append(result) | |
print("Writing to csv, Please Wait") | |
keys = final_dataset[0].keys() | |
with open('pr_review_data.csv', 'w', newline='') as output_file: | |
dict_writer = csv.DictWriter(output_file, keys) | |
dict_writer.writeheader() | |
dict_writer.writerows(final_dataset) | |
print("*************DONE*****************") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment