Created
March 14, 2023 13:03
-
-
Save dserodio/0fb346106614b5476f271e24cd8dcc0d to your computer and use it in GitHub Desktop.
Get the number of approvers in a given Github repository's PRs
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 | |
# Set the repo owner and name | |
owner = "YOUR_REPO_OWNER" | |
repo = "YOUR_REPO_NAME" | |
GITHUB_TOKEN = "YOUR_GITHUB_TOKEN" | |
# Set the GitHub API URL | |
url = f"https://api.github.com/repos/{owner}/{repo}/pulls" | |
# Set the number of PRs to retrieve | |
num_prs = 50 | |
# Create a session with the Github API token | |
github = requests.Session() | |
github.headers.update( | |
{ | |
"Accept": "application/vnd.github+json", | |
"Authorization": f"Bearer {GITHUB_TOKEN}", | |
"X-GitHub-Api-Version": "2022-11-28", | |
} | |
) | |
# Make the API request to retrieve the last 10 PRs | |
response = github.get( | |
url, | |
params={ | |
"state": "closed", | |
"sort": "created", | |
"direction": "desc", | |
"per_page": num_prs, | |
}, | |
) | |
# Iterate through each PR and get the number of approvers | |
for pr in response.json(): | |
pr_number = pr["number"] | |
pr_url = pr["url"] | |
pr_title = pr["title"] | |
pr_approvals_url = f"{pr_url}/reviews" | |
pr_approvals_response = github.get(pr_approvals_url) | |
num_approvals = len( | |
[r for r in pr_approvals_response.json() if r["state"] == "APPROVED"] | |
) | |
is_merged = False | |
if github.get(f"{pr['url']}/merge").status_code == 204: | |
is_merged = True | |
if not is_merged: | |
continue | |
print(f"PR #{pr_number} '{pr_title}' has {num_approvals} approvers: ({is_merged})") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment