From b0c8fd7a0dae4ce0311bba9b5ebbb31f7d05dbb7 Mon Sep 17 00:00:00 2001 From: rht Date: Wed, 21 Feb 2024 23:25:19 -0500 Subject: [PATCH] Add script to list unabeled PR's since latest release --- maintenance/fetch_unlabeled_prs.py | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 maintenance/fetch_unlabeled_prs.py diff --git a/maintenance/fetch_unlabeled_prs.py b/maintenance/fetch_unlabeled_prs.py new file mode 100644 index 00000000000..6afcbc6bb7a --- /dev/null +++ b/maintenance/fetch_unlabeled_prs.py @@ -0,0 +1,70 @@ +import os +import requests +from datetime import datetime + +# Configuration +# Your GitHub Personal Access Token +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") +GITHUB_USERNAME = "projectmesa" +GITHUB_REPO = "mesa" +HEADERS = {"Authorization": f"token {GITHUB_TOKEN}"} + + +def get_latest_release_date() -> str: + """Fetches the latest release date from the GitHub repository.""" + url = ( + f"https://api.github.com/repos/{GITHUB_USERNAME}/{GITHUB_REPO}/releases/latest" + ) + response = requests.get(url, headers=HEADERS) + response.raise_for_status() # Raises an exception for HTTP error codes + return response.json()["published_at"] + + +def get_closed_pull_requests_since_latest_release( + latest_release_date, +) -> list[dict[str, any]]: + """Fetches pull requests created or updated after the latest release date, then filters by merged date.""" + pull_requests = [] + page = 1 + while True: + # Fetch PRs that were created or updated after the latest release date + url = f"https://api.github.com/repos/{GITHUB_USERNAME}/{GITHUB_REPO}/pulls?state=closed&base=main&sort=updated&direction=desc&page={page}" + response = requests.get(url, headers=HEADERS) + response.raise_for_status() + prs = response.json() + if not prs: + break + + # Convert latest release date to datetime for comparison + latest_release_datetime = datetime.strptime( + latest_release_date, "%Y-%m-%dT%H:%M:%SZ" + ) + + for pr in prs: + # Convert PR's `updated_at` to datetime for comparison + pr_updated_at = datetime.strptime(pr["updated_at"], "%Y-%m-%dT%H:%M:%SZ") + # Stop fetching if PR was updated before the latest release + if pr_updated_at < latest_release_datetime: + return pull_requests + + if pr["merged_at"]: + pr_merged_at = datetime.strptime(pr["merged_at"], "%Y-%m-%dT%H:%M:%SZ") + if pr_merged_at > latest_release_datetime and not pr["labels"]: + pull_requests.append(pr) + page += 1 + return pull_requests + + +def main() -> None: + # Based on https://github.com/projectmesa/mesa/pull/1917#issuecomment-1871352058 + latest_release_date = get_latest_release_date() + pull_requests = get_closed_pull_requests_since_latest_release(latest_release_date) + if len(pull_requests) <= 0: + return + print("These pull requests must be labeled:") + for pr in pull_requests: + print(f" PR #{pr['number']}: {pr['title']} - Merged at: {pr['merged_at']}") + + +if __name__ == "__main__": + main()