Rest API to get all the merged commits in a branch

Hello Friends,

I want to list all the JIRA#(commits)  those were merged in a branch. I can get list of all the commits using below rest api, but it gives me all the commits including merged commit details, is there any way to get only "merged" commits?

https://<URL>/rest/api/1.0/projects/<KEY>/repos/<REPO>/commits?until=release/2.0.5

 

Thank you

Amit Raj

3 answers

1 accepted

This widget could not be displayed.
Mark Askew Atlassian Team Aug 09, 2017

Hey Amit,

Have you looked at our REST documentation? REST API

You just need to add the merges parameter with a value of only like so:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits?&merges=only

Adding the parameter will return only merge commits.

I hope this helps.

Cheers,

Mark 

@Mark Askew Thanks for the pointers. I was hoping to do similar but I've noticed that when I add the merges=only query parameter, it doesn't seem to pick up PR merges that had the squash option enabled. 

I was looking at this approach because the PR RESTful endpoints don't seem to return the commit hash a merged PR creates, only the to and from branch refs for each PR.

The only way I can seem to determine a commit a PR makes is by scanning the commit history of a branch and regex'ing the commit messages looking for the the merge commit msg pattern.

Can you let me know if there's a better approach I can take? 

Cheers,

Mike

Mark Askew Atlassian Team Sep 12, 2017

Hey Mike,

I'm sorry that I originally missed your question.

When using the Squash merge strategy, what it does is rewrite history by combining the commits into a new non-merge commit, therefore, the merges=only query will not retrieve this commit since it is not a merge.

As I noted below, you may be able to use our REST endpoint to compare commits and filter based on the message as it will still contain a reference to "Merge pull request".

-Mark

Thanks Mark and no worries.

Hey mark,

 

I am using the Bitbucket 2.0 api and I need to do a similar thing of getting only the merged commits. I tried using the merges=only parameter but does not seem to work as it just returns all the commits on master(I only want the ones with label "M" and grayed out). 

I am currently using this endpoint -

https://api.bitbucket.org/2.0/repositories/<username>/<repo-name>/commits?merges=only.

 

Please let me know how to achieve this with the 2.0 API. I cannot find any documentation on that.

 

Thank you very much,

Harshil

This widget could not be displayed.

Hi Mark,

How do you specify the target branch here, if I have multiple branches in a repository? Does it work by adding "at=refs/heads/release/<branchname>" in the above query?

Thanks in advance,

Rajan

Mark Askew Atlassian Team Sep 01, 2017

Hey Rajan,

You may be able to use PATH to help filter the commits by. You would not be able to use AT as that is not allowed. Only the following from the API is allowed for this endpoint:

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMMITS?FOLLOWRENAMES&IGNOREMISSING&MERGES&PATH&SINCE&UNTIL&WITHCOUNTS

I looked around the API and tested but could not get exactly what you're looking for strictly with the API. You may have to additionally filter the JSON data to retrieve the first level ID and its message which may display as "Merge branch..." or "Merge Pull Request..."

# First Merge Commit with collapsed elements
{
"id": "10326411acaf5036bd4d525718cbd9e56305303b",
"displayId": "10326411aca",
"author": {
"name": "Mark Williams",
"emailAddress": ""
},
"authorTimestamp": 1467995573000,
"committer": {
"name": "Mark Williams",
"emailAddress": ""
},
"committerTimestamp": 1467995573000,
"message": "Merge branch 'test' of https://proxy:8443/bitbucket/scm/mir/mirror",
"parents": [
{
"id": "0ba3baf0cac25e297f3c391bed6ba0f126f90378",
"displayId": "0ba3baf0cac"
},
{
"id": "60a4b9b7fe505f9e049afa3f2625d884f0aea11e",
"displayId": "60a4b9b7fe5"
}
]
}

# Second Merge Commit with expanded elements
{
"id": "8e5b2bc030dd64edcd048fa873e159a80521c0ec",
"displayId": "8e5b2bc030d",
"author": {
"name": "admin",
"emailAddress": "",
"id": 402,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://stash0:7990/bitbucket/users/admin"
}
]
}
},
"authorTimestamp": 1459285021000,
"committer": {
"name": "admin",
"emailAddress": "",
"id": 402,
"displayName": "admin",
"active": true,
"slug": "admin",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://stash0:7990/bitbucket/users/admin"
}
]
}
},
"committerTimestamp": 1459285021000,
"message": "Merge branch 'master' of https://proxy:8443/bitbucket/scm/mir/mirror",
"parents": [
{
"id": "4061f6ec24091ad552a01fd582748e17c269e637",
"displayId": "4061f6ec240"
},
{
"id": "ce216abf3393a46393d9031ffc1b5928ad7cbac6",
"displayId": "ce216abf339"
}
]
},

You can also play around with the compare commits endpoint 

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMPARE/COMMITS?FROM&TO

Again, you will have to filter the JSON results to get the MESSAGE that references Merging.

Unfortunately, there is nothing at the moment that gets directly to the core of your request.

-Mark

Thank you very much Mark!

Compare commits endpoint worked for me, I am comparing the release branch with master and filtering all the merged commits.

Thank you.

Mark Askew Atlassian Team Sep 12, 2017

Awesome to hear. I'm glad that is working for you.

Cheers,

Mark

Thanks Mark, Yes, that is the only option currently!

This widget could not be displayed.

Sorry to comment on an old thread, but what Amit mentioned "comparing the release branch with master" and getting the commits back is exactly what I want to do, but I don't seem to understand what the syntax would be. Would someone be so kind as to provide an example? 

 

If you're on Bitbucket Server, experiment with the compare commits rest api.

/REST/API/1.0/PROJECTS/{PROJECTKEY}/REPOS/{REPOSITORYSLUG}/COMPARE/COMMITS?FROM&TO&FROMREPO

> Gets the commits accessible from the {@code from} commit but not in the {@code to} commit.

FROM and TO can be commit-ids or branch names (e.g., FROM=release, TO=master).

If you don't get the results you expected, try flipping the values (e.g., FROM=master, TO=release).

 

 

 

thanks for the prompt reply, I think I'm getting somewhere. I also learned that branch names are case sensitive. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 21, 2018 in Bitbucket

Branch Management with Bitbucket

As a project manager, I have discovered that different developers want to bring their previous branching method with them when they join the team. Some developers are used to performing individual wo...

1,217 views 8 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you