summaryrefslogtreecommitdiff
path: root/app/models/namespace/package_setting.rb
blob: a2064e020b3c06c0989aaccc341ad4f5e8cb5ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

class Namespace::PackageSetting < ApplicationRecord
  self.primary_key = :namespace_id
  self.table_name = 'namespace_package_settings'

  PackageSettingNotImplemented = Class.new(StandardError)

  PACKAGES_WITH_SETTINGS = %w[maven].freeze

  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 }

  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")

      duplicates_allowed || regex.match?(package.name)
    end
  end
end