diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-26 23:05:22 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-26 23:05:22 -0700 |
commit | 3f715bb438f81d8847e48d4fa76d1827b2df88eb (patch) | |
tree | 9cacc78580d951fd7378220c06e3766ae9b37e55 | |
parent | 079b490ad2106e83bf315c7f3e9ac5e1d60c0d0c (diff) | |
download | gitlab-ce-3f715bb438f81d8847e48d4fa76d1827b2df88eb.tar.gz |
Consolidate server errors and add specs
-rw-r--r-- | app/controllers/import/bitbucket_server_controller.rb | 14 | ||||
-rw-r--r-- | lib/bitbucket_server/client.rb | 20 | ||||
-rw-r--r-- | spec/controllers/import/bitbucket_server_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/bitbucket_server/client_spec.rb | 86 |
4 files changed, 106 insertions, 16 deletions
diff --git a/app/controllers/import/bitbucket_server_controller.rb b/app/controllers/import/bitbucket_server_controller.rb index 0b57f4eef1f..95e49adde52 100644 --- a/app/controllers/import/bitbucket_server_controller.rb +++ b/app/controllers/import/bitbucket_server_controller.rb @@ -15,16 +15,6 @@ class Import::BitbucketServerController < Import::BaseController # (https://community.atlassian.com/t5/Answers-Developer-Questions/stash-repository-names/qaq-p/499054) 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 end @@ -52,7 +42,7 @@ class Import::BitbucketServerController < Import::BaseController else render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity end - rescue *SERVER_ERRORS => e + rescue BitbucketServer::Client::ServerError => e render json: { errors: "Unable to connect to server: #{e}" }, status: :unprocessable_entity end @@ -73,7 +63,7 @@ class Import::BitbucketServerController < Import::BaseController already_added_projects_names = @already_added_projects.pluck(:import_source) @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}" clear_session_data redirect_to new_import_bitbucket_server_path diff --git a/lib/bitbucket_server/client.rb b/lib/bitbucket_server/client.rb index 85c9757339e..15e59f93141 100644 --- a/lib/bitbucket_server/client.rb +++ b/lib/bitbucket_server/client.rb @@ -4,6 +4,18 @@ module BitbucketServer class Client 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 = {}) @connection = Connection.new(options) end @@ -13,8 +25,8 @@ module BitbucketServer get_collection(path, :pull_request) end - def activities(project_key, repo, pull_request) - path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request}/activities" + def activities(project_key, repo, pull_request_id) + path = "/projects/#{project_key}/repos/#{repo}/pull-requests/#{pull_request_id}/activities" get_collection(path, :activity) end @@ -50,8 +62,10 @@ module BitbucketServer private 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) + rescue *SERVER_ERRORS => e + raise ServerError, e end end end diff --git a/spec/controllers/import/bitbucket_server_controller_spec.rb b/spec/controllers/import/bitbucket_server_controller_spec.rb index 8d0034b2cc2..5024ef71771 100644 --- a/spec/controllers/import/bitbucket_server_controller_spec.rb +++ b/spec/controllers/import/bitbucket_server_controller_spec.rb @@ -78,7 +78,7 @@ describe Import::BitbucketServerController do end 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 diff --git a/spec/lib/bitbucket_server/client_spec.rb b/spec/lib/bitbucket_server/client_spec.rb new file mode 100644 index 00000000000..d9e0a0677b2 --- /dev/null +++ b/spec/lib/bitbucket_server/client_spec.rb @@ -0,0 +1,86 @@ +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 |