Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a branch using Bitbucket REST API

Using postman, I have succeeded in creating branches in bitbucket via their REST API and using the guide below

Bitbucket Rest API Guide

But I cannot delete a branch using the same guide as well.

I have created a JSON and placed it in the body tab of POSTMAN then using DELETE for the HTTP method but no success.

I am getting error 405 - Method Not Allowed, what does that mean? Did my request pushed through but I am not allowed?

Using the bitbucket web UI, I am able to delete and create branches.

Edit:

This is the postman generated CURL

curl -X DELETE https://<url>/rest/api/1.0/projects/<abc>/repos/<xyz>/branches 
-H 'authorization: Bearer xxxxxx' -H 'cache-control: no-cache' 
-H 'content-type: application/json'  
-H 'x-atlassian-token: nocheck' 
-d '{"name": "refs/heads/feature/name","dryRun": false}'
like image 987
Cante Ibanez Avatar asked Apr 24 '18 16:04

Cante Ibanez


People also ask

How do I delete a branch in Bitbucket?

In Bitbucket go to branches on the left hand side menu. Select your branch you want to delete. Go to action column, click on three dots (...) and select "delete branch".

How do I delete a branch in Atlassian?

Delete BranchClick on the "Branch" button. Click on "Delete Branches" tab. Check the branch or branches you intend to delete. Click on "Delete Branches".

How do I make a branch not Deletable in Bitbucket?

Add a user, check/uncheck the option, remove the user and save. Just a thought/idea, you may want to solve this problem at a lower layer, in `git` you can use an update-hook and simply prevent the deletion. This should live with the repo and protect it where it sits Bitbucket cloud or server.


1 Answers

It looks like you're using the incorrect REST endpoint, one that doesn't accept the DELETE HTTP verb. The one you're using is:

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

As per the docs, this endpoint only accepts GETs and POSTs

Judging by the format of your call, I'm guessing the one you're actually after is this:

/rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

The branch-utils API docs describe more or less the exact payload you're trying to use.

Digging a little deeper and I believe this mistake isn't your fault. The Bitbucket branch util docs for Bitbucket 5.8 and below show the correct URL path, but for 5.9 and up the path seems to be missing.

Full disclosure: I work for Atlassian. That being the case I'll chase this up and have it corrected :)

Edit: the docs have since been fixed!

like image 102
daveruinseverything Avatar answered Oct 13 '22 19:10

daveruinseverything