Commit ec4109d4 authored by Stan Hu's avatar Stan Hu

Make Connection#post consistent with Connection#get and add specs

parent 15220291
......@@ -33,22 +33,22 @@ module BitbucketServer
check_errors!(response)
response
response.parsed_response
end
private
def check_errors!(response)
if response.code != 200
error =
if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message'))
end
message = "Error #{response.code}"
message += ": #{error}" if error
raise ConnectionError, message
end
return if response.code == 200
details =
if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message'))
end
message = "Error #{response.code}"
message += ": #{details}" if details
raise ConnectionError, message
end
def auth
......
......@@ -90,12 +90,11 @@ module Gitlab
shas.each do |branch_name, sha|
next if sha_exists?(sha)
response = client.create_branch(project_key, repository_slug, branch_name, sha)
if response.success?
begin
client.create_branch(project_key, repository_slug, branch_name, sha)
branches_created << branch_name
else
Rails.logger.warn("BitbucketServerImporter: Unable to recreate branch for SHA #{sha}: #{response.code}")
rescue BitbucketServer::Connection::ConnectionError => e
Rails.logger.warn("BitbucketServerImporter: Unable to recreate branch for SHA #{sha}: #{e}")
end
end
end
......
......@@ -4,12 +4,11 @@ describe BitbucketServer::Connection do
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } }
let(:payload) { { 'test' => 1 } }
let(:headers) { { "Content-Type" => "application/json" } }
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
subject { described_class.new(options) }
describe '#get' do
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
it 'returns JSON body' do
WebMock.stub_request(:get, url).to_return(body: payload.to_json, status: 200, headers: headers)
......@@ -24,5 +23,16 @@ describe BitbucketServer::Connection do
end
describe '#post' do
it 'returns JSON body' do
WebMock.stub_request(:post, url).to_return(body: payload.to_json, status: 200, headers: headers)
expect(subject.post(url, payload)).to eq(payload)
end
it 'throws an exception if the response is not 200' do
WebMock.stub_request(:post, url).to_return(body: payload.to_json, status: 500, headers: headers)
expect { subject.post(url, payload) }.to raise_error(described_class::ConnectionError)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment