debian-mirror-gitlab/lib/gitlab/harbor/client.rb

74 lines
2.2 KiB
Ruby
Raw Normal View History

2022-05-07 20:08:51 +05:30
# frozen_string_literal: true
module Gitlab
module Harbor
class Client
Error = Class.new(StandardError)
ConfigError = Class.new(Error)
attr_reader :integration
def initialize(integration)
raise ConfigError, 'Please check your integration configuration.' unless integration
@integration = integration
end
2023-06-20 00:43:36 +05:30
def check_project_availability
options = { headers: headers.merge!('Accept': 'application/json') }
response = Gitlab::HTTP.head(url("projects?project_name=#{integration.project_name}"), options)
2022-05-07 20:08:51 +05:30
{ success: response.success? }
end
2022-08-13 15:12:31 +05:30
def get_repositories(params)
get(url("projects/#{integration.project_name}/repositories"), params)
end
def get_artifacts(params)
repository_name = params.delete(:repository_name)
get(url("projects/#{integration.project_name}/repositories/#{repository_name}/artifacts"), params)
end
def get_tags(params)
repository_name = params.delete(:repository_name)
artifact_name = params.delete(:artifact_name)
get(
url("projects/#{integration.project_name}/repositories/#{repository_name}/artifacts/#{artifact_name}/tags"),
params
)
end
2022-05-07 20:08:51 +05:30
private
2022-08-13 15:12:31 +05:30
def get(path, params = {})
options = { headers: headers, query: params }
response = Gitlab::HTTP.get(path, options)
raise Gitlab::Harbor::Client::Error, 'request error' unless response.success?
{
body: Gitlab::Json.parse(response.body),
total_count: response.headers['x-total-count'].to_i
}
rescue JSON::ParserError
raise Gitlab::Harbor::Client::Error, 'invalid response format'
2022-05-07 20:08:51 +05:30
end
2022-08-13 15:12:31 +05:30
# url must be used within get method otherwise this would avoid validation by GitLab::HTTP
def url(path)
base_url = Gitlab::Utils.append_path(integration.url, '/api/v2.0/')
Gitlab::Utils.append_path(base_url, path)
2022-05-07 20:08:51 +05:30
end
def headers
auth = Base64.strict_encode64("#{integration.username}:#{integration.password}")
{
'Content-Type': 'application/json',
'Authorization': "Basic #{auth}"
}
end
end
end
end