Read about our upcoming Code of Conduct on this issue

release.rb 1.85 KB
Newer Older
1
2
# frozen_string_literal: true

3
class Release < ApplicationRecord
4
  include Presentable
5
  include CacheMarkdownField
Shinya Maeda's avatar
Shinya Maeda committed
6
  include Gitlab::Utils::StrongMemoize
7
8
9

  cache_markdown_field :description

10
  belongs_to :project
Alessio Caiazza's avatar
Alessio Caiazza committed
11
  # releases prior to 11.7 have no author
12
  belongs_to :author, class_name: 'User'
13

14
15
  has_many :links, class_name: 'Releases::Link'

16
17
  has_many :milestone_releases
  has_many :milestones, through: :milestone_releases
18
  has_one :evidence
19

20
21
22
23
  default_value_for :released_at, allows_nil: false do
    Time.zone.now
  end

24
25
  accepts_nested_attributes_for :links, allow_destroy: true

26
  validates :description, :project, :tag, presence: true
27
  validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
Alessio Caiazza's avatar
Alessio Caiazza committed
28

29
  scope :sorted, -> { order(released_at: :desc) }
30
  scope :with_project_and_namespace, -> { includes(project: :namespace) }
31

Alessio Caiazza's avatar
Alessio Caiazza committed
32
33
  delegate :repository, to: :project

34
  after_commit :create_evidence!, on: :create
35
  after_commit :notify_new_release, on: :create
36

Alessio Caiazza's avatar
Alessio Caiazza committed
37
  def commit
Shinya Maeda's avatar
Shinya Maeda committed
38
39
40
    strong_memoize(:commit) do
      repository.commit(actual_sha)
    end
Alessio Caiazza's avatar
Alessio Caiazza committed
41
  end
42

Shinya Maeda's avatar
Shinya Maeda committed
43
44
  def tag_missing?
    actual_tag.nil?
45
46
  end

47
48
49
50
51
  def assets_count(except: [])
    links_count = links.count
    sources_count = except.include?(:sources) ? 0 : sources.count

    links_count + sources_count
52
53
54
55
56
57
58
59
  end

  def sources
    strong_memoize(:sources) do
      Releases::Source.all(project, tag)
    end
  end

60
61
62
63
  def upcoming_release?
    released_at.present? && released_at > Time.zone.now
  end

Shinya Maeda's avatar
Shinya Maeda committed
64
65
66
67
  private

  def actual_sha
    sha || actual_tag&.dereferenced_target
68
69
  end

Shinya Maeda's avatar
Shinya Maeda committed
70
71
72
73
  def actual_tag
    strong_memoize(:actual_tag) do
      repository.find_tag(tag)
    end
74
  end
75
76
77
78

  def create_evidence!
    CreateEvidenceWorker.perform_async(self.id)
  end
79
80
81
82

  def notify_new_release
    NewReleaseWorker.perform_async(id)
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83
end
84
85

Release.prepend_if_ee('EE::Release')