Skip to content
Snippets Groups Projects
Commit 81f5955e authored by Bob Van Landuyt's avatar Bob Van Landuyt
Browse files

Move Repository#wrapped_gitaly_errors into concern

Having this in a concern allows us to reuse it for different single
purpose classes that call out to git without going through the
repository every time.
parent 912741cf
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -5,6 +5,7 @@ module Gitlab
class Blob
include Gitlab::BlobHelper
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
 
# This number is the maximum amount of data that we want to display to
# the user. We load as much as we can for encoding detection and LFS
Loading
Loading
@@ -75,7 +76,7 @@ module Gitlab
# Returns array of Gitlab::Git::Blob
# Does not guarantee blob data will be set
def batch_lfs_pointers(repository, blob_ids)
repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repository.gitaly_blob_client.batch_lfs_pointers(blob_ids.to_a)
end
end
Loading
Loading
Loading
Loading
@@ -3,6 +3,7 @@ module Gitlab
module Git
class Commit
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
 
attr_accessor :raw_commit, :head
 
Loading
Loading
@@ -59,7 +60,7 @@ module Gitlab
# This saves us an RPC round trip.
return nil if commit_id.include?(':')
 
commit = repo.wrapped_gitaly_errors do
commit = wrapped_gitaly_errors do
repo.gitaly_commit_client.find_commit(commit_id)
end
 
Loading
Loading
@@ -100,7 +101,7 @@ module Gitlab
# Commit.between(repo, '29eda46b', 'master')
#
def between(repo, base, head)
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repo.gitaly_commit_client.between(base, head)
end
end
Loading
Loading
@@ -125,7 +126,7 @@ module Gitlab
# are documented here:
# http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant)
def find_all(repo, options = {})
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
Gitlab::GitalyClient::CommitService.new(repo).find_all_commits(options)
end
end
Loading
Loading
@@ -142,7 +143,7 @@ module Gitlab
# relation to each other. The last 10 commits for a branch for example,
# should go through .where
def batch_by_oid(repo, oids)
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repo.gitaly_commit_client.list_commits_by_oid(oids)
end
end
Loading
Loading
Loading
Loading
@@ -3,6 +3,8 @@
module Gitlab
module Git
class CommitStats
include Gitlab::Git::WrapsGitalyErrors
attr_reader :id, :additions, :deletions, :total
 
# Instantiate a CommitStats object
Loading
Loading
@@ -14,7 +16,7 @@ module Gitlab
@deletions = 0
@total = 0
 
repo.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_stats(repo, commit)
end
end
Loading
Loading
Loading
Loading
@@ -2,6 +2,8 @@ module Gitlab
module Git
module Conflict
class Resolver
include Gitlab::Git::WrapsGitalyErrors
ConflictSideMissing = Class.new(StandardError)
ResolutionError = Class.new(StandardError)
 
Loading
Loading
@@ -12,7 +14,7 @@ module Gitlab
end
 
def conflicts
@conflicts ||= @target_repository.wrapped_gitaly_errors do
@conflicts ||= wrapped_gitaly_errors do
gitaly_conflicts_client(@target_repository).list_conflict_files.to_a
end
rescue GRPC::FailedPrecondition => e
Loading
Loading
@@ -22,7 +24,7 @@ module Gitlab
end
 
def resolve_conflicts(source_repository, resolution, source_branch:, target_branch:)
source_repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_conflicts_client(source_repository).resolve_conflicts(@target_repository, resolution, source_branch, target_branch)
end
end
Loading
Loading
module Gitlab
module Git
class RemoteMirror
include Gitlab::Git::WrapsGitalyErrors
def initialize(repository, ref_name)
@repository = repository
@ref_name = ref_name
end
 
def update(only_branches_matching: [])
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
@repository.gitaly_remote_client.update_remote_mirror(@ref_name, only_branches_matching)
end
end
Loading
Loading
Loading
Loading
@@ -6,6 +6,7 @@ module Gitlab
module Git
class Repository
include Gitlab::Git::RepositoryMirroring
include Gitlab::Git::WrapsGitalyErrors
include Gitlab::EncodingHelper
include Gitlab::Utils::StrongMemoize
 
