# How to get all the commits of a pull request using bitbucket java api?

**URL:** <https://community.developer.atlassian.com/t/how-to-get-all-the-commits-of-a-pull-request-using-bitbucket-java-api/21183>\
**Category:** Bitbucket Development\
**Tags:** bitbucket\
**Created:** [July 17, 2018, 11:40pm UTC](https://community.developer.atlassian.com/t/how-to-get-all-the-commits-of-a-pull-request-using-bitbucket-java-api/21183 "2018-07-17T23:40:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivian.zhang](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vivian.zhang](https://community.developer.atlassian.com/u/vivian.zhang)\
**Post date:** [July 17, 2018, 11:40pm UTC](https://community.developer.atlassian.com/t/how-to-get-all-the-commits-of-a-pull-request-using-bitbucket-java-api/21183/1 "2018-07-17T23:40:02Z")

</div>

I’m creating a Bitbucket plugin which needs to check on all the commits of a pull request. I cannot find the corresponding Java api to get this information. The Bitbucket REST API give error message of

status: 401 Response: {“errors”:[{“context”:null,“message”:“You are not permitted to access this resource”,“exceptionName”:“com.atlassian.bitbucket.AuthorisationException”}]}

although I’ve given admin login to the http get method.

---

<div class="post-metadata">

**Author:** ![vivian.zhang](https://avatars.discourse-cdn.com/v4/letter/v/bc8723/32.png) [@vivian.zhang](https://community.developer.atlassian.com/u/vivian.zhang)\
**Post date:** [July 19, 2018, 7:04am UTC](https://community.developer.atlassian.com/t/how-to-get-all-the-commits-of-a-pull-request-using-bitbucket-java-api/21183/2 "2018-07-19T07:04:21Z")

</div>

I figure this out myself. Here are the code I used for getting pull request commits, hopefully it can be useful for others.

```
	CommitsBetweenRequest.Builder builder = new CommitsBetweenRequest.Builder(pullRequest);
	CommitsBetweenRequest cbr = builder.build();
	Page<Commit> commits = commitService.getCommitsBetween(cbr, new PageRequestImpl(0, SEARCH_PAGE_SIZE));
	SortedMap<Integer, Commit> commit_map = commits.getOrdinalIndexedValues();

```

Also the Bitbucket REST APIs are to be accessed from outside of Bitbucket. It doesn’t like to be used inside the Bitbucket. This could explain the error I received when try to use it inside the plugin.

---

<div class="post-metadata">

**Author:** ![RajeevSudra](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@RajeevSudra](https://community.developer.atlassian.com/u/RajeevSudra)\
**Post date:** [October 5, 2020, 10:00pm UTC](https://community.developer.atlassian.com/t/how-to-get-all-the-commits-of-a-pull-request-using-bitbucket-java-api/21183/3 "2020-10-05T22:00:29Z")

</div>

Does this code work for you if the merge strategy is rebase or squash? the rebased commit from a branch has a new commit hash when placed on the target branch so my use of similar code misses it.
