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

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.

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.

2 Likes

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.