can you fix this function , our goal is to fetch all open and draft PR and ignore closed…
- User
can you fix this function , our goal is to fetch all open and draft PR and ignore closed PRs
also if it's possible to ignore 2 time API call (open and draft ) to GitHub, make single call
make this super efficient without loosing functionality and our goal
# Function to fetch PRs for a specific user (open and draft PRs) def fetch_prs_for_user(username): open_prs_url = f"https://api.github.com/search/issues?q=repo:{REPO_OWNER}/{REPO_NAME}+is:pr+author:{username}+is:open" draft_prs_url = f"https://api.github.com/search/issues?q=repo:{REPO_OWNER}/{REPO_NAME}+is:pr+author:{username}+is:draft" def fetch_all_prs(url): prs = [] page = 1 while True: response = requests.get(url, headers=headers, params={"page": page, "per_page": 100}) if response.status_code == 200: data = response.json() prs.extend(data.get('items', [])) if 'next' not in response.links: # No more pages break page += 1 else: print(f"Error fetching PRs: {response.status_code} - {response.text}") break return prs # Fetch open PRs and draft PRs with pagination handling open_prs = fetch_all_prs(open_prs_url) draft_prs = fetch_all_prs(draft_prs_url) # Merge open and draft PRs while ensuring no duplicates (only open or draft PRs) merged_prs = {pr['number']: pr for pr in open_prs + draft_prs}.values() # Using PR number as the key to avoid duplication # Filter out closed PRs and include only open or draft PRs filtered_prs = [pr for pr in merged_prs if pr['state'] == 'open' or pr['draft']] return filtered_prs - Assistant
To optimize the function and reduce the number of API calls, we can leverage GitHub's search API to retrieve both open and draft pull requests in a single request. Specifically, we can use a query that checks for both open and draft statuses in one call.
Below is the revised version of the
fetch_prs_for_userfunction that accomplishes this:import requests # Function to fetch PRs for a specific user (open and draft PRs) def fetch_prs_for_user(username): # Combined query to fetch open and draft PRs pr_search_url = f"https://api.github.com/search/issues?q=repo:{REPO_OWNER}/{REPO_NAME}+is:pr+author:{username}+is:open+OR+is:draft" def fetch_all_prs(url): prs = [] page = 1 while True: response = requests.get(url, headers=headers, params={"page": page, "per_page": 100}) if response.status_code == 200: data = response.json() prs.extend(data.get('items', [])) if 'next' not in response.links: # No more pages break page += 1 else: print(f"Error fetching PRs: {response.status_code} - {response.text}") break return prs # Fetch both open and draft PRs with pagination handling all_prs = fetch_all_prs(pr_search_url) # Filter the PRs to ensure they are either open or drafts (the query should already ensure this) filtered_prs = [pr for pr in all_prs if pr['state'] == 'open' or pr.get('draft', False)] return filtered_prsExplanation of Changes:
-
Single API Call: We modified the
pr_search_urlto include both open and draft pull requests by combining the two conditions with anORoperator. -
Filter Logic: Since the
fetch_all_prsfunction retrieves both open and draft PRs, we only need to verify that the state is "open" or thatdraftisTruewhen filtering. -
Efficiency: By combining the queries, we reduce the execution time and the number of API calls made to GitHub, thus making the function more efficient without losing functionality.
Make sure to set the
REPO_OWNER,REPO_NAME, and proper authenticationheadersbefore executing the function for it to work correctly. -