How to efficiently locate Bitbucket pull requests associated with a Jira ticket?

I’m trying to find a faster way to get all pull requests linked to a specific Jira issue. Right now I’m using the Bitbucket API to get all merged and open PRs, then matching them to the issue number. But it’s slow, especially since I have to fetch PRs in batches of 100 and we use multiple repos.

I know there’s an API call to get commits for a Jira issue (/rest/jira/1.0/issues/{issue}/commits), but there’s no similar endpoint for pull requests.

Jira can show this info in its interface, so there must be a quicker way to get it. Any ideas on how Jira does this or how I can speed up my process?

# Current slow method
def get_pull_requests(issue_number):
    all_prs = []
    for repo in repositories:
        page = 1
        while True:
            prs = bitbucket_api.get_pull_requests(repo, page=page, limit=100)
            matching_prs = [pr for pr in prs if issue_number in pr.title]
            all_prs.extend(matching_prs)
            if len(prs) < 100:
                break
            page += 1
    return all_prs

Is there a more efficient approach?

I’ve dealt with this exact problem before, and I found a workaround that might help you out. Instead of fetching all PRs and then filtering, you can leverage Bitbucket’s search functionality in the API. Try using the ‘q’ parameter in your API call to filter PRs by the Jira issue number right from the start.

Here’s a rough idea of how you could modify your code:

def get_pull_requests(issue_number):
    all_prs = []
    for repo in repositories:
        prs = bitbucket_api.get_pull_requests(
            repo,
            q=f'title~"{issue_number}"',
            state='OPEN,MERGED'
        )
        all_prs.extend(prs)
    return all_prs

This approach should significantly reduce the number of API calls and the amount of data you’re processing. It’s not perfect, as it relies on consistent PR naming conventions, but it’s been a game-changer for me in terms of efficiency. You might need to tweak the search query based on your team’s PR naming practices.

yo, have u tried using webhooks? They can notify u when PRs are created or updated. Set em up to send PR info to a database when theyre linked to a Jira issue. Then u just query the DB for the issue number. WAY faster than constantly polling the API

Having worked extensively with Bitbucket and Jira integration, I can suggest an alternative approach that might prove more efficient. Consider utilizing Bitbucket’s Smart Commits feature. By including the Jira issue key in commit messages, you create a direct link between commits and Jira issues. You can then use the Bitbucket API to fetch commits related to a specific branch or PR, filtering for those containing the Jira issue key.

This method reduces API calls and processing time significantly. It requires team-wide adoption of Smart Commits, but the benefits extend beyond just this use case. It improves overall traceability and can automate Jira updates directly from commits.

Remember to implement proper error handling and rate limiting in your API requests to ensure robustness and compliance with API usage policies.