gitgithub

Github: list PRs between two tags


Github provides a nice way to list commits between two tags, e.g. https://github.com/jupyter/nbconvert/compare/6.0.6...6.0.7

Is there any way to list merged PRs between two tags? I want this to include PRs that were rebase-merged.


Solution

  • This is possible using Github's API. This is the key endpoint: https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-pull-requests-associated-with-a-commit

    Here's pseudo-code for listing PRs between two tags:

    function listMergedPRsBetweenTags(repoOwner, repoName, tagA, tagB, token):
        # Construct the compare URL to get commits between two tags
        compareUrl = "https://api.github.com/repos/" + repoOwner + "/" + repoName + "/compare/" + tagA + "..." + tagB
    
        # Get the list of commits between tagA and tagB
        commitsResponse = HTTP_GET(compareUrl, headers={"Authorization": "Bearer " + token})
        commits = commitsResponse.commits  # Array of commit objects
    
        # Initialize a set to store unique merged PR numbers
        mergedPRs = {}
    
        # Iterate through each commit
        for commit in commits:
            commitSHA = commit.sha
    
            # Construct the URL to list pull requests associated with the commit
            prUrl = "https://api.github.com/repos/" + repoOwner + "/" + repoName + "/commits/" + commitSHA + "/pulls"
            
            # Query the GitHub API for associated pull requests
            prResponse = HTTP_GET(prUrl, headers={
                "Authorization": "Bearer " + token,
                "Accept": "application/vnd.github+json"
            })
            prList = prResponse  # Array of PR objects
            
            # For each PR, check if it's merged
            for pr in prList:
                if pr.merged_at is not null:
                    mergedPRs.add(pr.number)  # Add to the set for uniqueness
    
        # Return the unique list of merged PR numbers
        return mergedPRs