get the statistics of pull requests of a user - python

Ram Sharma
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 30, 2025

is there a way to get the Pull requests statistics ( Raised, Reviewed, Committed, Approved .. ) for a  user for each of the repositories

I would be more interested if this query can be automated using python. 


Thanks in advance 

1 answer

1 accepted

1 vote
Answer accepted
Renata_Getint
Atlassian Partner
January 30, 2025

You can automate the retrieval of pull request statistics for a specific user across multiple repositories using the Bitbucket Cloud API (or Bitbucket Server/Data Center API, depending on your setup).

Using Python to Get PR Statistics

  1. Set Up Authentication
    If you’re using Bitbucket Cloud, you can authenticate using OAuth, App passwords, or personal access tokens. For Bitbucket Server/Data Center, use basic authentication or personal access tokens.

  2. Query the API for Pull Requests
    The Bitbucket Cloud API provides endpoints to retrieve pull requests, including their statuses (OPEN, MERGED, DECLINED) and approvals.

  3. Filter by User and Repository
    You can loop through all repositories and filter by the user ID to get the statistics you need.

Example Python Script

import requests

# Replace with your Bitbucket workspace and username
BITBUCKET_WORKSPACE = "your_workspace"
USERNAME = "target_username"
AUTH = ("your_username", "your_app_password") # Use OAuth or token-based auth if needed

def get_repos():
url = f"https://api.bitbucket.org/2.0/repositories/{BITBUCKET_WORKSPACE}"
response = requests.get(url, auth=AUTH)
return response.json().get("values", [])

def get_pull_requests(repo_slug):
url = f"https://api.bitbucket.org/2.0/repositories/{BITBUCKET_WORKSPACE}/{repo_slug}/pullrequests"
params = {"state": "MERGED"} # Change state to OPEN, DECLINED, etc., if needed
response = requests.get(url, params=params, auth=AUTH)
return response.json().get("values", [])

def get_user_pr_stats():
repos = get_repos()
stats = {"raised": 0, "reviewed": 0, "approved": 0}

for repo in repos:
repo_slug = repo["slug"]
prs = get_pull_requests(repo_slug)

for pr in prs:
if pr["author"]["username"] == USERNAME:
stats["raised"] += 1

# Check approvals
for participant in pr.get("participants", []):
if participant["user"]["username"] == USERNAME and participant["approved"]:
stats["approved"] += 1

# Count reviews (assuming comments or participation means review)
if any(p["user"]["username"] == USERNAME for p in pr.get("participants", [])):
stats["reviewed"] += 1

return stats

if __name__ == "__main__":
user_stats = get_user_pr_stats()
print(f"User PR Stats: {user_stats}")

What This Script Does

  • Fetches all repositories in your workspace.
  • Retrieves pull requests for each repository.
  • Filters PRs by the target user to count raised, reviewed, and approved PRs.

Next Steps

  • Modify the script to include other states (e.g., DECLINED).
  • Use pagination if you have a large number of PRs (page parameter in API).
  • Store the data in a database or generate reports for deeper insights.

If you're looking for automated PR tracking across multiple systems, Getint offers integrations between Jira and Git Repos, ensuring that development workflows remain synchronized. Let us know if we can help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events