diff --git a/lib/bitbucket_server/client.rb b/lib/bitbucket_server/client.rb index 802fe7953cec3e843288796baba03af672c96992..bfa8301f6430032b7ff48e2802950d64cb1835bd 100644 --- a/lib/bitbucket_server/client.rb +++ b/lib/bitbucket_server/client.rb @@ -36,6 +36,15 @@ module BitbucketServer connection.post("/projects/#{project_key}/repos/#{repo}/branches", payload.to_json) end + def delete_branch(project_key, repo, branch_name, sha) + payload = { + name: Gitlab::Git::BRANCH_REF_PREFIX + branch_name, + dryRun: false + } + + connection.delete(:branches, "/projects/#{project_key}/repos/#{repo}/branches", payload.to_json) + end + private def get_collection(path, type) diff --git a/lib/bitbucket_server/connection.rb b/lib/bitbucket_server/connection.rb index 853d09b776a441e48b294e169898a98ae99886c5..0b65203a8240cdf734fd39aa88641638d818b8bf 100644 --- a/lib/bitbucket_server/connection.rb +++ b/lib/bitbucket_server/connection.rb @@ -36,10 +36,27 @@ module BitbucketServer response.parsed_response end + # We need to support two different APIs for deletion: + # + # /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches/default + # /rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches + def delete(resource, path, body) + url = delete_url(resource, path) + + response = Gitlab::HTTP.delete(url, + basic_auth: auth, + headers: post_headers, + body: body) + + check_errors!(response) + + response.parsed_response + end + private def check_errors!(response) - return if response.code == 200 + return if response.code >= 200 && response.code < 300 details = if response.parsed_response && response.parsed_response.is_a?(Hash) @@ -68,5 +85,13 @@ module BitbucketServer def root_url "#{base_uri}/rest/api/#{api_version}" end + + def delete_url(resource, path) + if resource == :branches + "#{base_uri}/branch-utils/#{api_version}#{path}" + else + build_url(path) + end + end end end diff --git a/spec/lib/bitbucket_server/connection_spec.rb b/spec/lib/bitbucket_server/connection_spec.rb index 45affbcf4e4c7a01665d907605693dbe15ac7fde..65c78f14f58fe6aa54264f354d822202df7f5131 100644 --- a/spec/lib/bitbucket_server/connection_spec.rb +++ b/spec/lib/bitbucket_server/connection_spec.rb @@ -35,4 +35,24 @@ describe BitbucketServer::Connection do expect { subject.post(url, payload) }.to raise_error(described_class::ConnectionError) end end + + describe '#delete' do + context 'branch API' do + let(:branch_path) { '/projects/foo/repos/bar/branches' } + let(:branch_url) { 'https://test:7990/branch-utils/1.0/projects/foo/repos/bar/branches' } + let(:path) { } + + it 'returns JSON body' do + WebMock.stub_request(:delete, branch_url).to_return(body: payload.to_json, status: 200, headers: headers) + + expect(subject.delete(:branches, branch_path, payload)).to eq(payload) + end + + it 'throws an exception if the response is not 200' do + WebMock.stub_request(:delete, branch_url).to_return(body: payload.to_json, status: 500, headers: headers) + + expect { subject.delete(:branches, branch_path, payload) }.to raise_error(described_class::ConnectionError) + end + end + end end