Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

4 answers

1 accepted

0 votes
Answer accepted
Mark A_ 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 A_ 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 A_ 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

Hi, I am also trying to get all commits without merge commits. I used merges=exclude query parameter but it doesn't work. i'm getting all commits.

 

please suggest me a solution how to achieve this. 

The Bitbucket Server REST API can do this.   This means any on-prem Bitbucket instance can do this, including Bitbucket Data Center instances.

The Bitbucket Cloud REST API cannot.

And so I have some bad news for Amulya Kota and Harshil Shah:  api.bitbucket.org refers exclusively to Bitbucket Cloud (a.k.a. bitbucket.org), and its REST API does not support this kind of filtering.

You will need to process the JSON in your own code and ignore all results where {commit}.parents.length > 1.

Whenever a commit object's parents array has 2 or more entries you are looking at a merge.

Like akota likes this

Thanks for your reply. I'll try that. 

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 A_ 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 A_ 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!

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. 

Hi @Sylvie Davies _bit-booster_com_  

Do you know if the compare commit is like the command:

git log commit .. commit2    ?

means that you get list all commits that are reachable from commit1 and NOT reachable form 2 ?

If not, is it supported command in REST API?

Revisiting the "merges=exclude" topic above, I'm using Bitbucket Server and trying merges=exclude as follows, and it has no effect. It 

...compare/commits?from=fromBranch&to=toBranch&limit=1000&merges=exclude

Also, "Whenever a commit object's parents array has 2 or more entries you are looking at a merge" doesn't seem to be correct. I'm looking at merge commits that only have one parent entry. 

I'm resorting to doing a string search on the commit message to exclude merge commits. 

Am I missing something? Should I be able to exclude merge commits? 

Thanks in advance. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Calling any interview participants for Bitbucket Data Center

Hi everyone,  We are looking to learn more about development teams’ workflows and pain points, especially around DevOps, integrations, administration, scale, security, and the related challeng...

485 views 5 4
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you