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

2 answers

1 accepted

0 votes

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 Williams 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

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.

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

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.

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

Cheers,

Mark

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

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

630 views 0 4
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot