I'm trying fetch below details using bitbucket API's.
- list of all repo’s in a project
- list all type of pull requests(OPEN,MERGED) with status, if its merged who has merged
tried- https://api.bitbucket.org/2.0/repositories/turvobitbucket/accounts/pullrequests
Above API call only fetches OPEN PR's
- No. of lines of code added on the branch(ex:master)
tried:
git log --since="3 month ago" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2 } END { printf "Lines added: %s\nLines deleted: %s\n", add, subs }'
in the same way we need BBC API's
Above queries required on monthly basis.
Thank you Patrik S for quick response.
please respond if possible
1. from below api I'm unable to fetch all the repo's from project. fetching only few repo's.
https://api.bitbucket.org/2.0/repositories/<workspace>?q=project.name="my_project"
2. Is there any API to get the information about merged PR who all are approved.
Hello @sreekanth.c ,
Welcome to Atlassian Community!
Let me share the relevant Bitbucket Cloud API endpoints along with some examples for the use cases you have described :
We have an endpoint to list all the repositories within a workspace, which can be queried for the project name of the key. Following an example listing all the repositories that are within a project named "my_project"
https://api.bitbucket.org/2.0/repositories/<workspace>?q=project.name="my_project"
You also have the option to query by the project key :
https://api.bitbucket.org/2.0/repositories/<workspace>?q=project.key="ABC"
By default, the List Pull requests endpoint will indeed just return the Pull requests that are in OPEN state.
You can add the state=ALL query in the API URL to return all the Pull Requests in a repository, like in the example below :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pullrequests?state=ALL
For Pull Requests in the Merged state, you can check the closed_by field in the response of the API for details of the user who merged the Pull request.
I'm afraid that currently Bitbucket Cloud does not track this information and there's no API endpoint currently available to fetch this data. In this case, the git command you shared would need to be executed in a local clone of the repository.
For more details about the aforementioned API endpoints, and all the other available endpoints you can refer to Bitbucket Cloud API reference documentation.
Thank you, @sreekanth.c !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Patrik S for quick response.
please respond if possible
1. from below api I'm unable to fetch all the repo's from project. fetching only few repo's.
https://api.bitbucket.org/2.0/repositories/<workspace>?q=project.name="my_project"
2. Is there any API to get the information about merged PR who all are approved.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @sreekanth.c ,
You're very welcome!
In regards to your follow-up questions :
1. The Bitbucket API responses are paginated (learn more in BB API Pagination ) and the page length can include from 10 to 100 results. The default pagination depends on the endpoint, and specifically for the list all the repositories within a workspace API, the default pagination is 10, which means it will return a maximum of 10 repositories per page. With that in mind, the following approaches can be used to see all the results :
{
"size": 5421,
"page": 2,
"pagelen": 10,
"next": "https://api.bitbucket.org/2.0/repositories/pypy/pypy/commits?page=3", "previous": "https://api.bitbucket.org/2.0/repositories/pypy/pypy/commits?page=1",
"values": [ ... ]
}
Where the URL in the next field indicates the link you should call to get to the next page with the rest of the results. The lack of a next link in the response indicates the end of the results.
https://api.bitbucket.org/2.0/repositories/<workspace>?q=project.name="my_project"&pagelen=100
The example above will return 100 results per page. If you have up to 100 repos under "my_project", all the repos will be returned on a single page. If you have more than 100, then you will still need to iterate through the pages like in the example above.
2. You can list the Merged PRs using the query below :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pullrequests?state=MERGED
And to get the users that approved those PRs, for each PR ID returned in that list, you will need to make a new API call and specify the PR ID in the URL as well. Here is an example of how to get the name of all participants in the PR and which ones approved it :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repo>/pullrequests/<id>\?fields\=participants.user.display_name,participants.approved
More details about that endpoint are outlined in Get a Pull Request .
Hope that helps! Thank you, @sreekanth.c !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the reply @Patrik S
same issue for pull requests, not fetching all the results from all the pages just printing 10fields. i tried to add &pagelen=100 but not working. anything im missing?
if possible, is there any way to filter the pull request based on updated_on field. i have tried below but not working to the pull request API
jq '.' | jq 'select(.updated_on > "2023-01-09T08:21:50.037798+00:00")'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @sreekanth.c ,
The max size of the page varies depending on the API endpoint. Specifically for the List Pull Requests API, the max pagelen that can be used is 50. If you try using a pagelen bigger than that value, the API will return the error "Invalid pagelen".
As for filtering the Pull Request by the updated_on date, this is possible directly in the API query as the following example :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repositories>/pullrequests?q=updated_on+%3E+2023-01-09T08%3A21%3A50%2B00%3A00+AND+state="MERGED"&pagelen=50
This example also includes the &pagelen=50 at the end of the URL. That API query would translate to :
updated_on > 2023-01-09T08:21:50+00:00 AND state="MERGED"
Please note that URL queries must be encoded. You can find a list of characters and their respective encoding in HTML URL Encoding reference.
You're also welcome to check some additional examples of querying in Bitbucket Cloud API - Querying .
Thank you, @sreekanth.c !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Patrik S sorry if i miss something, as you suggested need to include updated_on date in the pull request API, if i add date in the api it will come PR's only based on dates(as per my understanding).but i need all pull request's using API
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @sreekanth.c ,
The example I shared in my previous response :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repositories>/pullrequests?q=updated_on+%3E+2023-01-09T08%3A21%3A50%2B00%3A00+AND+state="MERGED"&pagelen=50
would be equivalent to the following condition :
updated_on > 2023-01-09T08:21:50+00:00 AND state="MERGED"
Please note that the updated_on is using the greater than (>) symbol. This means that it would return all the Pull Requests that were last updated after 2023-01-09T08:21:50+00:00 AND that are currently in the MERGED state.
If you would like to get the pull requests both in MERGED or OPEN state where the updated_on date is greater than 2023-01-09T08:21:50+00:00, then the query would be like below :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repositories>/pullrequests?q=updated_on+%3E+2023-01-09T08%3A21%3A50%2B00%3A00+AND+%28state="MERGED"+OR+state="OPEN"%29+&pagelen=50
that is equivalent to the condition :
updated_on > 2023-01-09T08:21:50+00:00 AND (state="MERGED" OR state="OPEN")
You can adapt that condition according to your use case based on the available Bitbucket Cloud - Querying examples.
Thank you, @sreekanth.c
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
we need to fetch all the PR's based on type(ALL,OPEN,MERGED,DECLINED) on given number days, but its fetching only 50 records(except ALL which is not working), i tried to increase pagelen but getting an error. any solution?
and can we fetch the pull requests between two dates?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @sreekanth.c ,
The limit of 50 records is because, for the Pull Requests endpoint, the max pagelen allowed is 50. If the API query you are using returns more than 50 records, you will have to iterate through the pages, as I mentioned in my previous comment.
In the JSON response of the API, there will be a field called next with the link to the next page that you should call to get to the next page with the rest of the results. The lack of a next link in the response indicates the end of the results - meaning you reached the last page.
For example, to get the second page of records, an URL similar to the one below will be returned in the next field of the JSON :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pullrequests?q=updated_on+%3E+2023-01-09T08%3A21%3A50%2B00%3A00+AND+%28state%3D%22MERGED%22+OR+state%3D%22OPEN%22%29+&pagelen=50&page=2
Note that it has a page=2 at the end of the URL indicating we want the second page of results.
As for getting the Pull Requests between two dates, this is also possible a query similar to the below :
https://api.bitbucket.org/2.0/repositories/<workspace>/<repository>/pullrequests?q=updated_on+%3E+2023-01-09T08%3A21%3A50%2B00%3A00+AND+updated_on+%3C+2023-03-15T08%3A21%3A50%2B00%3A00+AND+%28state="MERGED"+OR+state="OPEN"+OR+state="DECLINED"%29+&pagelen=50
That would be equivalent to the following expression :
updated_on > 2023-01-09T08:21:50+00:00 AND updated_on < 2023-03-15T08:21:50+00:00 AND (state="MERGED" OR state="OPEN" OR state="DECLINED")
This would return all the Pull Requests that were last updated between 2023-01-09T08:21:50+00:00 and 2023-03-15T08:21:50+00:00 where the state is MERGED or OPEN or DECLINED. You won't need to use the ALL state because we are already covering all the 3 valid states in the query.
Thank you, @sreekanth.c !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.