2020-07-17 02:09:11 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Packages
|
|
|
|
module Composer
|
|
|
|
class PackagesPresenter
|
|
|
|
include API::Helpers::RelatedResourcesHelpers
|
|
|
|
|
2021-03-18 20:09:00 -04:00
|
|
|
def initialize(group, packages, is_v2 = false)
|
2020-07-17 02:09:11 -04:00
|
|
|
@group = group
|
|
|
|
@packages = packages
|
2021-03-18 20:09:00 -04:00
|
|
|
@is_v2 = is_v2
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def root
|
2021-03-10 07:09:14 -05:00
|
|
|
v2_path = expose_path(api_v4_group___packages_composer_p2_package_name_path({ id: @group.id, package_name: '%package%', format: '.json' }, true))
|
|
|
|
|
2021-03-18 20:09:00 -04:00
|
|
|
index = {
|
2021-03-10 07:09:14 -05:00
|
|
|
'packages' => [],
|
2021-03-18 20:09:00 -04:00
|
|
|
'metadata-url' => v2_path
|
|
|
|
}
|
|
|
|
|
|
|
|
# if the client is composer v2 then we don't want to
|
|
|
|
# include the provider_sha since it is computationally expensive
|
|
|
|
# to compute.
|
|
|
|
return index if @is_v2
|
|
|
|
|
|
|
|
v1_path = expose_path(api_v4_group___packages_composer_package_name_path({ id: @group.id, package_name: '%package%$%hash%', format: '.json' }, true))
|
|
|
|
|
|
|
|
index.merge!(
|
2021-03-10 07:09:14 -05:00
|
|
|
'provider-includes' => {
|
|
|
|
'p/%hash%.json' => {
|
|
|
|
'sha256' => provider_sha
|
|
|
|
}
|
|
|
|
},
|
2021-03-18 20:09:00 -04:00
|
|
|
'providers-url' => v1_path
|
|
|
|
)
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
{ 'providers' => providers_map }
|
|
|
|
end
|
|
|
|
|
|
|
|
def package_versions(packages = @packages)
|
2021-01-12 10:10:37 -05:00
|
|
|
package_versions_index(packages).as_json
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-12 10:10:37 -05:00
|
|
|
def package_versions_sha(packages = @packages)
|
|
|
|
package_versions_index(packages).sha
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
2021-01-12 10:10:37 -05:00
|
|
|
def package_versions_index(packages)
|
|
|
|
::Gitlab::Composer::VersionIndex.new(packages)
|
2020-07-17 02:09:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def providers_map
|
|
|
|
map = {}
|
|
|
|
|
|
|
|
@packages.group_by(&:name).each_pair do |package_name, packages|
|
|
|
|
map[package_name] = { 'sha256' => package_versions_sha(packages) }
|
|
|
|
end
|
|
|
|
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider_sha
|
|
|
|
Digest::SHA256.hexdigest(provider.to_json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|