debian-mirror-gitlab/app/models/namespace/package_setting.rb

37 lines
1.4 KiB
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
class Namespace::PackageSetting < ApplicationRecord
2022-11-25 23:54:43 +05:30
include CascadingNamespaceSettingAttribute
2021-03-08 18:12:59 +05:30
self.primary_key = :namespace_id
self.table_name = 'namespace_package_settings'
2022-11-25 23:54:43 +05:30
cascading_attr :maven_package_requests_forwarding
cascading_attr :npm_package_requests_forwarding
cascading_attr :pypi_package_requests_forwarding
2021-03-08 18:12:59 +05:30
PackageSettingNotImplemented = Class.new(StandardError)
2021-06-08 01:23:25 +05:30
PACKAGES_WITH_SETTINGS = %w[maven generic].freeze
2021-03-08 18:12:59 +05:30
belongs_to :namespace, inverse_of: :package_setting_relation
validates :namespace, presence: true
validates :maven_duplicates_allowed, inclusion: { in: [true, false] }
validates :maven_duplicate_exception_regex, untrusted_regexp: true, length: { maximum: 255 }
2021-06-08 01:23:25 +05:30
validates :generic_duplicates_allowed, inclusion: { in: [true, false] }
validates :generic_duplicate_exception_regex, untrusted_regexp: true, length: { maximum: 255 }
2021-03-08 18:12:59 +05:30
class << self
def duplicates_allowed?(package)
return true unless package
raise PackageSettingNotImplemented unless PACKAGES_WITH_SETTINGS.include?(package.package_type)
duplicates_allowed = package.package_settings["#{package.package_type}_duplicates_allowed"]
regex = ::Gitlab::UntrustedRegexp.new("\\A#{package.package_settings["#{package.package_type}_duplicate_exception_regex"]}\\z")
2021-06-08 01:23:25 +05:30
duplicates_allowed || regex.match?(package.name) || regex.match?(package.version)
2021-03-08 18:12:59 +05:30
end
end
end