Require API's to list PR's, name of approved PR, count LOC of branch based on month
Asked by sreekanth.c:

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.


Answers:
author: sreekanth.c
body: Thank you [Patrik S](https://community.atlassian.com/t5/user/viewprofilepage/user-id/4603309) 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.


author: Patrik S
body: Hello [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) ,

Welcome to Atlassian Community!

Let me share the relevant Bitbucket Cloud API endpoints along with some examples for the use cases you have described :

#### List of all repo's in a project {#toc-hId-229393815}

We have an endpoint to [list all the repositories within a workspace](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-get), 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"
```

#### List all the Pull Request regardless of the current state (OPEN, MERGED, DECLINED, etc) {#toc-hId--1578060648}

By default, the [List Pull requests](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get) 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.

#### No. of lines of code added on the branch {#toc-hId-909452185}

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](https://developer.atlassian.com/cloud/bitbucket/rest/intro/) documentation.

Thank you, [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) !

Patrik S

comments: author: sreekanth.c
body: Thank you [Patrik S](https://community.atlassian.com/t5/user/viewprofilepage/user-id/4603309) 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.


author: Patrik S
body: Hello [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) ,

You're very welcome!

In regards to your follow-up questions :

**1.** The Bitbucket API responses are paginated (learn more in [BB API Pagination](https://developer.atlassian.com/cloud/bitbucket/rest/intro/#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](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-get) 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 :

* **Iterate through all the pages** : The response of the API will include the following fields :

  ```
  { 
    "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.
* **Increase the pagelen to return more results in a single API call** : you can define the length of the page (number of results on each page) by adding the following query in the URL :

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

Hope that helps! Thank you, [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) !

Patrik S


author: sreekanth.c
body: Thanks for the reply [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

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")'


author: Patrik S
body: Hello [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) ,

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](https://www.w3schools.com/tags/ref_urlencode.ASP).

You're also welcome to check some additional examples of querying in [Bitbucket Cloud API - Querying](https://developer.atlassian.com/cloud/bitbucket/rest/intro/#querying) .

Thank you, [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) !

Patrik S


author: sreekanth.c
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) 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


author: Patrik S
body: Hello [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) ,

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](https://developer.atlassian.com/cloud/bitbucket/rest/intro/#filtering) examples.

Thank you, [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845)

Patrik S


author: sreekanth.c
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

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?


author: sreekanth.c
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) any help to the above query


author: Patrik S
body: Hello [@sreekanth.c](/t5/user/viewprofilepage/user-id/5169845) ,

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](/t5/user/viewprofilepage/user-id/5169845) !

Patrik S


Tags: bitbucket-cloud

cloud