Commit fd822643 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'use-gitaly-find-ref-name' into 'master'

Use Gitaly for Environment#first_deployment_for

See merge request !10294
parents c3af43c3 70982bb4
...@@ -6,6 +6,8 @@ class Repository ...@@ -6,6 +6,8 @@ class Repository
attr_accessor :path_with_namespace, :project attr_accessor :path_with_namespace, :project
delegate :ref_name_for_sha, to: :raw_repository
CommitError = Class.new(StandardError) CommitError = Class.new(StandardError)
CreateTreeError = Class.new(StandardError) CreateTreeError = Class.new(StandardError)
...@@ -700,14 +702,6 @@ class Repository ...@@ -700,14 +702,6 @@ class Repository
end end
end end
def ref_name_for_sha(ref_path, sha)
args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{ref_path} --contains #{sha})
# Not found -> ["", 0]
# Found -> ["b8d95eb4969eefacb0a58f6a28f6803f8070e7b9 commit\trefs/environments/production/77\n", 0]
Gitlab::Popen.popen(args, path_to_repo).first.split.last
end
def refs_contains_sha(ref_type, sha) def refs_contains_sha(ref_type, sha)
args = %W(#{Gitlab.config.git.bin_path} #{ref_type} --contains #{sha}) args = %W(#{Gitlab.config.git.bin_path} #{ref_type} --contains #{sha})
names = Gitlab::Popen.popen(args, path_to_repo).first names = Gitlab::Popen.popen(args, path_to_repo).first
......
...@@ -452,6 +452,21 @@ module Gitlab ...@@ -452,6 +452,21 @@ module Gitlab
Gitlab::Git::DiffCollection.new(diff_patches(from, to, options, *paths), options) Gitlab::Git::DiffCollection.new(diff_patches(from, to, options, *paths), options)
end end
# Returns a RefName for a given SHA
def ref_name_for_sha(ref_path, sha)
Gitlab::GitalyClient.migrate(:find_ref_name) do |is_enabled|
if is_enabled
gitaly_ref_client.find_ref_name(sha, ref_path)
else
args = %W(#{Gitlab.config.git.bin_path} for-each-ref --count=1 #{ref_path} --contains #{sha})
# Not found -> ["", 0]
# Found -> ["b8d95eb4969eefacb0a58f6a28f6803f8070e7b9 commit\trefs/environments/production/77\n", 0]
Gitlab::Popen.popen(args, @path).first.split.last
end
end
end
# Returns commits collection # Returns commits collection
# #
# Ex. # Ex.
......
...@@ -23,6 +23,16 @@ module Gitlab ...@@ -23,6 +23,16 @@ module Gitlab
consume_refs_response(stub.find_all_tag_names(request), prefix: 'refs/tags/') consume_refs_response(stub.find_all_tag_names(request), prefix: 'refs/tags/')
end end
def find_ref_name(commit_id, ref_prefix)
request = Gitaly::FindRefNameRequest.new(
repository: @repository,
commit_id: commit_id,
prefix: ref_prefix
)
stub.find_ref_name(request).name
end
private private
def consume_refs_response(response, prefix:) def consume_refs_response(response, prefix:)
......
...@@ -100,6 +100,7 @@ describe Environment, models: true do ...@@ -100,6 +100,7 @@ describe Environment, models: true do
let(:head_commit) { project.commit } let(:head_commit) { project.commit }
let(:commit) { project.commit.parent } let(:commit) { project.commit.parent }
context 'Gitaly find_ref_name feature disabled' do
it 'returns deployment id for the environment' do it 'returns deployment id for the environment' do
expect(environment.first_deployment_for(commit)).to eq deployment1 expect(environment.first_deployment_for(commit)).to eq deployment1
end end
...@@ -109,6 +110,19 @@ describe Environment, models: true do ...@@ -109,6 +110,19 @@ describe Environment, models: true do
end end
end end
context 'Gitaly find_ref_name feature enabled' do
before do
allow(Gitlab::GitalyClient).to receive(:feature_enabled?).with(:find_ref_name).and_return(true)
end
it 'calls GitalyClient' do
expect_any_instance_of(Gitlab::GitalyClient::Ref).to receive(:find_ref_name)
environment.first_deployment_for(commit)
end
end
end
describe '#environment_type' do describe '#environment_type' do
subject { environment.environment_type } subject { environment.environment_type }
......
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