Counting number of pull requests created, merged or rejected

Viewed 134

hope you all are doing good. I just wanted to know if there's a way to find out how many pull requests were created by a user. Also how many pull requests of the same user were approved or rejected. Just want to use this information for some metrics.

1 Answers

I did some research and found this:

First install requests via pip install requests. Then you need to generate an access token (Settings - Developer settings - personal access tokens - generate new token) and add user: read user permission to the token.

You then need to export your token as an environment variable. There are many blog posts on how to do this — for it to work with the script provided below, you should name it GITSTATS_TOKEN .

To get the number of PRs you’ve submitted and reviewed, you can then run the following script (add your own username and repositories you’re interested in — in this example, the query says how many PRs I’ve submitted and reviewed in pandas-dev/pandas):

import requests
import os

data = requests.get('https://api.github.com/search/issues?q=is:pr+repo:pandas-dev/pandas+author:AUTHOR+is:merged').json()

print(f"Merged PRs: {data['total_count']}")

url = 'https://api.github.com/graphql'
query = (
"""
{
  user(login: "AUTHOR") {
    contributionsCollection {
      pullRequestReviewContributionsByRepository {
        contributions {
          totalCount
        }
        repository {
          name
        }
      }
    }
  }
}
"""
)
json = { 'query' : query}
api_token = os.environ['GITSTATS_TOKEN']
headers = {'Authorization': 'token %s' % api_token}

r = requests.post(url=url, json=json, headers=headers)

json_data = r.json()['data']['user']['contributionsCollection']['pullRequestReviewContributionsByRepository']

reviews = [i['contributions']['totalCount'] for i in json_data if i['repository']['name'] == 'pandas'][0]

print(f'Number of reviews: {reviews}')

Source and demo repo

Related