Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get branch head commit using stash rest api

I am having some problem with getting HEAD commit from a stash branch.I can get branch commits using following REST call.

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits?until={branch Name}

Also i can get HEAD commit in master using /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits/HEAD

like wise i would like to know that how can i get branch HEAD commit using Stash REST API.

like image 388
gihan-maduranga Avatar asked Oct 18 '25 16:10

gihan-maduranga


2 Answers

For a branch name without forward slashes below works perfect:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits/develop
/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits/master

But this fails when referring to feature/JIRA-123-foo-bar-like branches.
Alternatively you could invoke

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

which will give you all the branches and their latest commits. See Stash API documentation for more

like image 63
Sergii Zaskaleta Avatar answered Oct 20 '25 06:10

Sergii Zaskaleta


If anyone is looking to get a commit message from feature branch, please have a look:-

  1. Get the commit id by using export COMMIT=`curl -s -H 'Authorization: Basic ***************=' --request GET https://bitbucket.your-organisation.com/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches | jq -r '[.values[]."latestCommit"][0]'` This will give you the latest commit to your repository(you can even limit this for feature branch.

  2. Get commit message by using commit id curl -s -H 'Authorization: Basic ***************=' --request GET https://bitbucket.your-organisation.com/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits/$COMMIT | jq . Note: if you are specifically looking to get JIRA id from the commit message, replace 2nd command by following curl -s -H 'Authorization: Basic ***************=' --request GET https://bitbucket.your-organisation.com/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits/$COMMIT | jq -r '.properties["jira-key"][]'

like image 25
Rahul Sahotay Avatar answered Oct 20 '25 06:10

Rahul Sahotay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!