From 3946794e8dc16f44786e89f6d4883e117d3091a9 Mon Sep 17 00:00:00 2001 From: Benedikt Volkel Date: Wed, 13 Mar 2024 13:33:04 +0100 Subject: [PATCH] Get all labels and change table organisation Logic is now the following: 1. Fetch all labels (optionally given some regex). 2. Get closed PRs that have at least one of those labels. 3. Split into merged and not merged. 4. Populate a table per label and list merged PRs. (A PR can therefore appear in more than one table.) 5. Write everything to a txt file. --- UTILS/o2dpg_make_github_pr_report.py | 118 ++++++++++++++++++++------- 1 file changed, 90 insertions(+), 28 deletions(-) diff --git a/UTILS/o2dpg_make_github_pr_report.py b/UTILS/o2dpg_make_github_pr_report.py index b6a4ac9c9..f2bd24bda 100755 --- a/UTILS/o2dpg_make_github_pr_report.py +++ b/UTILS/o2dpg_make_github_pr_report.py @@ -6,6 +6,7 @@ import sys import argparse import requests +import re def organise_prs(prs): @@ -29,23 +30,21 @@ def organise_prs(prs): merged_at.append(pr['merged_at']) # sort the merged PRs by their merged timestamp - prs_merged = [pr for _, pr in sorted(zip(merged_at, prs))] + prs_merged = [pr for _, pr in sorted(zip(merged_at, prs_merged))] return prs_merged, prs_other -def get_prs(owner, repo, prod_label, pr_state, include_unmerged, per_page=50, start_page=1, pages=1): +def get_prs(owner, repo, request_labels, pr_state, per_page=50, start_page=1, pages=1): """ Get PRs according to some selection """ # GitHub API endpoint for listing closed pull requests with a specific label - merged_token = '&is:merged=true' if not include_unmerged else '' prs_return = [] has_error = False for page in range(start_page, pages + 1): - url = f'https://api.github.com/repos/{owner}/{repo}/pulls?state={pr_state}{merged_token}&page={page}&per_page={per_page}' - print(f'Fetch PRs accrodring to {url}') + url = f'https://api.github.com/repos/{owner}/{repo}/pulls?state={pr_state}&page={page}&per_page={per_page}' # Send GET request to GitHub API response = requests.get(url) @@ -57,13 +56,13 @@ def get_prs(owner, repo, prod_label, pr_state, include_unmerged, per_page=50, st # PRs to return because we filter on a specific label for pr in prs: labels = pr['labels'] - accept = False + take_pr = False for label in labels: - if label['name'] == prod_label: + if label['name'] in request_labels: # only with the correct the label will be accepted - accept = True + take_pr = True break - if not accept: + if not take_pr: continue # we will end up here if accepted, so append prs_return.append(pr) @@ -80,37 +79,97 @@ def get_prs(owner, repo, prod_label, pr_state, include_unmerged, per_page=50, st return organise_prs(prs_return) -def make_report(prs_merged, prs_other, outfile): +def get_labels(owner, repo, regex=None): + """ + Get the labels that match given regex + """ + # the REST API url + url = f'https://api.github.com/repos/{owner}/{repo}/labels' + # Send GET request to GitHub API + response = requests.get(url) + + if response.status_code != 200: + print(f'ERROR: Problem to retrieve labels for owner {owner} and repository {repo}') + return None + + return [label['name'] for label in response.json() if not repo or re.match(regex, label['name'])] + + +def separate_labels_request_accept(labels, accept_suffix=None): + """ + Disentangle labels and