Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

can you fix this function , our goal is to fetch all open and draft PR and ignore closed…

  1. 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
    
  2. 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_user function 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_prs
    

    Explanation of Changes:

    1. Single API Call: We modified the pr_search_url to include both open and draft pull requests by combining the two conditions with an OR operator.

    2. Filter Logic: Since the fetch_all_prs function retrieves both open and draft PRs, we only need to verify that the state is "open" or that draft is True when filtering.

    3. 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 authentication headers before executing the function for it to work correctly.