2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-20 15:17:28 -05:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
class RemoteService
|
2018-09-07 00:46:12 -04:00
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2018-01-17 18:25:16 -05:00
|
|
|
MAX_MSG_SIZE = 128.kilobytes.freeze
|
|
|
|
|
2018-03-27 05:48:11 -04:00
|
|
|
def self.exists?(remote_url)
|
2020-07-07 11:08:49 -04:00
|
|
|
storage = GitalyClient.random_storage
|
2018-03-27 05:48:11 -04:00
|
|
|
|
2020-07-07 11:08:49 -04:00
|
|
|
request = Gitaly::FindRemoteRepositoryRequest.new(remote: remote_url, storage_name: storage)
|
|
|
|
|
|
|
|
response = GitalyClient.call(storage,
|
2018-03-27 05:48:11 -04:00
|
|
|
:remote_service,
|
|
|
|
:find_remote_repository, request,
|
|
|
|
timeout: GitalyClient.medium_timeout)
|
|
|
|
|
|
|
|
response.exists
|
|
|
|
end
|
|
|
|
|
2017-12-20 15:17:28 -05:00
|
|
|
def initialize(repository)
|
|
|
|
@repository = repository
|
|
|
|
@gitaly_repo = repository.gitaly_repository
|
|
|
|
@storage = repository.storage
|
|
|
|
end
|
|
|
|
|
2021-07-29 08:08:55 -04:00
|
|
|
def find_remote_root_ref(remote_url, authorization)
|
2021-05-26 14:10:52 -04:00
|
|
|
request = Gitaly::FindRemoteRootRefRequest.new(repository: @gitaly_repo,
|
|
|
|
remote_url: remote_url,
|
|
|
|
http_authorization_header: authorization)
|
2018-09-04 16:00:36 -04:00
|
|
|
|
|
|
|
response = GitalyClient.call(@storage, :remote_service,
|
2019-09-19 14:06:18 -04:00
|
|
|
:find_remote_root_ref, request, timeout: GitalyClient.medium_timeout)
|
2018-09-04 16:00:36 -04:00
|
|
|
|
2018-09-07 00:46:12 -04:00
|
|
|
encode_utf8(response.ref)
|
2018-09-04 16:00:36 -04:00
|
|
|
end
|
|
|
|
|
2021-08-12 08:11:05 -04:00
|
|
|
def update_remote_mirror(remote_url, only_branches_matching, ssh_key: nil, known_hosts: nil, keep_divergent_refs: false)
|
2018-01-17 18:25:16 -05:00
|
|
|
req_enum = Enumerator.new do |y|
|
2018-11-12 05:52:48 -05:00
|
|
|
first_request = Gitaly::UpdateRemoteMirrorRequest.new(
|
2021-06-16 11:10:08 -04:00
|
|
|
repository: @gitaly_repo
|
2018-01-17 18:25:16 -05:00
|
|
|
)
|
|
|
|
|
2021-08-12 08:11:05 -04:00
|
|
|
first_request.remote = Gitaly::UpdateRemoteMirrorRequest::Remote.new(url: remote_url)
|
2018-11-12 05:52:48 -05:00
|
|
|
first_request.ssh_key = ssh_key if ssh_key.present?
|
|
|
|
first_request.known_hosts = known_hosts if known_hosts.present?
|
2020-04-02 11:08:01 -04:00
|
|
|
first_request.keep_divergent_refs = keep_divergent_refs
|
2018-11-12 05:52:48 -05:00
|
|
|
|
|
|
|
y.yield(first_request)
|
|
|
|
|
2018-01-17 18:25:16 -05:00
|
|
|
current_size = 0
|
|
|
|
|
|
|
|
slices = only_branches_matching.slice_before do |branch_name|
|
|
|
|
current_size += branch_name.bytesize
|
|
|
|
|
|
|
|
next false if current_size < MAX_MSG_SIZE
|
|
|
|
|
|
|
|
current_size = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
slices.each do |slice|
|
|
|
|
y.yield Gitaly::UpdateRemoteMirrorRequest.new(only_branches_matching: slice)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
GitalyClient.call(@storage, :remote_service, :update_remote_mirror, req_enum, timeout: GitalyClient.long_timeout)
|
2018-01-17 18:25:16 -05:00
|
|
|
end
|
2017-12-20 15:17:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|