Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

From API how to get pull requests closed between two dates?

marius.guscius June 1, 2023

Using the official SDK https://pypi.org/project/atlassian-python-api/ how can someone get a list of pull requests that were closed between two dates?

Right now I can get only by creation date:

from atlassian.bitbucket.cloud import Cloud
from atlassian.bitbucket.cloud.repositories import Repository
from atlassian.bitbucket.cloud.repositories.pullRequests import PullRequest
from atlassian.bitbucket.cloud.workspaces.projects import Project

bitbucket: Cloud = Cloud(username = "zzz", password = "bbb")
project: Project = bitbucket.workspaces.get("xxx").projects.get("yyy")

repo: Repository
for repo in project.repositories.each():
pull_request: PullRequest
    for pull_request in repo.pullrequests.each(
        q = " AND ".join([
            "state = \"MERGED\"",
            "destination.branch.name = \"master\"",
            "created_on >= \"ggg\"",
            "created_on <= \"ggg\""
        ])
):
        print(pull_request)

2 answers

0 votes
marius.guscius June 8, 2023

Here's curl dump of the request...

curl -u "$BB_USER:$BB_PASSWORD" -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' 'https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests/?q=state+%3D+%22MERGED%22+AND+destination.branch.name+%3D+%22master%22+AND+closed_on+%3E%3D+%222023-04-01T00%3A00%3A00%2B00%3A00%22+AND+closed_on+%3C%3D+%
222023-05-31T23%3A59%3A59.999999%2B00%3A00%22'

 

Also, I got this:

{"type": "error", "error": {"message": "Something went wrong", "id": "1285ea2220784e27a67f022cf4a533fb"}}

 

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 6, 2023

Hi Marius,

The Pull Request API endpoint returns the field closed_on which indicates the date the PR was merged.

This field is not returned by default and it can be discovered when using a URL as the following:

https://api.bitbucket.org/2.0/repositories/workspace-id/my-repo/pullrequests/23?fields=*

If you only want to get this field:

https://api.bitbucket.org/2.0/repositories/workspace-id/my-repo/pullrequests/23?fields=closed_on

I'm not familiar with the library you are using so I don't know whether this field is returned by default or how to add the fields parameter to return more fields. You could reach out to the project's maintainers or create a question in the project's issue tracker.

Kind regards,
Theodora

marius.guscius June 8, 2023

But this endpoint is for a specific pull request, right? What if I need to get multiple pull requests based on this field? I do not know their IDs, I just need to get a list of PRs that were closed between some dates...

 

I went and reversed engineered a bit the package that I am using and to query the pull requests it is just calling this endpoint:

 

https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests?q=state+%3D+%22MERGED%22+AND+destination.branch.name+%3D+%22master%22+AND+created_on+%3E%3D+%22xxx%22+AND+created_on+%3C%3D+%22yyy%22

 But if I change from `created_on` to `closed_on` it doesn't work:

https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests?q=state+%3D+%22MERGED%22+AND+destination.branch.name+%3D+%22master%22+AND+closed_on+%3E%3D+%22xxx%22+AND+closed_on+%3C%3D+%22yyy%22
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 9, 2023

Hi Marius,

This endpoint is for a specific pull request indeed. If you need to get the closed_on field for all pull requests, you can use fields=*.* with this endpoint:

https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/pullrequests?q=state+%3D+%22MERGED%22&fields=*.*

I see though that filtering is not supported for the closed_on field, which is the reason for the error you posted in your other reply.

In this case, you could use fields=*.* with the rest of your filters in order to get the field closed_on in the output, and then use another tool that filters json data (like jq) in order to filter the results based on the value of this field.

Kind regards,
Theodora

marius.guscius June 9, 2023

but that will pull ALL pull requests and I will have to do the filtering on client side. What if we have thousands of pull requests - it will take ages to do it. Not only that, every time when new request is created and merged it will become even slower. This filtering should be done on server-side I think...

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 12, 2023

Hi Marius,

I understand that this is not ideal. I went ahead and created a request in our issue tracker to support this:

Please make sure to vote for it to express your interest.

You can still use the rest of the filters you have in your examples, but filtering by closed_on date on the client side is the only workaround at the moment.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events