debian-mirror-gitlab/app/models/release.rb

143 lines
3.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class Release < ApplicationRecord
2019-12-26 22:10:19 +05:30
include Presentable
2016-11-03 12:29:30 +05:30
include CacheMarkdownField
2020-03-13 15:44:24 +05:30
include Importable
2019-02-15 15:39:39 +05:30
include Gitlab::Utils::StrongMemoize
2022-05-07 20:08:51 +05:30
include EachBatch
include FromUnion
2016-11-03 12:29:30 +05:30
cache_markdown_field :description
2021-04-29 21:17:54 +05:30
belongs_to :project, touch: true
2019-02-15 15:39:39 +05:30
# releases prior to 11.7 have no author
belongs_to :author, class_name: 'User'
has_many :links, class_name: 'Releases::Link'
2021-06-08 01:23:25 +05:30
has_many :sorted_links, -> { sorted }, class_name: 'Releases::Link', inverse_of: :release
2019-02-15 15:39:39 +05:30
2019-12-04 20:38:33 +05:30
has_many :milestone_releases
has_many :milestones, through: :milestone_releases
2020-04-22 19:07:51 +05:30
has_many :evidences, inverse_of: :release, class_name: 'Releases::Evidence'
2019-12-04 20:38:33 +05:30
2019-02-15 15:39:39 +05:30
accepts_nested_attributes_for :links, allow_destroy: true
2015-11-26 14:37:03 +05:30
2020-10-24 23:57:45 +05:30
before_create :set_released_at
2020-04-08 14:13:33 +05:30
validates :project, :tag, presence: true
2023-03-17 16:20:25 +05:30
validates :author_id, presence: true, if: :validate_release_with_author?
2022-05-07 20:08:51 +05:30
validates :tag, uniqueness: { scope: :project_id }
2021-06-08 01:23:25 +05:30
validates :description, length: { maximum: Gitlab::Database::MAX_TEXT_SIZE_LIMIT }, if: :description_changed?
2019-12-04 20:38:33 +05:30
validates_associated :milestone_releases, message: -> (_, obj) { obj[:value].map(&:errors).map(&:full_messages).join(",") }
2021-03-11 19:13:27 +05:30
validates :links, nested_attributes_duplicates: { scope: :release, child_attributes: %i[name url filepath] }
2019-02-15 15:39:39 +05:30
2019-09-30 21:07:59 +05:30
scope :sorted, -> { order(released_at: :desc) }
2021-06-08 01:23:25 +05:30
scope :preloaded, -> {
includes(:author, :evidences, :milestones, :links, :sorted_links,
project: [:project_feature, :route, { namespace: :route }])
}
2021-11-18 22:05:49 +05:30
scope :with_milestones, -> { joins(:milestone_releases) }
2021-12-11 22:18:48 +05:30
scope :with_group_milestones, -> { joins(:milestones).where.not(milestones: { group_id: nil }) }
2019-12-26 22:10:19 +05:30
scope :recent, -> { sorted.limit(MAX_NUMBER_TO_DISPLAY) }
2021-02-22 17:27:13 +05:30
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) }
2019-02-15 15:39:39 +05:30
2021-01-03 14:25:43 +05:30
# Sorting
2021-09-04 01:27:46 +05:30
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) }
2021-01-03 14:25:43 +05:30
2019-02-15 15:39:39 +05:30
delegate :repository, to: :project
2019-12-26 22:10:19 +05:30
MAX_NUMBER_TO_DISPLAY = 3
def to_param
2022-07-23 23:45:48 +05:30
tag
2019-12-26 22:10:19 +05:30
end
2019-02-15 15:39:39 +05:30
def commit
strong_memoize(:commit) do
repository.commit(actual_sha)
end
end
def tag_missing?
actual_tag.nil?
end
2019-07-31 22:56:46 +05:30
def assets_count(except: [])
2021-06-08 01:23:25 +05:30
links_count = links.size
sources_count = except.include?(:sources) ? 0 : sources.size
2019-07-31 22:56:46 +05:30
links_count + sources_count
2019-02-15 15:39:39 +05:30
end
def sources
strong_memoize(:sources) do
Releases::Source.all(project, tag)
end
end
2019-09-30 21:07:59 +05:30
def upcoming_release?
2020-04-22 19:07:51 +05:30
released_at.present? && released_at.to_i > Time.zone.now.to_i
2019-09-30 21:07:59 +05:30
end
2020-03-13 15:44:24 +05:30
def historical_release?
2020-04-22 19:07:51 +05:30
released_at.present? && released_at.to_i < created_at.to_i
2020-03-13 15:44:24 +05:30
end
2019-12-26 22:10:19 +05:30
def name
self.read_attribute(:name) || tag
end
2020-04-22 19:07:51 +05:30
def milestone_titles
2022-08-27 11:52:29 +05:30
self.milestones.order_by_dates_and_title.map { |m| m.title }.join(', ')
2020-01-01 13:55:28 +05:30
end
2021-01-29 00:20:46 +05:30
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
2019-02-15 15:39:39 +05:30
private
def actual_sha
sha || actual_tag&.dereferenced_target
end
def actual_tag
strong_memoize(:actual_tag) do
repository.find_tag(tag)
end
end
2020-10-24 23:57:45 +05:30
2022-07-23 23:45:48 +05:30
def validate_release_with_author?
Feature.enabled?(:validate_release_with_author, self.project)
end
2020-10-24 23:57:45 +05:30
def set_released_at
self.released_at ||= created_at
end
2021-01-03 14:25:43 +05:30
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-26 14:37:03 +05:30
end
2019-12-21 20:55:43 +05:30
2021-06-08 01:23:25 +05:30
Release.prepend_mod_with('Release')