gihan-maduranga
gihan-maduranga

Reputation: 4801

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.

Upvotes: 4

Views: 4450

Answers (2)

Rahul Sahotay
Rahul Sahotay

Reputation: 165

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"][]'

Upvotes: 2

Sergii Zaskaleta
Sergii Zaskaleta

Reputation: 502

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

Upvotes: 6

Related Questions