Commit b83ea43d authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch '351865-ending-forward-slashes' into 'master'

Update the registry gitlab v1 API client

See merge request gitlab-org/gitlab!79975
parents 33c3e20e 76da2102
...@@ -57,7 +57,7 @@ module ContainerRegistry ...@@ -57,7 +57,7 @@ module ContainerRegistry
def faraday(timeout_enabled: true) def faraday(timeout_enabled: true)
@faraday ||= faraday_base(timeout_enabled: timeout_enabled) do |conn| @faraday ||= faraday_base(timeout_enabled: timeout_enabled) do |conn|
initialize_connection(conn, @options, &method(:accept_manifest)) initialize_connection(conn, @options, &method(:configure_connection))
end end
end end
...@@ -105,7 +105,7 @@ module ContainerRegistry ...@@ -105,7 +105,7 @@ module ContainerRegistry
faraday_redirect.get(uri) faraday_redirect.get(uri)
end end
def accept_manifest(conn) def configure_connection(conn)
conn.headers['Accept'] = ACCEPTED_TYPES conn.headers['Accept'] = ACCEPTED_TYPES
conn.response :json, content_type: 'application/json' conn.response :json, content_type: 'application/json'
......
...@@ -4,6 +4,8 @@ module ContainerRegistry ...@@ -4,6 +4,8 @@ module ContainerRegistry
class GitlabApiClient < BaseClient class GitlabApiClient < BaseClient
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
JSON_TYPE = 'application/json'
IMPORT_RESPONSES = { IMPORT_RESPONSES = {
200 => :already_imported, 200 => :already_imported,
202 => :ok, 202 => :ok,
...@@ -46,9 +48,20 @@ module ContainerRegistry ...@@ -46,9 +48,20 @@ module ContainerRegistry
private private
def start_import_for(path, pre:) def start_import_for(path, pre:)
faraday.put("/gitlab/v1/import/#{path}") do |req| faraday.put(import_url_for(path)) do |req|
req.params['pre'] = pre.to_s req.params['pre'] = pre.to_s
end end
end end
def import_url_for(path)
"/gitlab/v1/import/#{path}/"
end
# overrides the default configuration
def configure_connection(conn)
conn.headers['Accept'] = [JSON_TYPE]
conn.response :json, content_type: JSON_TYPE
end
end end
end end
...@@ -7,6 +7,8 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -7,6 +7,8 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
include_context 'container registry client' include_context 'container registry client'
let(:path) { 'namespace/path/to/repository' }
describe '#supports_gitlab_api?' do describe '#supports_gitlab_api?' do
subject { client.supports_gitlab_api? } subject { client.supports_gitlab_api? }
...@@ -30,9 +32,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -30,9 +32,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
it 'returns the expected result' do it 'returns the expected result' do
if expect_registry_to_be_pinged if expect_registry_to_be_pinged
expect_next_instance_of(Faraday::Connection) do |connection| expect(Faraday::Connection).to receive(:new).and_call_original
expect(connection).to receive(:run_request).and_call_original
end
else else
expect(Faraday::Connection).not_to receive(:new) expect(Faraday::Connection).not_to receive(:new)
end end
...@@ -54,9 +54,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -54,9 +54,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end end
describe '#pre_import_repository' do describe '#pre_import_repository' do
let(:path) { 'namespace/path/to/repository' } subject { client.pre_import_repository(path) }
subject { client.pre_import_repository('namespace/path/to/repository') }
where(:status_code, :expected_result) do where(:status_code, :expected_result) do
200 | :already_imported 200 | :already_imported
...@@ -80,9 +78,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -80,9 +78,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end end
describe '#pre_import_repository' do describe '#pre_import_repository' do
let(:path) { 'namespace/path/to/repository' } subject { client.import_repository(path) }
subject { client.import_repository('namespace/path/to/repository') }
where(:status_code, :expected_result) do where(:status_code, :expected_result) do
200 | :already_imported 200 | :already_imported
...@@ -129,9 +125,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -129,9 +125,7 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
it 'returns the expected result' do it 'returns the expected result' do
if expect_registry_to_be_pinged if expect_registry_to_be_pinged
expect_next_instance_of(Faraday::Connection) do |connection| expect(Faraday::Connection).to receive(:new).and_call_original
expect(connection).to receive(:run_request).and_call_original
end
else else
expect(Faraday::Connection).not_to receive(:new) expect(Faraday::Connection).not_to receive(:new)
end end
...@@ -166,13 +160,15 @@ RSpec.describe ContainerRegistry::GitlabApiClient do ...@@ -166,13 +160,15 @@ RSpec.describe ContainerRegistry::GitlabApiClient do
end end
def stub_pre_import(path, status_code, pre:) def stub_pre_import(path, status_code, pre:)
stub_request(:put, "#{registry_api_url}/gitlab/v1/import/#{path}?pre=#{pre}") stub_request(:put, "#{registry_api_url}/gitlab/v1/import/#{path}/?pre=#{pre}")
.with(headers: { 'Accept' => described_class::JSON_TYPE })
.to_return(status: status_code, body: '') .to_return(status: status_code, body: '')
end end
def stub_registry_gitlab_api_support(supported = true) def stub_registry_gitlab_api_support(supported = true)
status_code = supported ? 200 : 404 status_code = supported ? 200 : 404
stub_request(:get, "#{registry_api_url}/gitlab/v1/") stub_request(:get, "#{registry_api_url}/gitlab/v1/")
.with(headers: { 'Accept' => described_class::JSON_TYPE })
.to_return(status: status_code, body: '') .to_return(status: status_code, body: '')
end end
end end
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