diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /app/models/packages | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'app/models/packages')
-rw-r--r-- | app/models/packages/debian.rb | 2 | ||||
-rw-r--r-- | app/models/packages/event.rb | 8 | ||||
-rw-r--r-- | app/models/packages/npm.rb | 13 | ||||
-rw-r--r-- | app/models/packages/package.rb | 14 | ||||
-rw-r--r-- | app/models/packages/package_file.rb | 24 |
5 files changed, 55 insertions, 6 deletions
diff --git a/app/models/packages/debian.rb b/app/models/packages/debian.rb index e20f1b8244a..2daafe0ebcf 100644 --- a/app/models/packages/debian.rb +++ b/app/models/packages/debian.rb @@ -6,6 +6,8 @@ module Packages COMPONENT_REGEX = DISTRIBUTION_REGEX.freeze ARCHITECTURE_REGEX = %r{[a-z0-9][-a-z0-9]*}.freeze + LETTER_REGEX = %r{(lib)?[a-z0-9]}.freeze + def self.table_name_prefix 'packages_debian_' end diff --git a/app/models/packages/event.rb b/app/models/packages/event.rb index a1eb7120117..bb2c33594e5 100644 --- a/app/models/packages/event.rb +++ b/app/models/packages/event.rb @@ -4,7 +4,7 @@ class Packages::Event < ApplicationRecord belongs_to :package, optional: true UNIQUE_EVENTS_ALLOWED = %i[push_package delete_package pull_package pull_symbol_package push_symbol_package].freeze - EVENT_SCOPES = ::Packages::Package.package_types.merge(container: 1000, tag: 1001).freeze + EVENT_SCOPES = ::Packages::Package.package_types.merge(container: 1000, tag: 1001, dependency_proxy: 1002).freeze EVENT_PREFIX = "i_package" @@ -23,7 +23,11 @@ class Packages::Event < ApplicationRecord list_tags: 9, cli_metadata: 10, pull_symbol_package: 11, - push_symbol_package: 12 + push_symbol_package: 12, + pull_manifest: 13, + pull_manifest_from_cache: 14, + pull_blob: 15, + pull_blob_from_cache: 16 } enum originator_type: { user: 0, deploy_token: 1, guest: 2 } diff --git a/app/models/packages/npm.rb b/app/models/packages/npm.rb new file mode 100644 index 00000000000..e49199d911c --- /dev/null +++ b/app/models/packages/npm.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true +module Packages + module Npm + # from "@scope/package-name" return "scope" or nil + def self.scope_of(package_name) + return unless package_name + return unless package_name.starts_with?('@') + return unless package_name.include?('/') + + package_name.match(Gitlab::Regex.npm_package_name_regex)&.captures&.first + end + end +end diff --git a/app/models/packages/package.rb b/app/models/packages/package.rb index d2e4f46898c..4ea127fc222 100644 --- a/app/models/packages/package.rb +++ b/app/models/packages/package.rb @@ -62,7 +62,7 @@ class Packages::Package < ApplicationRecord validate :valid_conan_package_recipe, if: :conan? validate :valid_composer_global_name, if: :composer? - validate :package_already_taken, if: :npm? + validate :npm_package_already_taken, if: :npm? validates :name, format: { with: Gitlab::Regex.conan_recipe_component_regex }, if: :conan? validates :name, format: { with: Gitlab::Regex.generic_package_name_regex }, if: :generic? validates :name, format: { with: Gitlab::Regex.helm_package_regex }, if: :helm? @@ -320,14 +320,22 @@ class Packages::Package < ApplicationRecord end end - def package_already_taken + def npm_package_already_taken return unless project + return unless follows_npm_naming_convention? - if project.package_already_taken?(name) + if project.package_already_taken?(name, version, package_type: :npm) errors.add(:base, _('Package already exists')) end end + # https://docs.gitlab.com/ee/user/packages/npm_registry/#package-naming-convention + def follows_npm_naming_convention? + return false unless project&.root_namespace&.path + + project.root_namespace.path == ::Packages::Npm.scope_of(name) + end + def unique_debian_package_name return unless debian_publication&.distribution diff --git a/app/models/packages/package_file.rb b/app/models/packages/package_file.rb index 799242a639a..8aa19397086 100644 --- a/app/models/packages/package_file.rb +++ b/app/models/packages/package_file.rb @@ -5,11 +5,14 @@ class Packages::PackageFile < ApplicationRecord delegate :project, :project_id, to: :package delegate :conan_file_type, to: :conan_file_metadatum - delegate :file_type, :component, :architecture, :fields, to: :debian_file_metadatum, prefix: :debian + delegate :file_type, :dsc?, :component, :architecture, :fields, to: :debian_file_metadatum, prefix: :debian delegate :channel, :metadata, to: :helm_file_metadatum, prefix: :helm belongs_to :package + # used to move the linked file within object storage + attribute :new_file_path, default: nil + has_one :conan_file_metadatum, inverse_of: :package_file, class_name: 'Packages::Conan::FileMetadatum' has_many :package_file_build_infos, inverse_of: :package_file, class_name: 'Packages::PackageFileBuildInfo' has_many :pipelines, through: :package_file_build_infos @@ -33,6 +36,8 @@ class Packages::PackageFile < ApplicationRecord 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) } scope :with_format, ->(format) { where(::Packages::PackageFile.arel_table[:file_name].matches("%.#{format}")) } + + scope :preload_package, -> { preload(:package) } scope :preload_conan_file_metadata, -> { preload(:conan_file_metadatum) } scope :preload_debian_file_metadata, -> { preload(:debian_file_metadatum) } scope :preload_helm_file_metadata, -> { preload(:helm_file_metadatum) } @@ -78,6 +83,12 @@ class Packages::PackageFile < ApplicationRecord before_save :update_size_from_file + # 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? + def download_path Gitlab::Routing.url_helpers.download_project_package_file_path(project, self) end @@ -87,6 +98,17 @@ class Packages::PackageFile < ApplicationRecord def update_size_from_file self.size ||= file.size end + + 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 end Packages::PackageFile.prepend_mod_with('Packages::PackageFile') |