Make Connection#post consistent with Connection#get and add specs
This commit is contained in:
parent
15220291ae
commit
ec4109d413
|
@ -33,22 +33,22 @@ 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 =
|
|
||||||
if response.parsed_response && response.parsed_response.is_a?(Hash)
|
|
||||||
sanitize(response.parsed_response.dig('errors', 0, 'message'))
|
|
||||||
end
|
|
||||||
|
|
||||||
message = "Error #{response.code}"
|
details =
|
||||||
message += ": #{error}" if error
|
if response.parsed_response && response.parsed_response.is_a?(Hash)
|
||||||
raise ConnectionError, message
|
sanitize(response.parsed_response.dig('errors', 0, 'message'))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
message = "Error #{response.code}"
|
||||||
|
message += ": #{details}" if details
|
||||||
|
raise ConnectionError, message
|
||||||
end
|
end
|
||||||
|
|
||||||
def auth
|
def auth
|
||||||
|
|
|
@ -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
|
||||||
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
|
||||||
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
|
||||||
|
|
Loading…
Reference in New Issue