From API how to get pull requests closed between two dates?
Asked by marius.guscius:

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)
```


Answers:
author: marius.guscius
body: 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"}}
```


author: Theodora Boudale
body: Hi Marius,

The [Pull Request API endpoint](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-get) 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

comments: author: marius.guscius
body: 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
```


author: Theodora Boudale
body: 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


author: marius.guscius
body: 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...


author: Theodora Boudale
body: Hi Marius,

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

* <https://jira.atlassian.com/browse/BCLOUD-22709>

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


Tags: bitbucket-cloud

close

cloud

pull-request

python

sdk