2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class Release < ApplicationRecord
|
2019-10-23 17:06:17 -04:00
|
|
|
include Presentable
|
2016-10-06 17:17:11 -04:00
|
|
|
include CacheMarkdownField
|
2020-01-14 10:07:55 -05:00
|
|
|
include Importable
|
2018-12-25 04:48:26 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-10-06 17:17:11 -04:00
|
|
|
|
|
|
|
cache_markdown_field :description
|
|
|
|
|
2021-03-17 11:09:03 -04:00
|
|
|
belongs_to :project, touch: true
|
2018-12-13 06:08:53 -05:00
|
|
|
# releases prior to 11.7 have no author
|
2018-12-12 04:56:43 -05:00
|
|
|
belongs_to :author, class_name: 'User'
|
2015-11-06 09:29:04 -05:00
|
|
|
|
2019-01-01 20:40:33 -05:00
|
|
|
has_many :links, class_name: 'Releases::Link'
|
2021-04-29 14:10:23 -04:00
|
|
|
has_many :sorted_links, -> { sorted }, class_name: 'Releases::Link', inverse_of: :release
|
2019-01-01 20:40:33 -05:00
|
|
|
|
2019-09-16 11:06:26 -04:00
|
|
|
has_many :milestone_releases
|
|
|
|
has_many :milestones, through: :milestone_releases
|
2020-03-24 08:09:42 -04:00
|
|
|
has_many :evidences, inverse_of: :release, class_name: 'Releases::Evidence'
|
2019-09-03 05:38:59 -04:00
|
|
|
|
2019-01-01 20:40:33 -05:00
|
|
|
accepts_nested_attributes_for :links, allow_destroy: true
|
|
|
|
|
2020-07-17 11:09:13 -04:00
|
|
|
before_create :set_released_at
|
|
|
|
|
2020-03-16 08:09:12 -04:00
|
|
|
validates :project, :tag, presence: true
|
2021-05-05 05:10:02 -04:00
|
|
|
validates :description, length: { maximum: Gitlab::Database::MAX_TEXT_SIZE_LIMIT }, if: :description_changed?
|
2019-09-16 11:06:26 -04:00
|
|
|
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
|
2021-01-26 10:08:58 -05:00
|
|
|
validates :links, nested_attributes_duplicates: { scope: :release, child_attributes: %i[name url filepath] }
|
2018-12-13 06:08:53 -05:00
|
|
|
|
2019-07-03 05:12:15 -04:00
|
|
|
scope :sorted, -> { order(released_at: :desc) }
|
2021-04-28 05:10:13 -04:00
|
|
|
scope :preloaded, -> {
|
|
|
|
includes(:author, :evidences, :milestones, :links, :sorted_links,
|
|
|
|
project: [:project_feature, :route, { namespace: :route }])
|
|
|
|
}
|
2021-10-05 20:11:56 -04:00
|
|
|
scope :with_milestones, -> { joins(:milestone_releases) }
|
2019-11-04 10:07:36 -05:00
|
|
|
scope :recent, -> { sorted.limit(MAX_NUMBER_TO_DISPLAY) }
|
2020-11-18 16:09:32 -05:00
|
|
|
scope :without_evidence, -> { left_joins(:evidences).where(::Releases::Evidence.arel_table[:id].eq(nil)) }
|
|
|
|
scope :released_within_2hrs, -> { where(released_at: Time.zone.now - 1.hour..Time.zone.now + 1.hour) }
|
2018-12-21 09:51:46 -05:00
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
# Sorting
|
2021-05-27 05:10:32 -04:00
|
|
|
scope :order_created, -> { reorder(created_at: :asc) }
|
|
|
|
scope :order_created_desc, -> { reorder(created_at: :desc) }
|
|
|
|
scope :order_released, -> { reorder(released_at: :asc) }
|
|
|
|
scope :order_released_desc, -> { reorder(released_at: :desc) }
|
2020-10-12 14:08:31 -04:00
|
|
|
|
2018-12-13 06:08:53 -05:00
|
|
|
delegate :repository, to: :project
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
MAX_NUMBER_TO_DISPLAY = 3
|
|
|
|
|
2019-10-31 23:06:26 -04:00
|
|
|
def to_param
|
|
|
|
CGI.escape(tag)
|
|
|
|
end
|
|
|
|
|
2018-12-13 06:08:53 -05:00
|
|
|
def commit
|
2018-12-25 04:48:26 -05:00
|
|
|
strong_memoize(:commit) do
|
|
|
|
repository.commit(actual_sha)
|
|
|
|
end
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|
2018-12-21 09:13:23 -05:00
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
def tag_missing?
|
|
|
|
actual_tag.nil?
|
2018-12-21 09:13:23 -05:00
|
|
|
end
|
|
|
|
|
2019-05-03 09:29:20 -04:00
|
|
|
def assets_count(except: [])
|
2021-04-28 05:10:13 -04:00
|
|
|
links_count = links.size
|
|
|
|
sources_count = except.include?(:sources) ? 0 : sources.size
|
2019-05-03 09:29:20 -04:00
|
|
|
|
|
|
|
links_count + sources_count
|
2019-01-01 20:40:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def sources
|
|
|
|
strong_memoize(:sources) do
|
|
|
|
Releases::Source.all(project, tag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-03 05:12:15 -04:00
|
|
|
def upcoming_release?
|
2020-03-24 08:09:42 -04:00
|
|
|
released_at.present? && released_at.to_i > Time.zone.now.to_i
|
2019-07-03 05:12:15 -04:00
|
|
|
end
|
|
|
|
|
2020-01-31 19:08:41 -05:00
|
|
|
def historical_release?
|
2020-03-24 08:09:42 -04:00
|
|
|
released_at.present? && released_at.to_i < created_at.to_i
|
2020-01-31 19:08:41 -05:00
|
|
|
end
|
|
|
|
|
2019-11-12 16:06:30 -05:00
|
|
|
def name
|
|
|
|
self.read_attribute(:name) || tag
|
|
|
|
end
|
|
|
|
|
2020-03-24 08:09:42 -04:00
|
|
|
def milestone_titles
|
2020-12-23 13:10:19 -05:00
|
|
|
self.milestones.order_by_dates_and_title.map {|m| m.title }.join(', ')
|
2019-11-19 04:06:16 -05:00
|
|
|
end
|
|
|
|
|
2020-11-06 19:08:58 -05:00
|
|
|
def to_hook_data(action)
|
|
|
|
Gitlab::HookData::ReleaseBuilder.new(self).build(action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_hooks(action)
|
|
|
|
hook_data = to_hook_data(action)
|
|
|
|
project.execute_hooks(hook_data, :release_hooks)
|
|
|
|
end
|
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def actual_sha
|
|
|
|
sha || actual_tag&.dereferenced_target
|
2018-12-21 09:13:23 -05:00
|
|
|
end
|
|
|
|
|
2018-12-25 04:48:26 -05:00
|
|
|
def actual_tag
|
|
|
|
strong_memoize(:actual_tag) do
|
|
|
|
repository.find_tag(tag)
|
|
|
|
end
|
2018-12-21 09:13:23 -05:00
|
|
|
end
|
2020-07-17 11:09:13 -04:00
|
|
|
|
|
|
|
def set_released_at
|
|
|
|
self.released_at ||= created_at
|
|
|
|
end
|
2020-10-12 14:08:31 -04:00
|
|
|
|
|
|
|
def self.sort_by_attribute(method)
|
|
|
|
case method.to_s
|
|
|
|
when 'created_at_asc' then order_created
|
|
|
|
when 'created_at_desc' then order_created_desc
|
|
|
|
when 'released_at_asc' then order_released
|
|
|
|
when 'released_at_desc' then order_released_desc
|
|
|
|
else
|
|
|
|
order_created_desc
|
|
|
|
end
|
|
|
|
end
|
2015-11-05 05:03:02 -05:00
|
|
|
end
|
2019-10-03 20:06:03 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Release.prepend_mod_with('Release')
|