Class: ContainerRegistry::GitlabApiClient
- Inherits:
-
BaseClient
- Object
- BaseClient
- ContainerRegistry::GitlabApiClient
show all
- Includes:
- Gitlab::Utils::StrongMemoize
- Defined in:
- lib/container_registry/gitlab_api_client.rb
Constant Summary
collapse
- JSON_TYPE =
'application/json'
'status'
- IMPORT_RESPONSES =
{
200 => :already_imported,
202 => :ok,
400 => :bad_request,
401 => :unauthorized,
404 => :not_found,
409 => :already_being_imported,
424 => :pre_import_failed,
425 => :already_being_imported,
429 => :too_many_imports
}.freeze
- REGISTRY_GITLAB_V1_API_FEATURE =
'gitlab_v1_api'
- MAX_TAGS_PAGE_SIZE =
1000
- MAX_REPOSITORIES_PAGE_SIZE =
1000
- PAGE_SIZE =
1
- UnsuccessfulResponseError =
Class.new(StandardError)
Constants inherited
from BaseClient
BaseClient::ACCEPTED_TYPES, BaseClient::ACCEPTED_TYPES_RAW, BaseClient::CONTAINER_IMAGE_V1_TYPE, BaseClient::DOCKER_DISTRIBUTION_MANIFEST_LIST_V2_TYPE, BaseClient::DOCKER_DISTRIBUTION_MANIFEST_V2_TYPE, BaseClient::ERROR_CALLBACK_OPTIONS, BaseClient::OCI_DISTRIBUTION_INDEX_TYPE, BaseClient::OCI_MANIFEST_V1_TYPE, BaseClient::RETRY_EXCEPTIONS, BaseClient::RETRY_OPTIONS
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseClient
#initialize
Class Method Details
.deduplicated_size(path) ⇒ Object
36
37
38
39
40
|
# File 'lib/container_registry/gitlab_api_client.rb', line 36
def self.deduplicated_size(path)
with_dummy_client(token_config: { type: :nested_repositories_token, path: path&.downcase }) do |client|
client.repository_details(path&.downcase, sizing: :self_with_descendants)['size_bytes']
end
end
|
.each_sub_repositories_with_tag_page(path:, page_size: 100, &block) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/container_registry/gitlab_api_client.rb', line 57
def self.each_sub_repositories_with_tag_page(path:, page_size: 100, &block)
raise ArgumentError, 'block not given' unless block
next_page_uri = URI('')
page_count = 0
with_dummy_client(token_config: { type: :nested_repositories_token, path: path&.downcase }) do |client|
while next_page_uri
last = Rack::Utils.parse_nested_query(next_page_uri.query)['last']
current_page = client.sub_repositories_with_tag(path&.downcase, page_size: page_size, last: last)
if current_page&.key?(:response_body)
yield (current_page[:response_body] || [])
next_page_uri = current_page.dig(:pagination, :next, :uri)
else
next_page_uri = nil
end
page_count += 1
raise 'too many pages requested' if page_count >= MAX_REPOSITORIES_PAGE_SIZE
end
end
end
|
.one_project_with_container_registry_tag(path) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/container_registry/gitlab_api_client.rb', line 42
def self.one_project_with_container_registry_tag(path)
with_dummy_client(token_config: { type: :nested_repositories_token, path: path&.downcase }) do |client|
page = client.sub_repositories_with_tag(path&.downcase, page_size: PAGE_SIZE)
details = page[:response_body]&.first
break unless details
path = ContainerRegistry::Path.new(details["path"])
break unless path.valid?
ContainerRepository.find_by_path(path)&.project
end
end
|
.supports_gitlab_api? ⇒ Boolean
30
31
32
33
34
|
# File 'lib/container_registry/gitlab_api_client.rb', line 30
def self.supports_gitlab_api?
with_dummy_client(return_value_if_disabled: false) do |client|
client.supports_gitlab_api?
end
end
|
Instance Method Details
#cancel_repository_import(path, force: false) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/container_registry/gitlab_api_client.rb', line 110
def cancel_repository_import(path, force: false)
response = with_import_token_faraday do |faraday_client|
faraday_client.delete(import_url_for(path)) do |req|
req.params['force'] = true if force
end
end
status = IMPORT_RESPONSES.fetch(response.status, :error)
actual_state = response.body[CANCEL_RESPONSE_STATUS_HEADER]
{ status: status, migration_state: actual_state }
end
|
#import_repository(path) ⇒ Object
104
105
106
107
|
# File 'lib/container_registry/gitlab_api_client.rb', line 104
def import_repository(path)
response = start_import_for(path, pre: false)
IMPORT_RESPONSES.fetch(response.status, :error)
end
|
#import_status(path) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/container_registry/gitlab_api_client.rb', line 124
def import_status(path)
with_import_token_faraday do |faraday_client|
response = faraday_client.get(import_url_for(path))
break 'pre_import_failed' unless response.success?
body_hash = response_body(response)
body_hash&.fetch('status') || 'error'
end
end
|
#pre_import_repository(path) ⇒ Object
98
99
100
101
|
# File 'lib/container_registry/gitlab_api_client.rb', line 98
def pre_import_repository(path)
response = start_import_for(path, pre: true)
IMPORT_RESPONSES.fetch(response.status, :error)
end
|
#repository_details(path, sizing: nil) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/container_registry/gitlab_api_client.rb', line 138
def repository_details(path, sizing: nil)
with_token_faraday do |faraday_client|
req = faraday_client.get("/gitlab/v1/repositories/#{path}/") do |req|
req.params['size'] = sizing if sizing
end
break {} unless req.success?
response_body(req)
end
end
|
#sub_repositories_with_tag(path, page_size: 100, last: nil) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/container_registry/gitlab_api_client.rb', line 184
def sub_repositories_with_tag(path, page_size: 100, last: nil)
limited_page_size = [page_size, MAX_REPOSITORIES_PAGE_SIZE].min
with_token_faraday do |faraday_client|
url = "/gitlab/v1/repository-paths/#{path}/repositories/list/"
response = faraday_client.get(url) do |req|
req.params['n'] = limited_page_size
req.params['last'] = last if last
end
unless response.success?
Gitlab::ErrorTracking.log_exception(
UnsuccessfulResponseError.new,
class: self.class.name,
url: url,
status_code: response.status
)
break {}
end
link_parser = Gitlab::Utils::LinkHeaderParser.new(response.['link'])
{
pagination: link_parser.parse,
response_body: response_body(response)
}
end
end
|
#supports_gitlab_api? ⇒ Boolean
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/container_registry/gitlab_api_client.rb', line 85
def supports_gitlab_api?
strong_memoize(:supports_gitlab_api) do
registry_features = Gitlab::CurrentSettings.container_registry_features || []
next true if ::Gitlab.com_except_jh? && registry_features.include?(REGISTRY_GITLAB_V1_API_FEATURE)
with_token_faraday do |faraday_client|
response = faraday_client.get('/gitlab/v1/')
response.success? || response.status == 401
end
end
end
|
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/container_registry/gitlab_api_client.rb', line 151
def tags(path, page_size: 100, last: nil, before: nil, name: nil, sort: nil)
limited_page_size = [page_size, MAX_TAGS_PAGE_SIZE].min
with_token_faraday do |faraday_client|
url = "/gitlab/v1/repositories/#{path}/tags/list/"
response = faraday_client.get(url) do |req|
req.params['n'] = limited_page_size
req.params['last'] = last if last
req.params['before'] = before if before
req.params['name'] = name if name
req.params['sort'] = sort if sort
end
unless response.success?
Gitlab::ErrorTracking.log_exception(
UnsuccessfulResponseError.new,
class: self.class.name,
url: url,
status_code: response.status
)
break {}
end
link_parser = Gitlab::Utils::LinkHeaderParser.new(response.['link'])
{
pagination: link_parser.parse,
response_body: response_body(response)
}
end
end
|