2020-07-08 08:09:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
class Packages::PackageFile < ApplicationRecord
|
|
|
|
include UpdateProjectStatistics
|
2020-11-27 13:09:52 -05:00
|
|
|
include FileStoreMounter
|
2022-01-06 13:13:45 -05:00
|
|
|
include Packages::Installable
|
2022-01-18 10:14:54 -05:00
|
|
|
include Packages::Destructible
|
2022-01-06 13:13:45 -05:00
|
|
|
|
|
|
|
INSTALLABLE_STATUSES = [:default].freeze
|
2020-07-08 08:09:33 -04:00
|
|
|
|
|
|
|
delegate :project, :project_id, to: :package
|
|
|
|
delegate :conan_file_type, to: :conan_file_metadatum
|
2021-08-02 08:09:59 -04:00
|
|
|
delegate :file_type, :dsc?, :component, :architecture, :fields, to: :debian_file_metadatum, prefix: :debian
|
2021-05-04 14:10:03 -04:00
|
|
|
delegate :channel, :metadata, to: :helm_file_metadatum, prefix: :helm
|
2020-07-08 08:09:33 -04:00
|
|
|
|
2022-01-18 10:14:54 -05:00
|
|
|
enum status: { default: 0, pending_destruction: 1, processing: 2, error: 3 }
|
2022-01-06 13:13:45 -05:00
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
belongs_to :package
|
|
|
|
|
2021-08-10 17:10:06 -04:00
|
|
|
# used to move the linked file within object storage
|
|
|
|
attribute :new_file_path, default: nil
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
has_one :conan_file_metadatum, inverse_of: :package_file, class_name: 'Packages::Conan::FileMetadatum'
|
2020-11-16 19:09:16 -05:00
|
|
|
has_many :package_file_build_infos, inverse_of: :package_file, class_name: 'Packages::PackageFileBuildInfo'
|
2021-11-11 22:12:11 -05:00
|
|
|
has_many :pipelines, through: :package_file_build_infos, disable_joins: true
|
2020-12-21 01:10:03 -05:00
|
|
|
has_one :debian_file_metadatum, inverse_of: :package_file, class_name: 'Packages::Debian::FileMetadatum'
|
2021-05-04 14:10:03 -04:00
|
|
|
has_one :helm_file_metadatum, inverse_of: :package_file, class_name: 'Packages::Helm::FileMetadatum'
|
2020-07-08 08:09:33 -04:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :conan_file_metadatum
|
2020-12-21 01:10:03 -05:00
|
|
|
accepts_nested_attributes_for :debian_file_metadatum
|
2021-05-04 14:10:03 -04:00
|
|
|
accepts_nested_attributes_for :helm_file_metadatum
|
2020-07-08 08:09:33 -04:00
|
|
|
|
|
|
|
validates :package, presence: true
|
|
|
|
validates :file, presence: true
|
|
|
|
validates :file_name, presence: true
|
|
|
|
|
2020-07-31 05:09:34 -04:00
|
|
|
validates :file_name, uniqueness: { scope: :package }, if: -> { package&.pypi? }
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
scope :recent, -> { order(id: :desc) }
|
2021-06-25 08:07:44 -04:00
|
|
|
scope :limit_recent, ->(limit) { recent.limit(limit) }
|
2021-05-11 14:10:36 -04:00
|
|
|
scope :for_package_ids, ->(ids) { where(package_id: ids) }
|
2020-07-08 08:09:33 -04:00
|
|
|
scope :with_file_name, ->(file_name) { where(file_name: file_name) }
|
|
|
|
scope :with_file_name_like, ->(file_name) { where(arel_table[:file_name].matches(file_name)) }
|
|
|
|
scope :with_files_stored_locally, -> { where(file_store: ::Packages::PackageFileUploader::Store::LOCAL) }
|
2021-06-29 02:07:52 -04:00
|
|
|
scope :with_format, ->(format) { where(::Packages::PackageFile.arel_table[:file_name].matches("%.#{format}")) }
|
2021-08-02 08:09:59 -04:00
|
|
|
|
|
|
|
scope :preload_package, -> { preload(:package) }
|
2021-10-27 20:10:09 -04:00
|
|
|
scope :preload_pipelines, -> { preload(pipelines: :user) }
|
2020-07-08 08:09:33 -04:00
|
|
|
scope :preload_conan_file_metadata, -> { preload(:conan_file_metadatum) }
|
2020-12-21 01:10:03 -05:00
|
|
|
scope :preload_debian_file_metadata, -> { preload(:debian_file_metadatum) }
|
2021-05-28 05:10:41 -04:00
|
|
|
scope :preload_helm_file_metadata, -> { preload(:helm_file_metadatum) }
|
2020-07-08 08:09:33 -04:00
|
|
|
|
2021-03-11 16:09:09 -05:00
|
|
|
scope :for_rubygem_with_file_name, ->(project, file_name) do
|
|
|
|
joins(:package).merge(project.packages.rubygems).with_file_name(file_name)
|
|
|
|
end
|
|
|
|
|
2021-05-28 05:10:41 -04:00
|
|
|
scope :for_helm_with_channel, ->(project, channel) do
|
2022-01-06 13:13:45 -05:00
|
|
|
result = joins(:package)
|
|
|
|
.merge(project.packages.helm.installable)
|
|
|
|
.joins(:helm_file_metadatum)
|
|
|
|
.where(packages_helm_file_metadata: { channel: channel })
|
2022-01-18 10:14:54 -05:00
|
|
|
result = result.installable if Feature.enabled?(:packages_installable_package_files, default_enabled: :yaml)
|
2022-01-06 13:13:45 -05:00
|
|
|
result
|
2021-05-28 05:10:41 -04:00
|
|
|
end
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
scope :with_conan_file_type, ->(file_type) do
|
|
|
|
joins(:conan_file_metadatum)
|
|
|
|
.where(packages_conan_file_metadata: { conan_file_type: ::Packages::Conan::FileMetadatum.conan_file_types[file_type] })
|
|
|
|
end
|
|
|
|
|
2020-12-21 01:10:03 -05:00
|
|
|
scope :with_debian_file_type, ->(file_type) do
|
|
|
|
joins(:debian_file_metadatum)
|
2021-05-11 14:10:36 -04:00
|
|
|
.where(packages_debian_file_metadata: { file_type: ::Packages::Debian::FileMetadatum.file_types[file_type] })
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :with_debian_component_name, ->(component_name) do
|
|
|
|
joins(:debian_file_metadatum)
|
|
|
|
.where(packages_debian_file_metadata: { component: component_name })
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :with_debian_architecture_name, ->(architecture_name) do
|
|
|
|
joins(:debian_file_metadatum)
|
|
|
|
.where(packages_debian_file_metadata: { architecture: architecture_name })
|
2020-12-21 01:10:03 -05:00
|
|
|
end
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
scope :with_conan_package_reference, ->(conan_package_reference) do
|
|
|
|
joins(:conan_file_metadatum)
|
|
|
|
.where(packages_conan_file_metadata: { conan_package_reference: conan_package_reference })
|
|
|
|
end
|
|
|
|
|
2021-08-30 05:09:12 -04:00
|
|
|
def self.most_recent!
|
|
|
|
recent.first!
|
|
|
|
end
|
|
|
|
|
2020-11-27 13:09:52 -05:00
|
|
|
mount_file_store_uploader Packages::PackageFileUploader
|
2020-07-08 08:09:33 -04:00
|
|
|
|
|
|
|
update_project_statistics project_statistics_name: :packages_size
|
|
|
|
|
2020-07-23 23:09:19 -04:00
|
|
|
before_save :update_size_from_file
|
|
|
|
|
2021-08-10 17:10:06 -04:00
|
|
|
# if a new_file_path is provided, we need
|
|
|
|
# * disable the remove_previously_stored_file callback so that carrierwave doesn't take care of the file
|
|
|
|
# * enable a new after_commit callback that will move the file in object storage
|
|
|
|
skip_callback :commit, :after, :remove_previously_stored_file, if: :execute_move_in_object_storage?
|
|
|
|
after_commit :move_in_object_storage, if: :execute_move_in_object_storage?
|
|
|
|
|
2022-01-06 13:13:45 -05:00
|
|
|
# Returns the most recent installable package file for *each* of the given packages.
|
2021-09-14 08:10:35 -04:00
|
|
|
# The order is not guaranteed.
|
|
|
|
def self.most_recent_for(packages, extra_join: nil, extra_where: nil)
|
|
|
|
cte_name = :packages_cte
|
|
|
|
cte = Gitlab::SQL::CTE.new(cte_name, packages.select(:id))
|
|
|
|
|
2022-01-18 10:14:54 -05:00
|
|
|
package_files = if Feature.enabled?(:packages_installable_package_files, default_enabled: :yaml)
|
2022-01-06 13:13:45 -05:00
|
|
|
::Packages::PackageFile.installable.limit_recent(1)
|
|
|
|
.where(arel_table[:package_id].eq(Arel.sql("#{cte_name}.id")))
|
|
|
|
else
|
|
|
|
::Packages::PackageFile.limit_recent(1)
|
|
|
|
.where(arel_table[:package_id].eq(Arel.sql("#{cte_name}.id")))
|
|
|
|
end
|
2021-09-14 08:10:35 -04:00
|
|
|
|
|
|
|
package_files = package_files.joins(extra_join) if extra_join
|
|
|
|
package_files = package_files.where(extra_where) if extra_where
|
|
|
|
|
|
|
|
query = select('finder.*')
|
|
|
|
.from([Arel.sql(cte_name.to_s), package_files.arel.lateral.as('finder')])
|
|
|
|
|
|
|
|
query.with(cte.to_arel)
|
|
|
|
end
|
|
|
|
|
2020-07-08 08:09:33 -04:00
|
|
|
def download_path
|
2020-07-21 08:09:30 -04:00
|
|
|
Gitlab::Routing.url_helpers.download_project_package_file_path(project, self)
|
2020-07-08 08:09:33 -04:00
|
|
|
end
|
|
|
|
|
2020-07-23 23:09:19 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def update_size_from_file
|
|
|
|
self.size ||= file.size
|
|
|
|
end
|
2021-08-10 17:10:06 -04:00
|
|
|
|
|
|
|
def execute_move_in_object_storage?
|
|
|
|
!file.file_storage? && new_file_path?
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_in_object_storage
|
|
|
|
carrierwave_file = file.file
|
|
|
|
|
|
|
|
carrierwave_file.copy_to(new_file_path)
|
|
|
|
carrierwave_file.delete
|
|
|
|
end
|
2020-07-08 08:09:33 -04:00
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Packages::PackageFile.prepend_mod_with('Packages::PackageFile')
|