Commit 0efa7e24 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'gitaly-fetch-source-branch' into 'master'

Add FetchSourceBranch Gitaly call

Closes gitaly#758

See merge request gitlab-org/gitlab-ce!15498
parents 6369db01 571f1dda
...@@ -1058,12 +1058,11 @@ module Gitlab ...@@ -1058,12 +1058,11 @@ module Gitlab
end end
def fetch_source_branch!(source_repository, source_branch, local_ref) def fetch_source_branch!(source_repository, source_branch, local_ref)
with_repo_branch_commit(source_repository, source_branch) do |commit| Gitlab::GitalyClient.migrate(:fetch_source_branch) do |is_enabled|
if commit if is_enabled
write_ref(local_ref, commit.sha) gitaly_repository_client.fetch_source_branch(source_repository, source_branch, local_ref)
true
else else
false rugged_fetch_source_branch(source_repository, source_branch, local_ref)
end end
end end
end end
...@@ -1216,6 +1215,17 @@ module Gitlab ...@@ -1216,6 +1215,17 @@ module Gitlab
private private
def rugged_fetch_source_branch(source_repository, source_branch, local_ref)
with_repo_branch_commit(source_repository, source_branch) do |commit|
if commit
write_ref(local_ref, commit.sha)
true
else
false
end
end
end
# Gitaly note: JV: Trying to get rid of the 'filter' option so we can implement this with 'git'. # Gitaly note: JV: Trying to get rid of the 'filter' option so we can implement this with 'git'.
def branches_filter(filter: nil, sort_by: nil) def branches_filter(filter: nil, sort_by: nil)
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37464 # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37464
......
...@@ -75,6 +75,10 @@ module Gitlab ...@@ -75,6 +75,10 @@ module Gitlab
address address
end end
def self.address_metadata(storage)
Base64.strict_encode64(JSON.dump({ storage => { 'address' => address(storage), 'token' => token(storage) } }))
end
# All Gitaly RPC call sites should use GitalyClient.call. This method # All Gitaly RPC call sites should use GitalyClient.call. This method
# makes sure that per-request authentication headers are set. # makes sure that per-request authentication headers are set.
# #
...@@ -89,18 +93,19 @@ module Gitlab ...@@ -89,18 +93,19 @@ module Gitlab
# kwargs.merge(deadline: Time.now + 10) # kwargs.merge(deadline: Time.now + 10)
# end # end
# #
def self.call(storage, service, rpc, request) def self.call(storage, service, rpc, request, remote_storage: nil)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
enforce_gitaly_request_limits(:call) enforce_gitaly_request_limits(:call)
kwargs = request_kwargs(storage) kwargs = request_kwargs(storage, remote_storage: remote_storage)
kwargs = yield(kwargs) if block_given? kwargs = yield(kwargs) if block_given?
stub(service, storage).__send__(rpc, request, kwargs) # rubocop:disable GitlabSecurity/PublicSend stub(service, storage).__send__(rpc, request, kwargs) # rubocop:disable GitlabSecurity/PublicSend
ensure ensure
self.query_time += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start self.query_time += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
end end
def self.request_kwargs(storage) def self.request_kwargs(storage, remote_storage: nil)
encoded_token = Base64.strict_encode64(token(storage).to_s) encoded_token = Base64.strict_encode64(token(storage).to_s)
metadata = { metadata = {
'authorization' => "Bearer #{encoded_token}", 'authorization' => "Bearer #{encoded_token}",
...@@ -110,6 +115,7 @@ module Gitlab ...@@ -110,6 +115,7 @@ module Gitlab
feature_stack = Thread.current[:gitaly_feature_stack] feature_stack = Thread.current[:gitaly_feature_stack]
feature = feature_stack && feature_stack[0] feature = feature_stack && feature_stack[0]
metadata['call_site'] = feature.to_s if feature metadata['call_site'] = feature.to_s if feature
metadata['gitaly-servers'] = address_metadata(remote_storage) if remote_storage
{ metadata: metadata } { metadata: metadata }
end end
......
...@@ -65,6 +65,25 @@ module Gitlab ...@@ -65,6 +65,25 @@ module Gitlab
response.value response.value
end end
def fetch_source_branch(source_repository, source_branch, local_ref)
request = Gitaly::FetchSourceBranchRequest.new(
repository: @gitaly_repo,
source_repository: source_repository.gitaly_repository,
source_branch: source_branch.b,
target_ref: local_ref.b
)
response = GitalyClient.call(
@storage,
:repository_service,
:fetch_source_branch,
request,
remote_storage: source_repository.storage
)
response.result
end
end end
end end
end end
...@@ -78,6 +78,8 @@ namespace :gitlab do ...@@ -78,6 +78,8 @@ namespace :gitlab do
config[:auth] = { token: 'secret' } if Rails.env.test? config[:auth] = { token: 'secret' } if Rails.env.test?
config[:'gitaly-ruby'] = { dir: File.join(Dir.pwd, 'ruby') } if gitaly_ruby config[:'gitaly-ruby'] = { dir: File.join(Dir.pwd, 'ruby') } if gitaly_ruby
config[:'gitlab-shell'] = { dir: Gitlab.config.gitlab_shell.path } config[:'gitlab-shell'] = { dir: Gitlab.config.gitlab_shell.path }
config[:bin_dir] = Gitlab.config.gitaly.client_path
TOML.dump(config) TOML.dump(config)
end end
......
...@@ -628,7 +628,7 @@ describe API::MergeRequests do ...@@ -628,7 +628,7 @@ describe API::MergeRequests do
context 'forked projects' do context 'forked projects' do
let!(:user2) { create(:user) } let!(:user2) { create(:user) }
let!(:forked_project) { fork_project(project, user2) } let!(:forked_project) { fork_project(project, user2, repository: true) }
let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) } let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) }
before do before do
......
...@@ -314,7 +314,7 @@ describe API::MergeRequests do ...@@ -314,7 +314,7 @@ describe API::MergeRequests do
context 'forked projects' do context 'forked projects' do
let!(:user2) { create(:user) } let!(:user2) { create(:user) }
let!(:forked_project) { fork_project(project, user2) } let!(:forked_project) { fork_project(project, user2, repository: true) }
let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) } let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) }
before do before do
......
...@@ -112,6 +112,7 @@ describe 'gitlab:gitaly namespace rake task' do ...@@ -112,6 +112,7 @@ describe 'gitlab:gitaly namespace rake task' do
expected_output = <<~TOML expected_output = <<~TOML
# Gitaly storage configuration generated from #{Gitlab.config.source} on #{Time.current.to_s(:long)} # Gitaly storage configuration generated from #{Gitlab.config.source} on #{Time.current.to_s(:long)}
# This is in TOML format suitable for use in Gitaly's config.toml file. # This is in TOML format suitable for use in Gitaly's config.toml file.
bin_dir = "tmp/tests/gitaly"
socket_path = "/path/to/my.socket" socket_path = "/path/to/my.socket"
[gitlab-shell] [gitlab-shell]
dir = "#{Gitlab.config.gitlab_shell.path}" dir = "#{Gitlab.config.gitlab_shell.path}"
......
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