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
|
2020-10-12 14:08:31 -04:00
|
|
|
releases = order_releases(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 include_subgroups?
|
|
|
|
params.fetch(:include_subgroups, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
strong_memoize(:projects) do
|
|
|
|
if parent.is_a?(Project)
|
|
|
|
Ability.allowed?(current_user, :read_release, parent) ? [parent] : []
|
|
|
|
elsif parent.is_a?(Group)
|
|
|
|
accessible_projects
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accessible_projects
|
|
|
|
projects = if include_subgroups?
|
|
|
|
Project.for_group_and_its_subgroups(parent)
|
|
|
|
else
|
|
|
|
parent.projects
|
|
|
|
end
|
|
|
|
|
|
|
|
projects.select { |project| Ability.allowed?(current_user, :read_release, project) }
|
|
|
|
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
|