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 Cloudfrom atlassian.bitbucket.cloud.repositories import Repositoryfrom atlassian.bitbucket.cloud.repositories.pullRequests import PullRequestfrom atlassian.bitbucket.cloud.workspaces.projects import Projectbitbucket: Cloud = Cloud(username = "zzz", password = "bbb")project: Project = bitbucket.workspaces.get("xxx").projects.get("yyy")repo: Repositoryfor 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)
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
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:
<span>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</span>
But if I change from `created_on` to `closed_on` it doesn't work:
<span>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</span>
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+=+"MERGED"+AND+destination.branch.name+=+"master"+AND+closed_on+>=+"2023-04-01T00:00:00+00:00"+AND+closed_on+<=+%222023-05-31T23%3A59%3A59.999999%2B00%3A00%22'
Also, I got this:
{"type": "error", "error": {"message": "Something went wrong", "id": "1285ea2220784e27a67f022cf4a533fb"}}
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+=+"MERGED"&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.
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...
It looks like you're new here. Sign in or register to get started.