debian-mirror-gitlab/app/presenters/packages/conan/package_presenter.rb

102 lines
2.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
module Conan
class PackagePresenter
2020-11-24 15:15:51 +05:30
include API::Helpers::Packages::Conan::ApiHelpers
2020-07-28 23:09:34 +05:30
include API::Helpers::RelatedResourcesHelpers
include Gitlab::Utils::StrongMemoize
attr_reader :params
2020-11-24 15:15:51 +05:30
def initialize(package, user, project, params = {})
@package = package
2020-07-28 23:09:34 +05:30
@user = user
@project = project
@params = params
end
def recipe_urls
map_package_files do |package_file|
2020-11-24 15:15:51 +05:30
next unless package_file.conan_file_metadatum.recipe_file?
options = url_options(package_file)
recipe_file_url(options)
2020-07-28 23:09:34 +05:30
end
end
def recipe_snapshot
map_package_files do |package_file|
package_file.file_md5 if package_file.conan_file_metadatum.recipe_file?
end
end
def package_urls
map_package_files do |package_file|
next unless package_file.conan_file_metadatum.package_file? && matching_reference?(package_file)
2020-11-24 15:15:51 +05:30
options = url_options(package_file).merge(
conan_package_reference: package_file.conan_file_metadatum.conan_package_reference,
package_revision: package_file.conan_file_metadatum.package_revision
)
package_file_url(options)
2020-07-28 23:09:34 +05:30
end
end
def package_snapshot
map_package_files do |package_file|
next unless package_file.conan_file_metadatum.package_file? && matching_reference?(package_file)
package_file.file_md5
end
end
private
2020-11-24 15:15:51 +05:30
def url_options(package_file)
{
package_name: @package.name,
package_version: @package.version,
package_username: @package.conan_metadatum.package_username,
package_channel: @package.conan_metadatum.package_channel,
file_name: package_file.file_name,
recipe_revision: package_file.conan_file_metadatum.recipe_revision.presence || ::Packages::Conan::FileMetadatum::DEFAULT_RECIPE_REVISION
}
2020-07-28 23:09:34 +05:30
end
def map_package_files
package_files.to_a.map do |package_file|
2020-11-24 15:15:51 +05:30
next unless package_file.conan_file_metadatum
2020-07-28 23:09:34 +05:30
key = package_file.file_name
value = yield(package_file)
next unless key && value
[key, value]
end.compact.to_h
end
def package_files
2020-11-24 15:15:51 +05:30
return unless @package
2020-07-28 23:09:34 +05:30
2022-03-02 08:16:31 +05:30
strong_memoize(:package_files) do
if Feature.enabled?(:packages_installable_package_files, default_enabled: :yaml)
@package.installable_package_files.preload_conan_file_metadata
else
@package.package_files.preload_conan_file_metadata
end
end
2020-07-28 23:09:34 +05:30
end
def matching_reference?(package_file)
package_file.conan_file_metadatum.conan_package_reference == conan_package_reference
end
def conan_package_reference
params[:conan_package_reference]
end
end
end
end