File: package.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (65 lines) | stat: -rw-r--r-- 2,139 bytes parent folder | download
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Packages
  module Debian
    class Package < Packages::Package
      self.allow_legacy_sti_class = true

      has_one :publication, inverse_of: :package, class_name: 'Packages::Debian::Publication'
      has_one :distribution, through: :publication, source: :distribution, inverse_of: :packages,
        class_name: 'Packages::Debian::ProjectDistribution'

      accepts_nested_attributes_for :publication

      delegate :codename, :suite, to: :distribution, prefix: :distribution

      validates :name, format: { with: Gitlab::Regex.debian_package_name_regex }, if: :version?
      validates :name, inclusion: { in: [Packages::Debian::INCOMING_PACKAGE_NAME] }, unless: :version?

      validates :version,
        presence: true,
        format: { with: Gitlab::Regex.debian_version_regex },
        if: :version?
      validate :forbidden_changes

      scope :with_codename, ->(codename) do
        joins(:distribution).where(Packages::Debian::ProjectDistribution.table_name => { codename: codename })
      end

      scope :with_codename_or_suite, ->(codename_or_suite) do
        joins(:distribution)
          .where(Packages::Debian::ProjectDistribution.table_name => { codename: codename_or_suite })
          .or(where(Packages::Debian::ProjectDistribution.table_name => { suite: codename_or_suite }))
      end

      scope :preload_debian_file_metadata, -> { preload(package_files: :debian_file_metadatum) }

      def self.incoming_package!
        default
          .with_version(nil)
          .find_by!(name: Packages::Debian::INCOMING_PACKAGE_NAME)
      end

      def self.existing_packages_with(name:, version:)
        with_name(name)
          .with_version(version)
          .not_pending_destruction
      end

      def incoming?
        name == Packages::Debian::INCOMING_PACKAGE_NAME && version.nil?
      end

      private

      def forbidden_changes
        return unless persisted?

        # Debian incoming
        return unless version_was.nil? || version.nil?

        errors.add(:version, _('cannot be changed')) if version_changed?
      end
    end
  end
end