From a89f18bf2c1421460fcb3f42aac538df51660912 Mon Sep 17 00:00:00 2001 From: Andrew Newdigate Date: Tue, 18 Jul 2017 07:59:36 +0000 Subject: Renamed Gitaly services --- app/models/commit.rb | 4 +- lib/api/internal.rb | 2 +- lib/gitlab/git/blob.rb | 8 +- lib/gitlab/git/repository.rb | 4 +- lib/gitlab/gitaly_client/blob.rb | 30 ------ lib/gitlab/gitaly_client/blob_service.rb | 30 ++++++ lib/gitlab/gitaly_client/commit.rb | 82 --------------- lib/gitlab/gitaly_client/commit_service.rb | 82 +++++++++++++++ lib/gitlab/gitaly_client/notification_service.rb | 20 ++++ lib/gitlab/gitaly_client/notifications.rb | 20 ---- lib/gitlab/gitaly_client/ref.rb | 110 --------------------- lib/gitlab/gitaly_client/ref_service.rb | 110 +++++++++++++++++++++ spec/lib/gitlab/git/repository_spec.rb | 18 ++-- .../gitlab/gitaly_client/commit_service_spec.rb | 85 ++++++++++++++++ spec/lib/gitlab/gitaly_client/commit_spec.rb | 85 ---------------- .../gitaly_client/notification_service_spec.rb | 17 ++++ .../lib/gitlab/gitaly_client/notifications_spec.rb | 17 ---- spec/lib/gitlab/gitaly_client/ref_service_spec.rb | 83 ++++++++++++++++ spec/lib/gitlab/gitaly_client/ref_spec.rb | 83 ---------------- spec/lib/gitlab/gitaly_client_spec.rb | 8 +- spec/requests/api/internal_spec.rb | 18 ++-- 21 files changed, 458 insertions(+), 458 deletions(-) delete mode 100644 lib/gitlab/gitaly_client/blob.rb create mode 100644 lib/gitlab/gitaly_client/blob_service.rb delete mode 100644 lib/gitlab/gitaly_client/commit.rb create mode 100644 lib/gitlab/gitaly_client/commit_service.rb create mode 100644 lib/gitlab/gitaly_client/notification_service.rb delete mode 100644 lib/gitlab/gitaly_client/notifications.rb delete mode 100644 lib/gitlab/gitaly_client/ref.rb create mode 100644 lib/gitlab/gitaly_client/ref_service.rb create mode 100644 spec/lib/gitlab/gitaly_client/commit_service_spec.rb delete mode 100644 spec/lib/gitlab/gitaly_client/commit_spec.rb create mode 100644 spec/lib/gitlab/gitaly_client/notification_service_spec.rb delete mode 100644 spec/lib/gitlab/gitaly_client/notifications_spec.rb create mode 100644 spec/lib/gitlab/gitaly_client/ref_service_spec.rb delete mode 100644 spec/lib/gitlab/gitaly_client/ref_spec.rb diff --git a/app/models/commit.rb b/app/models/commit.rb index c7f62617c4c..21b906e1110 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -322,7 +322,7 @@ class Commit def raw_diffs(*args) if Gitlab::GitalyClient.feature_enabled?(:commit_raw_diffs) - Gitlab::GitalyClient::Commit.new(project.repository).diff_from_parent(self, *args) + Gitlab::GitalyClient::CommitService.new(project.repository).diff_from_parent(self, *args) else raw.diffs(*args) end @@ -331,7 +331,7 @@ class Commit def raw_deltas @deltas ||= Gitlab::GitalyClient.migrate(:commit_deltas) do |is_enabled| if is_enabled - Gitlab::GitalyClient::Commit.new(project.repository).commit_deltas(self) + Gitlab::GitalyClient::CommitService.new(project.repository).commit_deltas(self) else raw.deltas end diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 465363da582..8b007869dc3 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -150,7 +150,7 @@ module API # # begin # repository = wiki? ? project.wiki.repository : project.repository - # Gitlab::GitalyClient::Notifications.new(repository.raw_repository).post_receive + # Gitlab::GitalyClient::NotificationService.new(repository.raw_repository).post_receive # rescue GRPC::Unavailable => e # render_api_error!(e, 500) # end diff --git a/lib/gitlab/git/blob.rb b/lib/gitlab/git/blob.rb index b6dd3cd20e0..db6cfc9671f 100644 --- a/lib/gitlab/git/blob.rb +++ b/lib/gitlab/git/blob.rb @@ -29,7 +29,7 @@ module Gitlab path = path.sub(/\A\/*/, '') path = '/' if path.empty? name = File.basename(path) - entry = Gitlab::GitalyClient::Commit.new(repository).tree_entry(sha, path, MAX_DATA_DISPLAY_SIZE) + entry = Gitlab::GitalyClient::CommitService.new(repository).tree_entry(sha, path, MAX_DATA_DISPLAY_SIZE) return unless entry case entry.type @@ -87,10 +87,10 @@ module Gitlab def raw(repository, sha) Gitlab::GitalyClient.migrate(:git_blob_raw) do |is_enabled| if is_enabled - Gitlab::GitalyClient::Blob.new(repository).get_blob(oid: sha, limit: MAX_DATA_DISPLAY_SIZE) + Gitlab::GitalyClient::BlobService.new(repository).get_blob(oid: sha, limit: MAX_DATA_DISPLAY_SIZE) else blob = repository.lookup(sha) - + new( id: blob.oid, size: blob.size, @@ -182,7 +182,7 @@ module Gitlab Gitlab::GitalyClient.migrate(:git_blob_load_all_data) do |is_enabled| @data = begin if is_enabled - Gitlab::GitalyClient::Blob.new(repository).get_blob(oid: id, limit: -1).data + Gitlab::GitalyClient::BlobService.new(repository).get_blob(oid: id, limit: -1).data else repository.lookup(id).content end diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index 5aefa1404ad..c091bb9dcfe 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -1106,11 +1106,11 @@ module Gitlab end def gitaly_ref_client - @gitaly_ref_client ||= Gitlab::GitalyClient::Ref.new(self) + @gitaly_ref_client ||= Gitlab::GitalyClient::RefService.new(self) end def gitaly_commit_client - @gitaly_commit_client ||= Gitlab::GitalyClient::Commit.new(self) + @gitaly_commit_client ||= Gitlab::GitalyClient::CommitService.new(self) end def gitaly_migrate(method, &block) diff --git a/lib/gitlab/gitaly_client/blob.rb b/lib/gitlab/gitaly_client/blob.rb deleted file mode 100644 index 0c398b46a08..00000000000 --- a/lib/gitlab/gitaly_client/blob.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Gitlab - module GitalyClient - class Blob - def initialize(repository) - @gitaly_repo = repository.gitaly_repository - end - - def get_blob(oid:, limit:) - request = Gitaly::GetBlobRequest.new( - repository: @gitaly_repo, - oid: oid, - limit: limit - ) - response = GitalyClient.call(@gitaly_repo.storage_name, :blob_service, :get_blob, request) - - blob = response.first - return unless blob.oid.present? - - data = response.reduce(blob.data.dup) { |memo, msg| memo << msg.data.dup } - - Gitlab::Git::Blob.new( - id: blob.oid, - size: blob.size, - data: data, - binary: Gitlab::Git::Blob.binary?(data) - ) - end - end - end -end diff --git a/lib/gitlab/gitaly_client/blob_service.rb b/lib/gitlab/gitaly_client/blob_service.rb new file mode 100644 index 00000000000..7ea8e8d0857 --- /dev/null +++ b/lib/gitlab/gitaly_client/blob_service.rb @@ -0,0 +1,30 @@ +module Gitlab + module GitalyClient + class BlobService + def initialize(repository) + @gitaly_repo = repository.gitaly_repository + end + + def get_blob(oid:, limit:) + request = Gitaly::GetBlobRequest.new( + repository: @gitaly_repo, + oid: oid, + limit: limit + ) + response = GitalyClient.call(@gitaly_repo.storage_name, :blob_service, :get_blob, request) + + blob = response.first + return unless blob.oid.present? + + data = response.reduce(blob.data.dup) { |memo, msg| memo << msg.data.dup } + + Gitlab::Git::Blob.new( + id: blob.oid, + size: blob.size, + data: data, + binary: Gitlab::Git::Blob.binary?(data) + ) + end + end + end +end diff --git a/lib/gitlab/gitaly_client/commit.rb b/lib/gitlab/gitaly_client/commit.rb deleted file mode 100644 index aafc0520664..00000000000 --- a/lib/gitlab/gitaly_client/commit.rb +++ /dev/null @@ -1,82 +0,0 @@ -module Gitlab - module GitalyClient - class Commit - # The ID of empty tree. - # See http://stackoverflow.com/a/40884093/1856239 and https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012 - EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'.freeze - - def initialize(repository) - @gitaly_repo = repository.gitaly_repository - @repository = repository - end - - def is_ancestor(ancestor_id, child_id) - request = Gitaly::CommitIsAncestorRequest.new( - repository: @gitaly_repo, - ancestor_id: ancestor_id, - child_id: child_id - ) - - GitalyClient.call(@repository.storage, :commit, :commit_is_ancestor, request).value - end - - def diff_from_parent(commit, options = {}) - request_params = commit_diff_request_params(commit, options) - request_params[:ignore_whitespace_change] = options.fetch(:ignore_whitespace_change, false) - request = Gitaly::CommitDiffRequest.new(request_params) - response = GitalyClient.call(@repository.storage, :diff, :commit_diff, request) - Gitlab::Git::DiffCollection.new(GitalyClient::DiffStitcher.new(response), options) - end - - def commit_deltas(commit) - request = Gitaly::CommitDeltaRequest.new(commit_diff_request_params(commit)) - response = GitalyClient.call(@repository.storage, :diff, :commit_delta, request) - response.flat_map do |msg| - msg.deltas.map { |d| Gitlab::Git::Diff.new(d) } - end - end - - def tree_entry(ref, path, limit = nil) - request = Gitaly::TreeEntryRequest.new( - repository: @gitaly_repo, - revision: ref, - path: path.dup.force_encoding(Encoding::ASCII_8BIT), - limit: limit.to_i - ) - - response = GitalyClient.call(@repository.storage, :commit, :tree_entry, request) - entry = response.first - return unless entry.oid.present? - - if entry.type == :BLOB - rest_of_data = response.reduce("") { |memo, msg| memo << msg.data } - entry.data += rest_of_data - end - - entry - end - - def commit_count(ref) - request = Gitaly::CountCommitsRequest.new( - repository: @gitaly_repo, - revision: ref - ) - - GitalyClient.call(@repository.storage, :commit_service, :count_commits, request).count - end - - private - - def commit_diff_request_params(commit, options = {}) - parent_id = commit.parents[0]&.id || EMPTY_TREE_ID - - { - repository: @gitaly_repo, - left_commit_id: parent_id, - right_commit_id: commit.id, - paths: options.fetch(:paths, []) - } - end - end - end -end diff --git a/lib/gitlab/gitaly_client/commit_service.rb b/lib/gitlab/gitaly_client/commit_service.rb new file mode 100644 index 00000000000..470e3ac8779 --- /dev/null +++ b/lib/gitlab/gitaly_client/commit_service.rb @@ -0,0 +1,82 @@ +module Gitlab + module GitalyClient + class CommitService + # The ID of empty tree. + # See http://stackoverflow.com/a/40884093/1856239 and https://github.com/git/git/blob/3ad8b5bf26362ac67c9020bf8c30eee54a84f56d/cache.h#L1011-L1012 + EMPTY_TREE_ID = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'.freeze + + def initialize(repository) + @gitaly_repo = repository.gitaly_repository + @repository = repository + end + + def is_ancestor(ancestor_id, child_id) + request = Gitaly::CommitIsAncestorRequest.new( + repository: @gitaly_repo, + ancestor_id: ancestor_id, + child_id: child_id + ) + + GitalyClient.call(@repository.storage, :commit_service, :commit_is_ancestor, request).value + end + + def diff_from_parent(commit, options = {}) + request_params = commit_diff_request_params(commit, options) + request_params[:ignore_whitespace_change] = options.fetch(:ignore_whitespace_change, false) + request = Gitaly::CommitDiffRequest.new(request_params) + response = GitalyClient.call(@repository.storage, :diff_service, :commit_diff, request) + Gitlab::Git::DiffCollection.new(GitalyClient::DiffStitcher.new(response), options) + end + + def commit_deltas(commit) + request = Gitaly::CommitDeltaRequest.new(commit_diff_request_params(commit)) + response = GitalyClient.call(@repository.storage, :diff_service, :commit_delta, request) + response.flat_map do |msg| + msg.deltas.map { |d| Gitlab::Git::Diff.new(d) } + end + end + + def tree_entry(ref, path, limit = nil) + request = Gitaly::TreeEntryRequest.new( + repository: @gitaly_repo, + revision: ref, + path: path.dup.force_encoding(Encoding::ASCII_8BIT), + limit: limit.to_i + ) + + response = GitalyClient.call(@repository.storage, :commit_service, :tree_entry, request) + entry = response.first + return unless entry.oid.present? + + if entry.type == :BLOB + rest_of_data = response.reduce("") { |memo, msg| memo << msg.data } + entry.data += rest_of_data + end + + entry + end + + def commit_count(ref) + request = Gitaly::CountCommitsRequest.new( + repository: @gitaly_repo, + revision: ref + ) + + GitalyClient.call(@repository.storage, :commit_service, :count_commits, request).count + end + + private + + def commit_diff_request_params(commit, options = {}) + parent_id = commit.parents[0]&.id || EMPTY_TREE_ID + + { + repository: @gitaly_repo, + left_commit_id: parent_id, + right_commit_id: commit.id, + paths: options.fetch(:paths, []) + } + end + end + end +end diff --git a/lib/gitlab/gitaly_client/notification_service.rb b/lib/gitlab/gitaly_client/notification_service.rb new file mode 100644 index 00000000000..326e6f7dafc --- /dev/null +++ b/lib/gitlab/gitaly_client/notification_service.rb @@ -0,0 +1,20 @@ +module Gitlab + module GitalyClient + class NotificationService + # 'repository' is a Gitlab::Git::Repository + def initialize(repository) + @gitaly_repo = repository.gitaly_repository + @storage = repository.storage + end + + def post_receive + GitalyClient.call( + @storage, + :notification_service, + :post_receive, + Gitaly::PostReceiveRequest.new(repository: @gitaly_repo) + ) + end + end + end +end diff --git a/lib/gitlab/gitaly_client/notifications.rb b/lib/gitlab/gitaly_client/notifications.rb deleted file mode 100644 index 78ed433e6b8..00000000000 --- a/lib/gitlab/gitaly_client/notifications.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Gitlab - module GitalyClient - class Notifications - # 'repository' is a Gitlab::Git::Repository - def initialize(repository) - @gitaly_repo = repository.gitaly_repository - @storage = repository.storage - end - - def post_receive - GitalyClient.call( - @storage, - :notifications, - :post_receive, - Gitaly::PostReceiveRequest.new(repository: @gitaly_repo) - ) - end - end - end -end diff --git a/lib/gitlab/gitaly_client/ref.rb b/lib/gitlab/gitaly_client/ref.rb deleted file mode 100644 index 2c17b67d4b7..00000000000 --- a/lib/gitlab/gitaly_client/ref.rb +++ /dev/null @@ -1,110 +0,0 @@ -module Gitlab - module GitalyClient - class Ref - include Gitlab::EncodingHelper - - # 'repository' is a Gitlab::Git::Repository - def initialize(repository) - @repository = repository - @gitaly_repo = repository.gitaly_repository - @storage = repository.storage - end - - def default_branch_name - request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo) - response = GitalyClient.call(@storage, :ref, :find_default_branch_name, request) - Gitlab::Git.branch_name(response.name) - end - - def branch_names - request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo) - response = GitalyClient.call(@storage, :ref, :find_all_branch_names, request) - consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) } - end - - def tag_names - request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo) - response = GitalyClient.call(@storage, :ref, :find_all_tag_names, request) - consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) } - end - - def find_ref_name(commit_id, ref_prefix) - request = Gitaly::FindRefNameRequest.new( - repository: @gitaly_repo, - commit_id: commit_id, - prefix: ref_prefix - ) - encode!(GitalyClient.call(@storage, :ref, :find_ref_name, request).name.dup) - end - - def count_tag_names - tag_names.count - end - - def count_branch_names - branch_names.count - end - - def local_branches(sort_by: nil) - request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo) - request.sort_by = sort_by_param(sort_by) if sort_by - response = GitalyClient.call(@storage, :ref, :find_local_branches, request) - consume_branches_response(response) - end - - private - - def consume_refs_response(response) - response.flat_map { |message| message.names.map { |name| yield(name) } } - end - - def sort_by_param(sort_by) - sort_by = 'name' if sort_by == 'name_asc' - - enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym) - raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value - enum_value - end - - def consume_branches_response(response) - response.flat_map do |message| - message.branches.map do |gitaly_branch| - Gitlab::Git::Branch.new( - @repository, - encode!(gitaly_branch.name.dup), - gitaly_branch.commit_id, - commit_from_local_branches_response(gitaly_branch) - ) - end - end - end - - def commit_from_local_branches_response(response) - # Git messages have no encoding enforcements. However, in the UI we only - # handle UTF-8, so basically we cross our fingers that the message force - # encoded to UTF-8 is readable. - message = response.commit_subject.dup.force_encoding('UTF-8') - - # NOTE: For ease of parsing in Gitaly, we have only the subject of - # the commit and not the full message. This is ok, since all the - # code that uses `local_branches` only cares at most about the - # commit message. - # TODO: Once gitaly "takes over" Rugged consider separating the - # subject from the message to make it clearer when there's one - # available but not the other. - hash = { - id: response.commit_id, - message: message, - authored_date: Time.at(response.commit_author.date.seconds), - author_name: response.commit_author.name, - author_email: response.commit_author.email, - committed_date: Time.at(response.commit_committer.date.seconds), - committer_name: response.commit_committer.name, - committer_email: response.commit_committer.email - } - - Gitlab::Git::Commit.decorate(hash) - end - end - end -end diff --git a/lib/gitlab/gitaly_client/ref_service.rb b/lib/gitlab/gitaly_client/ref_service.rb new file mode 100644 index 00000000000..f541887843d --- /dev/null +++ b/lib/gitlab/gitaly_client/ref_service.rb @@ -0,0 +1,110 @@ +module Gitlab + module GitalyClient + class RefService + include Gitlab::EncodingHelper + + # 'repository' is a Gitlab::Git::Repository + def initialize(repository) + @repository = repository + @gitaly_repo = repository.gitaly_repository + @storage = repository.storage + end + + def default_branch_name + request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo) + response = GitalyClient.call(@storage, :ref_service, :find_default_branch_name, request) + Gitlab::Git.branch_name(response.name) + end + + def branch_names + request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo) + response = GitalyClient.call(@storage, :ref_service, :find_all_branch_names, request) + consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) } + end + + def tag_names + request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo) + response = GitalyClient.call(@storage, :ref_service, :find_all_tag_names, request) + consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) } + end + + def find_ref_name(commit_id, ref_prefix) + request = Gitaly::FindRefNameRequest.new( + repository: @gitaly_repo, + commit_id: commit_id, + prefix: ref_prefix + ) + encode!(GitalyClient.call(@storage, :ref_service, :find_ref_name, request).name.dup) + end + + def count_tag_names + tag_names.count + end + + def count_branch_names + branch_names.count + end + + def local_branches(sort_by: nil) + request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo) + request.sort_by = sort_by_param(sort_by) if sort_by + response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request) + consume_branches_response(response) + end + + private + + def consume_refs_response(response) + response.flat_map { |message| message.names.map { |name| yield(name) } } + end + + def sort_by_param(sort_by) + sort_by = 'name' if sort_by == 'name_asc' + + enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym) + raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value + enum_value + end + + def consume_branches_response(response) + response.flat_map do |message| + message.branches.map do |gitaly_branch| + Gitlab::Git::Branch.new( + @repository, + encode!(gitaly_branch.name.dup), + gitaly_branch.commit_id, + commit_from_local_branches_response(gitaly_branch) + ) + end + end + end + + def commit_from_local_branches_response(response) + # Git messages have no encoding enforcements. However, in the UI we only + # handle UTF-8, so basically we cross our fingers that the message force + # encoded to UTF-8 is readable. + message = response.commit_subject.dup.force_encoding('UTF-8') + + # NOTE: For ease of parsing in Gitaly, we have only the subject of + # the commit and not the full message. This is ok, since all the + # code that uses `local_branches` only cares at most about the + # commit message. + # TODO: Once gitaly "takes over" Rugged consider separating the + # subject from the message to make it clearer when there's one + # available but not the other. + hash = { + id: response.commit_id, + message: message, + authored_date: Time.at(response.commit_author.date.seconds), + author_name: response.commit_author.name, + author_email: response.commit_author.email, + committed_date: Time.at(response.commit_committer.date.seconds), + committer_name: response.commit_committer.name, + committer_email: response.commit_committer.email + } + + Gitlab::Git::Commit.decorate(hash) + end + end + end +end diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb index 3eeed6126a0..83d067b2c31 100644 --- a/spec/lib/gitlab/git/repository_spec.rb +++ b/spec/lib/gitlab/git/repository_spec.rb @@ -45,11 +45,11 @@ describe Gitlab::Git::Repository, seed_helper: true do end it 'gets the branch name from GitalyClient' do - expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:default_branch_name) + expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:default_branch_name) repository.root_ref end - it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :default_branch_name do + it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :default_branch_name do subject { repository.root_ref } end end @@ -132,11 +132,11 @@ describe Gitlab::Git::Repository, seed_helper: true do it { is_expected.not_to include("branch-from-space") } it 'gets the branch names from GitalyClient' do - expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:branch_names) + expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:branch_names) subject end - it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :branch_names + it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :branch_names end describe '#tag_names' do @@ -160,11 +160,11 @@ describe Gitlab::Git::Repository, seed_helper: true do it { is_expected.not_to include("v5.0.0") } it 'gets the tag names from GitalyClient' do - expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:tag_names) + expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:tag_names) subject end - it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :tag_names + it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :tag_names end shared_examples 'archive check' do |extenstion| @@ -368,7 +368,7 @@ describe Gitlab::Git::Repository, seed_helper: true do context 'when Gitaly commit_count feature is enabled' do it_behaves_like 'counting commits' - it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Commit, :commit_count do + it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::CommitService, :commit_count do subject { repository.commit_count('master') } end end @@ -1225,12 +1225,12 @@ describe Gitlab::Git::Repository, seed_helper: true do end it 'gets the branches from GitalyClient' do - expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:local_branches) + expect_any_instance_of(Gitlab::GitalyClient::RefService).to receive(:local_branches) .and_return([]) @repo.local_branches end - it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::Ref, :local_branches do + it_behaves_like 'wrapping gRPC errors', Gitlab::GitalyClient::RefService, :local_branches do subject { @repo.local_branches } end end diff --git a/spec/lib/gitlab/gitaly_client/commit_service_spec.rb b/spec/lib/gitlab/gitaly_client/commit_service_spec.rb new file mode 100644 index 00000000000..fee5bb45fe5 --- /dev/null +++ b/spec/lib/gitlab/gitaly_client/commit_service_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper' + +describe Gitlab::GitalyClient::CommitService do + let(:diff_stub) { double('Gitaly::DiffService::Stub') } + let(:project) { create(:project, :repository) } + let(:repository) { project.repository } + let(:repository_message) { repository.gitaly_repository } + let(:commit) { project.commit('913c66a37b4a45b9769037c55c2d238bd0942d2e') } + + describe '#diff_from_parent' do + context 'when a commit has a parent' do + it 'sends an RPC request with the parent ID as left commit' do + request = Gitaly::CommitDiffRequest.new( + repository: repository_message, + left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660', + right_commit_id: commit.id + ) + + expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash)) + + described_class.new(repository).diff_from_parent(commit) + end + end + + context 'when a commit does not have a parent' do + it 'sends an RPC request with empty tree ref as left commit' do + initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') + request = Gitaly::CommitDiffRequest.new( + repository: repository_message, + left_commit_id: '4b825dc642cb6eb9a060e54bf8d69288fbee4904', + right_commit_id: initial_commit.id + ) + + expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_diff).with(request, kind_of(Hash)) + + described_class.new(repository).diff_from_parent(initial_commit) + end + end + + it 'returns a Gitlab::Git::DiffCollection' do + ret = described_class.new(repository).diff_from_parent(commit) + + expect(ret).to be_kind_of(Gitlab::Git::DiffCollection) + end + + it 'passes options to Gitlab::Git::DiffCollection' do + options = { max_files: 31, max_lines: 13 } + + expect(Gitlab::Git::DiffCollection).to receive(:new).with(kind_of(Enumerable), options) + + described_class.new(repository).diff_from_parent(commit, options) + end + end + + describe '#commit_deltas' do + context 'when a commit has a parent' do + it 'sends an RPC request with the parent ID as left commit' do + request = Gitaly::CommitDeltaRequest.new( + repository: repository_message, + left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660', + right_commit_id: commit.id + ) + + expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([]) + + described_class.new(repository).commit_deltas(commit) + end + end + + context 'when a commit does not have a parent' do + it 'sends an RPC request with empty tree ref as left commit' do + initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') + request = Gitaly::CommitDeltaRequest.new( + repository: repository_message, + left_commit_id: '4b825dc642cb6eb9a060e54bf8d69288fbee4904', + right_commit_id: initial_commit.id + ) + + expect_any_instance_of(Gitaly::DiffService::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([]) + + described_class.new(repository).commit_deltas(initial_commit) + end + end + end +end diff --git a/spec/lib/gitlab/gitaly_client/commit_spec.rb b/spec/lib/gitlab/gitaly_client/commit_spec.rb deleted file mode 100644 index dff5b25c712..00000000000 --- a/spec/lib/gitlab/gitaly_client/commit_spec.rb +++ /dev/null @@ -1,85 +0,0 @@ -require 'spec_helper' - -describe Gitlab::GitalyClient::Commit do - let(:diff_stub) { double('Gitaly::Diff::Stub') } - let(:project) { create(:project, :repository) } - let(:repository) { project.repository } - let(:repository_message) { repository.gitaly_repository } - let(:commit) { project.commit('913c66a37b4a45b9769037c55c2d238bd0942d2e') } - - describe '#diff_from_parent' do - context 'when a commit has a parent' do - it 'sends an RPC request with the parent ID as left commit' do - request = Gitaly::CommitDiffRequest.new( - repository: repository_message, - left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660', - right_commit_id: commit.id - ) - - expect_any_instance_of(Gitaly::Diff::Stub).to receive(:commit_diff).with(request, kind_of(Hash)) - - described_class.new(repository).diff_from_parent(commit) - end - end - - context 'when a commit does not have a parent' do - it 'sends an RPC request with empty tree ref as left commit' do - initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') - request = Gitaly::CommitDiffRequest.new( - repository: repository_message, - left_commit_id: '4b825dc642cb6eb9a060e54bf8d69288fbee4904', - right_commit_id: initial_commit.id - ) - - expect_any_instance_of(Gitaly::Diff::Stub).to receive(:commit_diff).with(request, kind_of(Hash)) - - described_class.new(repository).diff_from_parent(initial_commit) - end - end - - it 'returns a Gitlab::Git::DiffCollection' do - ret = described_class.new(repository).diff_from_parent(commit) - - expect(ret).to be_kind_of(Gitlab::Git::DiffCollection) - end - - it 'passes options to Gitlab::Git::DiffCollection' do - options = { max_files: 31, max_lines: 13 } - - expect(Gitlab::Git::DiffCollection).to receive(:new).with(kind_of(Enumerable), options) - - described_class.new(repository).diff_from_parent(commit, options) - end - end - - describe '#commit_deltas' do - context 'when a commit has a parent' do - it 'sends an RPC request with the parent ID as left commit' do - request = Gitaly::CommitDeltaRequest.new( - repository: repository_message, - left_commit_id: 'cfe32cf61b73a0d5e9f13e774abde7ff789b1660', - right_commit_id: commit.id - ) - - expect_any_instance_of(Gitaly::Diff::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([]) - - described_class.new(repository).commit_deltas(commit) - end - end - - context 'when a commit does not have a parent' do - it 'sends an RPC request with empty tree ref as left commit' do - initial_commit = project.commit('1a0b36b3cdad1d2ee32457c102a8c0b7056fa863') - request = Gitaly::CommitDeltaRequest.new( - repository: repository_message, - left_commit_id: '4b825dc642cb6eb9a060e54bf8d69288fbee4904', - right_commit_id: initial_commit.id - ) - - expect_any_instance_of(Gitaly::Diff::Stub).to receive(:commit_delta).with(request, kind_of(Hash)).and_return([]) - - described_class.new(repository).commit_deltas(initial_commit) - end - end - end -end diff --git a/spec/lib/gitlab/gitaly_client/notification_service_spec.rb b/spec/lib/gitlab/gitaly_client/notification_service_spec.rb new file mode 100644 index 00000000000..d9597c4aa78 --- /dev/null +++ b/spec/lib/gitlab/gitaly_client/notification_service_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Gitlab::GitalyClient::NotificationService do + describe '#post_receive' do + let(:project) { create(:empty_project) } + let(:storage_name) { project.repository_storage } + let(:relative_path) { project.path_with_namespace + '.git' } + subject { described_class.new(project.repository) } + + it 'sends a post_receive message' do + expect_any_instance_of(Gitaly::NotificationService::Stub) + .to receive(:post_receive).with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) + + subject.post_receive + end + end +end diff --git a/spec/lib/gitlab/gitaly_client/notifications_spec.rb b/spec/lib/gitlab/gitaly_client/notifications_spec.rb deleted file mode 100644 index 7404ffe0f06..00000000000 --- a/spec/lib/gitlab/gitaly_client/notifications_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe Gitlab::GitalyClient::Notifications do - describe '#post_receive' do - let(:project) { create(:empty_project) } - let(:storage_name) { project.repository_storage } - let(:relative_path) { project.path_with_namespace + '.git' } - subject { described_class.new(project.repository) } - - it 'sends a post_receive message' do - expect_any_instance_of(Gitaly::Notifications::Stub) - .to receive(:post_receive).with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) - - subject.post_receive - end - end -end diff --git a/spec/lib/gitlab/gitaly_client/ref_service_spec.rb b/spec/lib/gitlab/gitaly_client/ref_service_spec.rb new file mode 100644 index 00000000000..1e8ed9d645b --- /dev/null +++ b/spec/lib/gitlab/gitaly_client/ref_service_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe Gitlab::GitalyClient::RefService do + let(:project) { create(:empty_project) } + let(:storage_name) { project.repository_storage } + let(:relative_path) { project.path_with_namespace + '.git' } + let(:client) { described_class.new(project.repository) } + + describe '#branch_names' do + it 'sends a find_all_branch_names message' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_all_branch_names) + .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) + .and_return([]) + + client.branch_names + end + end + + describe '#tag_names' do + it 'sends a find_all_tag_names message' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_all_tag_names) + .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) + .and_return([]) + + client.tag_names + end + end + + describe '#default_branch_name' do + it 'sends a find_default_branch_name message' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_default_branch_name) + .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) + .and_return(double(name: 'foo')) + + client.default_branch_name + end + end + + describe '#local_branches' do + it 'sends a find_local_branches message' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_local_branches) + .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) + .and_return([]) + + client.local_branches + end + + it 'parses and sends the sort parameter' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_local_branches) + .with(gitaly_request_with_params(sort_by: :UPDATED_DESC), kind_of(Hash)) + .and_return([]) + + client.local_branches(sort_by: 'updated_desc') + end + + it 'translates known mismatches on sort param values' do + expect_any_instance_of(Gitaly::RefService::Stub) + .to receive(:find_local_branches) + .with(gitaly_request_with_params(sort_by: :NAME), kind_of(Hash)) + .and_return([]) + + client.local_branches(sort_by: 'name_asc') + end + + it 'raises an argument error if an invalid sort_by parameter is passed' do + expect { client.local_branches(sort_by: 'invalid_sort') }.to raise_error(ArgumentError) + end + end + + describe '#find_ref_name', seed_helper: true do + let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } + let(:client) { described_class.new(repository) } + subject { client.find_ref_name(SeedRepo::Commit::ID, 'refs/heads/master') } + + it { is_expected.to be_utf8 } + it { is_expected.to eq('refs/heads/master') } + end +end diff --git a/spec/lib/gitlab/gitaly_client/ref_spec.rb b/spec/lib/gitlab/gitaly_client/ref_spec.rb deleted file mode 100644 index 7c090460764..00000000000 --- a/spec/lib/gitlab/gitaly_client/ref_spec.rb +++ /dev/null @@ -1,83 +0,0 @@ -require 'spec_helper' - -describe Gitlab::GitalyClient::Ref do - let(:project) { create(:empty_project) } - let(:storage_name) { project.repository_storage } - let(:relative_path) { project.path_with_namespace + '.git' } - let(:client) { described_class.new(project.repository) } - - describe '#branch_names' do - it 'sends a find_all_branch_names message' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_all_branch_names) - .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) - .and_return([]) - - client.branch_names - end - end - - describe '#tag_names' do - it 'sends a find_all_tag_names message' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_all_tag_names) - .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) - .and_return([]) - - client.tag_names - end - end - - describe '#default_branch_name' do - it 'sends a find_default_branch_name message' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_default_branch_name) - .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) - .and_return(double(name: 'foo')) - - client.default_branch_name - end - end - - describe '#local_branches' do - it 'sends a find_local_branches message' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_local_branches) - .with(gitaly_request_with_path(storage_name, relative_path), kind_of(Hash)) - .and_return([]) - - client.local_branches - end - - it 'parses and sends the sort parameter' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_local_branches) - .with(gitaly_request_with_params(sort_by: :UPDATED_DESC), kind_of(Hash)) - .and_return([]) - - client.local_branches(sort_by: 'updated_desc') - end - - it 'translates known mismatches on sort param values' do - expect_any_instance_of(Gitaly::Ref::Stub) - .to receive(:find_local_branches) - .with(gitaly_request_with_params(sort_by: :NAME), kind_of(Hash)) - .and_return([]) - - client.local_branches(sort_by: 'name_asc') - end - - it 'raises an argument error if an invalid sort_by parameter is passed' do - expect { client.local_branches(sort_by: 'invalid_sort') }.to raise_error(ArgumentError) - end - end - - describe '#find_ref_name', seed_helper: true do - let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } - let(:client) { described_class.new(repository) } - subject { client.find_ref_name(SeedRepo::Commit::ID, 'refs/heads/master') } - - it { is_expected.to be_utf8 } - it { is_expected.to eq('refs/heads/master') } - end -end diff --git a/spec/lib/gitlab/gitaly_client_spec.rb b/spec/lib/gitlab/gitaly_client_spec.rb index ce7b18b784a..558ddb3fbd6 100644 --- a/spec/lib/gitlab/gitaly_client_spec.rb +++ b/spec/lib/gitlab/gitaly_client_spec.rb @@ -16,9 +16,9 @@ describe Gitlab::GitalyClient, lib: true, skip_gitaly_mock: true do 'default' => { 'gitaly_address' => address } }) - expect(Gitaly::Commit::Stub).to receive(:new).with(address, any_args) + expect(Gitaly::CommitService::Stub).to receive(:new).with(address, any_args) - described_class.stub(:commit, 'default') + described_class.stub(:commit_service, 'default') end end @@ -31,9 +31,9 @@ describe Gitlab::GitalyClient, lib: true, skip_gitaly_mock: true do 'default' => { 'gitaly_address' => prefixed_address } }) - expect(Gitaly::Commit::Stub).to receive(:new).with(address, any_args) + expect(Gitaly::CommitService::Stub).to receive(:new).with(address, any_args) - described_class.stub(:commit, 'default') + described_class.stub(:commit_service, 'default') end end end diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index beaaf346283..cab3089c6b1 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -594,10 +594,10 @@ describe API::Internal do # end # # it "calls the Gitaly client with the project's repository" do - # expect(Gitlab::GitalyClient::Notifications). + # expect(Gitlab::GitalyClient::NotificationService). # to receive(:new).with(gitlab_git_repository_with(path: project.repository.path)). # and_call_original - # expect_any_instance_of(Gitlab::GitalyClient::Notifications). + # expect_any_instance_of(Gitlab::GitalyClient::NotificationService). # to receive(:post_receive) # # post api("/internal/notify_post_receive"), valid_params @@ -606,10 +606,10 @@ describe API::Internal do # end # # it "calls the Gitaly client with the wiki's repository if it's a wiki" do - # expect(Gitlab::GitalyClient::Notifications). + # expect(Gitlab::GitalyClient::NotificationService). # to receive(:new).with(gitlab_git_repository_with(path: project.wiki.repository.path)). # and_call_original - # expect_any_instance_of(Gitlab::GitalyClient::Notifications). + # expect_any_instance_of(Gitlab::GitalyClient::NotificationService). # to receive(:post_receive) # # post api("/internal/notify_post_receive"), valid_wiki_params @@ -618,7 +618,7 @@ describe API::Internal do # end # # it "returns 500 if the gitaly call fails" do - # expect_any_instance_of(Gitlab::GitalyClient::Notifications). + # expect_any_instance_of(Gitlab::GitalyClient::NotificationService). # to receive(:post_receive).and_raise(GRPC::Unavailable) # # post api("/internal/notify_post_receive"), valid_params @@ -636,10 +636,10 @@ describe API::Internal do # end # # it "calls the Gitaly client with the project's repository" do - # expect(Gitlab::GitalyClient::Notifications). + # expect(Gitlab::GitalyClient::NotificationService). # to receive(:new).with(gitlab_git_repository_with(path: project.repository.path)). # and_call_original - # expect_any_instance_of(Gitlab::GitalyClient::Notifications). + # expect_any_instance_of(Gitlab::GitalyClient::NotificationService). # to receive(:post_receive) # # post api("/internal/notify_post_receive"), valid_params @@ -648,10 +648,10 @@ describe API::Internal do # end # # it "calls the Gitaly client with the wiki's repository if it's a wiki" do - # expect(Gitlab::GitalyClient::Notifications). + # expect(Gitlab::GitalyClient::NotificationService). # to receive(:new).with(gitlab_git_repository_with(path: project.wiki.repository.path)). # and_call_original - # expect_any_instance_of(Gitlab::GitalyClient::Notifications). + # expect_any_instance_of(Gitlab::GitalyClient::NotificationService). # to receive(:post_receive) # # post api("/internal/notify_post_receive"), valid_wiki_params -- cgit v1.2.3