Commit ec4109d4 authored by Stan Hu's avatar Stan Hu

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

parent 15220291
...@@ -33,23 +33,23 @@ module BitbucketServer ...@@ -33,23 +33,23 @@ module BitbucketServer
check_errors!(response) check_errors!(response)
response response.parsed_response
end end
private private
def check_errors!(response) def check_errors!(response)
if response.code != 200 return if response.code == 200
error =
details =
if response.parsed_response && response.parsed_response.is_a?(Hash) if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message')) sanitize(response.parsed_response.dig('errors', 0, 'message'))
end end
message = "Error #{response.code}" message = "Error #{response.code}"
message += ": #{error}" if error message += ": #{details}" if details
raise ConnectionError, message raise ConnectionError, message
end end
end
def auth def auth
@auth ||= { username: username, password: token } @auth ||= { username: username, password: token }
......
...@@ -90,12 +90,11 @@ module Gitlab ...@@ -90,12 +90,11 @@ module Gitlab
shas.each do |branch_name, sha| shas.each do |branch_name, sha|
next if sha_exists?(sha) next if sha_exists?(sha)
response = client.create_branch(project_key, repository_slug, branch_name, sha) begin
client.create_branch(project_key, repository_slug, branch_name, sha)
if response.success?
branches_created << branch_name branches_created << branch_name
else rescue BitbucketServer::Connection::ConnectionError => e
Rails.logger.warn("BitbucketServerImporter: Unable to recreate branch for SHA #{sha}: #{response.code}") Rails.logger.warn("BitbucketServerImporter: Unable to recreate branch for SHA #{sha}: #{e}")
end end
end end
end end
......
...@@ -4,12 +4,11 @@ describe BitbucketServer::Connection do ...@@ -4,12 +4,11 @@ describe BitbucketServer::Connection do
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } } let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } }
let(:payload) { { 'test' => 1 } } let(:payload) { { 'test' => 1 } }
let(:headers) { { "Content-Type" => "application/json" } } let(:headers) { { "Content-Type" => "application/json" } }
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
subject { described_class.new(options) } subject { described_class.new(options) }
describe '#get' do describe '#get' do
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
it 'returns JSON body' do it 'returns JSON body' do
WebMock.stub_request(:get, url).to_return(body: payload.to_json, status: 200, headers: headers) WebMock.stub_request(:get, url).to_return(body: payload.to_json, status: 200, headers: headers)
...@@ -24,5 +23,16 @@ describe BitbucketServer::Connection do ...@@ -24,5 +23,16 @@ describe BitbucketServer::Connection do
end end
describe '#post' do 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
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