Loading
Loading
@@ -845,23 +846,9 @@ module Gitlab
end
 
def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block)
Gitlab::GitalyClient.migrate(method, status: status, &block)
rescue GRPC::NotFound => e
raise NoRepository.new(e)
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise CommandError.new(e)
end
def wrapped_gitaly_errors(&block)
yield block
rescue GRPC::NotFound => e
raise NoRepository.new(e)
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise CommandError.new(e)
wrapped_gitaly_errors do
Gitlab::GitalyClient.migrate(method, status: status, &block)
end
end
 
def clean_stale_repository_files
Loading
Loading
Loading
Loading
@@ -2,6 +2,7 @@ module Gitlab
module Git
class Tree
include Gitlab::EncodingHelper
extend Gitlab::Git::WrapsGitalyErrors
 
attr_accessor :id, :root_id, :name, :path, :flat_path, :type,
:mode, :commit_id, :submodule_url
Loading
Loading
@@ -15,7 +16,7 @@ module Gitlab
def where(repository, sha, path = nil, recursive = false)
path = nil if path == '' || path == '/'
 
repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
repository.gitaly_commit_client.tree_entries(repository, sha, path, recursive)
end
end
Loading
Loading
module Gitlab
module Git
class Wiki
include Gitlab::Git::WrapsGitalyErrors
DuplicatePageError = Class.new(StandardError)
OperationError = Class.new(StandardError)
 
Loading
Loading
@@ -65,37 +67,37 @@ module Gitlab
end
 
def write_page(name, format, content, commit_details)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_write_page(name, format, content, commit_details)
end
end
 
def delete_page(page_path, commit_details)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_delete_page(page_path, commit_details)
end
end
 
def update_page(page_path, title, format, content, commit_details)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_update_page(page_path, title, format, content, commit_details)
end
end
 
def pages(limit: 0)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_get_all_pages(limit: limit)
end
end
 
def page(title:, version: nil, dir: nil)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_find_page(title: title, version: version, dir: dir)
end
end
 
def file(name, version)
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_find_file(name, version)
end
end
Loading
Loading
@@ -105,7 +107,7 @@ module Gitlab
# :per_page - The number of items per page.
# :limit - Total number of items to return.
def page_versions(page_path, options = {})
versions = @repository.wrapped_gitaly_errors do
versions = wrapped_gitaly_errors do
gitaly_wiki_client.page_versions(page_path, options)
end
 
Loading
Loading
@@ -127,7 +129,7 @@ module Gitlab
def page_formatted_data(title:, dir: nil, version: nil)
version = version&.id
 
@repository.wrapped_gitaly_errors do
wrapped_gitaly_errors do
gitaly_wiki_client.get_formatted_data(title: title, dir: dir, version: version)
end
end
Loading
Loading
module Gitlab
module Git
module WrapsGitalyErrors
def wrapped_gitaly_errors(&block)
yield block
rescue GRPC::NotFound => e
raise Gitlab::Git::Repository::NoRepository.new(e)
rescue GRPC::InvalidArgument => e
raise ArgumentError.new(e)
rescue GRPC::BadStatus => e
raise Gitlab::Git::CommandError.new(e)
end
end
end
end
require 'spec_helper'
describe Gitlab::Git::WrapsGitalyErrors do
subject(:wrapper) do
klazz = Class.new { include Gitlab::Git::WrapsGitalyErrors }
klazz.new
end
describe "#wrapped_gitaly_errors" do
mapping = {
GRPC::NotFound => Gitlab::Git::Repository::NoRepository,
GRPC::InvalidArgument => ArgumentError,
GRPC::BadStatus => Gitlab::Git::CommandError
}
mapping.each do |grpc_error, error|
it "wraps #{grpc_error} in a #{error}" do
expect { wrapper.wrapped_gitaly_errors { raise grpc_error.new('wrapped') } }
.to raise_error(error)
end
end
it 'does not swallow other errors' do
expect { wrapper.wrapped_gitaly_errors { raise 'raised' } }
.to raise_error(RuntimeError)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment