Commit 3f715bb4 authored by Stan Hu's avatar Stan Hu

Consolidate server errors and add specs

parent 079b490a
...@@ -15,16 +15,6 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -15,16 +15,6 @@ class Import::BitbucketServerController < Import::BaseController
# (https://community.atlassian.com/t5/Answers-Developer-Questions/stash-repository-names/qaq-p/499054) # (https://community.atlassian.com/t5/Answers-Developer-Questions/stash-repository-names/qaq-p/499054)
VALID_BITBUCKET_CHARS = /\A[a-zA-z0-9\-_\.\s]+\z/ VALID_BITBUCKET_CHARS = /\A[a-zA-z0-9\-_\.\s]+\z/
SERVER_ERRORS = [SocketError,
OpenSSL::SSL::SSLError,
Errno::ECONNRESET,
Errno::ECONNREFUSED,
Errno::EHOSTUNREACH,
Net::OpenTimeout,
Net::ReadTimeout,
Gitlab::HTTP::BlockedUrlError,
BitbucketServer::Connection::ConnectionError].freeze
def new def new
end end
...@@ -52,7 +42,7 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -52,7 +42,7 @@ class Import::BitbucketServerController < Import::BaseController
else else
render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
end end
rescue *SERVER_ERRORS => e rescue BitbucketServer::Client::ServerError => e
render json: { errors: "Unable to connect to server: #{e}" }, status: :unprocessable_entity render json: { errors: "Unable to connect to server: #{e}" }, status: :unprocessable_entity
end end
...@@ -73,7 +63,7 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -73,7 +63,7 @@ class Import::BitbucketServerController < Import::BaseController
already_added_projects_names = @already_added_projects.pluck(:import_source) already_added_projects_names = @already_added_projects.pluck(:import_source)
@repos.to_a.reject! { |repo| already_added_projects_names.include?(repo.browse_url) } @repos.to_a.reject! { |repo| already_added_projects_names.include?(repo.browse_url) }
rescue *SERVER_ERRORS => e rescue BitbucketServer::Connection::ConnectionError, BitbucketServer::Client::ServerError => e
flash[:alert] = "Unable to connect to server: #{e}" flash[:alert] = "Unable to connect to server: #{e}"
clear_session_data clear_session_data
redirect_to new_import_bitbucket_server_path redirect_to new_import_bitbucket_server_path
......
...@@ -4,6 +4,18 @@ module BitbucketServer ...@@ -4,6 +4,18 @@ module BitbucketServer
class Client class Client
attr_reader :connection attr_reader :connection
ServerError = Class.new(StandardError)
SERVER_ERRORS = [SocketError,
OpenSSL::SSL::SSLError,
Errno::ECONNRESET,
Errno::ECONNREFUSED,
Errno::EHOSTUNREACH,
Net::OpenTimeout,
Net::ReadTimeout,
Gitlab::HTTP::BlockedUrlError,
BitbucketServer::Connection::ConnectionError].freeze
def initialize(options = {}) def initialize(options = {})
@connection = Connection.new(options) @connection = Connection.new(options)
end end
...@@ -13,8 +25,8 @@ module BitbucketServer ...@@ -13,8 +25,8 @@ module BitbucketServer
get_collection(path, :pull_request) get_collection(path, :pull_request)
end end
def activities(project_key, repo, pull_request) def activities(project_key, repo, pull_request_id)
path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request}/activities" path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request_id}/activities"
get_collection(path, :activity) get_collection(path, :activity)
end end
...@@ -50,8 +62,10 @@ module BitbucketServer ...@@ -50,8 +62,10 @@ module BitbucketServer
private private
def get_collection(path, type) def get_collection(path, type)
paginator = BitbucketServer::Paginator.new(connection, path, type) paginator = BitbucketServer::Paginator.new(connection, Addressable::URI.escape(path), type)
BitbucketServer::Collection.new(paginator) BitbucketServer::Collection.new(paginator)
rescue *SERVER_ERRORS => e
raise ServerError, e
end end
end end
end end
...@@ -78,7 +78,7 @@ describe Import::BitbucketServerController do ...@@ -78,7 +78,7 @@ describe Import::BitbucketServerController do
end end
it "returns an error when the server can't be contacted" do it "returns an error when the server can't be contacted" do
expect(client).to receive(:repo).with(project_key, repo_slug).and_raise(Errno::ECONNREFUSED) expect(client).to receive(:repo).with(project_key, repo_slug).and_raise(BitbucketServer::Client::ServerError)
post :create, project: project_key, repository: repo_slug, format: :json post :create, project: project_key, repository: repo_slug, format: :json
......
require 'spec_helper'
describe BitbucketServer::Client do
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } }
let(:project) { 'SOME-PROJECT' }
let(:repo_slug) { 'my-repo' }
subject { described_class.new(options) }
describe '#pull_requests' do
let(:path) { "/projects/#{project}/repos/#{repo_slug}/pull-requests?state=ALL" }
it 'requests a collection' do
expect(BitbucketServer::Paginator).to receive(:new).with(anything, path, :pull_request)
subject.pull_requests(project, repo_slug)
end
it 'throws an exception when connection fails' do
allow(BitbucketServer::Collection).to receive(:new).and_raise(OpenSSL::SSL::SSLError)
expect { subject.pull_requests(project, repo_slug) }.to raise_error(described_class::ServerError)
end
end
describe '#activities' do
let(:path) { "/projects/#{project}/repos/#{repo_slug}/pull-requests/1/activities" }
it 'requests a collection' do
expect(BitbucketServer::Paginator).to receive(:new).with(anything, path, :activity)
subject.activities(project, repo_slug, 1)
end
end
describe '#repo' do
let(:path) { "/projects/#{project}/repos/#{repo_slug}" }
let(:url) { "https://test:7990/rest/api/1.0/projects/SOME-PROJECT/repos/my-repo" }
it 'requests a specific repository' do
stub_request(:get, url)
subject.repo(project, repo_slug)
expect(WebMock).to have_requested(:get, url)
end
end
describe '#repos' do
let(:path) { "/repos" }
it 'requests a collection' do
expect(BitbucketServer::Paginator).to receive(:new).with(anything, path, :repo)
subject.repos
end
end
describe '#create_branch' do
let(:branch) { 'test-branch' }
let(:sha) { '12345678' }
let(:url) { 'https://test:7990/rest/api/1.0/projects/SOME-PROJECT/repos/my-repo/branches' }
it 'requests Bitbucket to create a branch' do
stub_request(:post, url)
subject.create_branch(project, repo_slug, branch, sha)
expect(WebMock).to have_requested(:post, url)
end
end
describe '#delete_branch' do
let(:branch) { 'test-branch' }
let(:sha) { '12345678' }
let(:url) { 'https://test:7990/rest/branch-utils/1.0/projects/SOME-PROJECT/repos/my-repo/branches' }
it 'requests Bitbucket to create a branch' do
stub_request(:delete, url)
subject.delete_branch(project, repo_slug, branch, sha)
expect(WebMock).to have_requested(:delete, url)
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