2018-12-13 06:08:53 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ReleasesFinder
|
2020-08-12 08:10:25 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-05-06 20:11:11 -04:00
|
|
|
|
2020-08-12 08:10:25 -04:00
|
|
|
attr_reader :parent, :current_user, :params
|
|
|
|
|
|
|
|
def initialize(parent, current_user = nil, params = {})
|
|
|
|
@parent = parent
|
2018-12-13 06:08:53 -05:00
|
|
|
@current_user = current_user
|
2020-05-06 20:11:11 -04:00
|
|
|
@params = params
|
2020-10-12 14:08:31 -04:00
|
|
|
|
|
|
|
params[:order_by] ||= 'released_at'
|
|
|
|
params[:sort] ||= 'desc'
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|
|
|
|
|
2019-10-31 23:06:26 -04:00
|
|
|
def execute(preload: true)
|
2020-08-12 08:10:25 -04:00
|
|
|
return Release.none if projects.empty?
|
2018-12-13 06:08:53 -05:00
|
|
|
|
2020-08-12 08:10:25 -04:00
|
|
|
releases = get_releases
|
2020-05-06 20:11:11 -04:00
|
|
|
releases = by_tag(releases)
|
2019-10-31 23:06:26 -04:00
|
|
|
releases = releases.preloaded if preload
|
2021-04-19 11:09:08 -04:00
|
|
|
order_releases(releases)
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|
2020-05-06 20:11:11 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-12 08:10:25 -04:00
|
|
|
def get_releases
|
|
|
|
Release.where(project_id: projects).where.not(tag: nil) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
strong_memoize(:projects) do
|
|
|
|
if parent.is_a?(Project)
|
|
|
|
Ability.allowed?(current_user, :read_release, parent) ? [parent] : []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 20:11:11 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_tag(releases)
|
|
|
|
return releases unless params[:tag].present?
|
|
|
|
|
|
|
|
releases.where(tag: params[:tag])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2020-10-12 14:08:31 -04:00
|
|
|
|
|
|
|
def order_releases(releases)
|
|
|
|
releases.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
|
|
|
|
end
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|