2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class Release < ApplicationRecord
|
2019-10-23 21:06:17 +00:00
|
|
|
include Presentable
|
2016-10-06 21:17:11 +00:00
|
|
|
include CacheMarkdownField
|
2020-01-14 15:07:55 +00:00
|
|
|
include Importable
|
2018-12-25 09:48:26 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-10-06 21:17:11 +00:00
|
|
|
|
|
|
|
cache_markdown_field :description
|
|
|
|
|
2015-11-06 14:29:04 +00:00
|
|
|
belongs_to :project
|
2018-12-13 11:08:53 +00:00
|
|
|
# releases prior to 11.7 have no author
|
2018-12-12 09:56:43 +00:00
|
|
|
belongs_to :author, class_name: 'User'
|
2015-11-06 14:29:04 +00:00
|
|
|
|
2019-01-02 01:40:33 +00:00
|
|
|
has_many :links, class_name: 'Releases::Link'
|
|
|
|
|
2019-09-16 15:06:26 +00:00
|
|
|
has_many :milestone_releases
|
|
|
|
has_many :milestones, through: :milestone_releases
|
2020-03-24 12:09:42 +00:00
|
|
|
has_many :evidences, inverse_of: :release, class_name: 'Releases::Evidence'
|
2019-09-03 09:38:59 +00:00
|
|
|
|
2019-01-02 01:40:33 +00:00
|
|
|
accepts_nested_attributes_for :links, allow_destroy: true
|
|
|
|
|
2020-07-17 15:09:13 +00:00
|
|
|
before_create :set_released_at
|
|
|
|
|
2020-03-16 12:09:12 +00:00
|
|
|
validates :project, :tag, presence: true
|
2019-09-16 15:06:26 +00:00
|
|
|
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
|
2018-12-13 11:08:53 +00:00
|
|
|
|
2019-07-03 09:12:15 +00:00
|
|
|
scope :sorted, -> { order(released_at: :desc) }
|
2020-03-24 12:09:42 +00:00
|
|
|
scope :preloaded, -> { includes(:evidences, :milestones, project: [:project_feature, :route, { namespace: :route }]) }
|
2019-11-04 15:07:36 +00:00
|
|
|
scope :with_project_and_namespace, -> { includes(project: :namespace) }
|
|
|
|
scope :recent, -> { sorted.limit(MAX_NUMBER_TO_DISPLAY) }
|
2018-12-21 14:51:46 +00:00
|
|
|
|
2018-12-13 11:08:53 +00:00
|
|
|
delegate :repository, to: :project
|
|
|
|
|
2019-11-04 15:07:36 +00:00
|
|
|
MAX_NUMBER_TO_DISPLAY = 3
|
|
|
|
|
2019-11-01 03:06:26 +00:00
|
|
|
def to_param
|
|
|
|
CGI.escape(tag)
|
|
|
|
end
|
|
|
|
|
2018-12-13 11:08:53 +00:00
|
|
|
def commit
|
2018-12-25 09:48:26 +00:00
|
|
|
strong_memoize(:commit) do
|
|
|
|
repository.commit(actual_sha)
|
|
|
|
end
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
2018-12-21 14:13:23 +00:00
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
def tag_missing?
|
|
|
|
actual_tag.nil?
|
2018-12-21 14:13:23 +00:00
|
|
|
end
|
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
def assets_count(except: [])
|
|
|
|
links_count = links.count
|
|
|
|
sources_count = except.include?(:sources) ? 0 : sources.count
|
|
|
|
|
|
|
|
links_count + sources_count
|
2019-01-02 01:40:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def sources
|
|
|
|
strong_memoize(:sources) do
|
|
|
|
Releases::Source.all(project, tag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-03 09:12:15 +00:00
|
|
|
def upcoming_release?
|
2020-03-24 12:09:42 +00:00
|
|
|
released_at.present? && released_at.to_i > Time.zone.now.to_i
|
2019-07-03 09:12:15 +00:00
|
|
|
end
|
|
|
|
|
2020-02-01 00:08:41 +00:00
|
|
|
def historical_release?
|
2020-03-24 12:09:42 +00:00
|
|
|
released_at.present? && released_at.to_i < created_at.to_i
|
2020-02-01 00:08:41 +00:00
|
|
|
end
|
|
|
|
|
2019-11-12 21:06:30 +00:00
|
|
|
def name
|
|
|
|
self.read_attribute(:name) || tag
|
|
|
|
end
|
|
|
|
|
2020-03-24 12:09:42 +00:00
|
|
|
def milestone_titles
|
|
|
|
self.milestones.map {|m| m.title }.sort.join(", ")
|
2019-11-19 09:06:16 +00:00
|
|
|
end
|
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def actual_sha
|
|
|
|
sha || actual_tag&.dereferenced_target
|
2018-12-21 14:13:23 +00:00
|
|
|
end
|
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
def actual_tag
|
|
|
|
strong_memoize(:actual_tag) do
|
|
|
|
repository.find_tag(tag)
|
|
|
|
end
|
2018-12-21 14:13:23 +00:00
|
|
|
end
|
2020-07-17 15:09:13 +00:00
|
|
|
|
|
|
|
def set_released_at
|
|
|
|
self.released_at ||= created_at
|
|
|
|
end
|
2015-11-05 10:03:02 +00:00
|
|
|
end
|
2019-10-04 00:06:03 +00:00
|
|
|
|
|
|
|
Release.prepend_if_ee('EE::Release')
|