Last active
March 19, 2020 14:56
-
-
Save Morxander/51ee71737e3f01bef4b0e95175c6b6c6 to your computer and use it in GitHub Desktop.
PRs and Commits stats per repo for specific users
This file contains 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 | |
REPO = "org/repo_name" # Replace it with your repo | |
TOKEN = "XXXXX" # Replace it with your Github token | |
START_DATE = "2019-07-01" # Start date YYYY-MM-DD | |
TEAM = ["user1", "user2"] # A list of users | |
BASE_URL = "https://api.github.com" | |
pr_urls = [] | |
pr_counts = 0 | |
commits_count = 0 | |
additions_count = 0 | |
deletions_count = 0 | |
print(f"{REPO} - {START_DATE}") | |
for user in TEAM: | |
url = BASE_URL + f"/search/issues?q=repo:{REPO}+type:pr+author:{user}+created:>{START_DATE}&page=1&per_page=100" | |
response = requests.get(url, headers={"Authorization": f"token {TOKEN}", "Accept": "application/json"}) | |
response_json = response.json() | |
user_pr_count = response_json['total_count'] | |
pr_counts += user_pr_count | |
print(f"{user} = {user_pr_count} PRs") | |
for pr in response_json['items']: | |
pr_urls.append(pr['pull_request']['url']) | |
# A dirty workaround but in my defense I didn't have enough time | |
if response_json['incomplete_results'] == False: | |
url_2 = BASE_URL + f"/search/issues?q=repo:{REPO}+type:pr+author:{user}+created:>{START_DATE}&page=2&per_page=100" | |
response2 = requests.get(url_2, headers={"Authorization": f"token {TOKEN}", "Accept": "application/json"}) | |
response_json2 = response2.json() | |
for pr in response_json2['items']: | |
pr_urls.append(pr['pull_request']['url']) | |
for pr_url in pr_urls: | |
response = requests.get(pr_url, headers={"Authorization": f"token {TOKEN}", "Accept": "application/json"}) | |
response_json = response.json() | |
commits_count += response_json['commits'] | |
additions_count += response_json['additions'] | |
deletions_count += response_json['deletions'] | |
print(f"Total PRs: {pr_counts}") | |
print(f"Total Commits: {commits_count}") | |
print(f"Total additions: {additions_count}") | |
print(f"Total deletions: {deletions_count}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment