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

0 vote

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!

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

85 views 0 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