2018-12-13 06:08:53 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ReleasesFinder
|
2020-05-06 20:11:11 -04:00
|
|
|
attr_reader :project, :current_user, :params
|
|
|
|
|
|
|
|
def initialize(project, current_user = nil, params = {})
|
2018-12-13 06:08:53 -05:00
|
|
|
@project = project
|
|
|
|
@current_user = current_user
|
2020-05-06 20:11:11 -04:00
|
|
|
@params = params
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|
|
|
|
|
2019-10-31 23:06:26 -04:00
|
|
|
def execute(preload: true)
|
2020-05-06 20:11:11 -04:00
|
|
|
return Release.none unless Ability.allowed?(current_user, :read_release, project)
|
2018-12-13 06:08:53 -05:00
|
|
|
|
2020-03-24 20:08:11 -04:00
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/211988
|
2020-05-06 20:11:11 -04:00
|
|
|
releases = project.releases.where.not(tag: nil) # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
releases = by_tag(releases)
|
2019-10-31 23:06:26 -04:00
|
|
|
releases = releases.preloaded if preload
|
|
|
|
releases.sorted
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|
2020-05-06 20:11:11 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_tag(releases)
|
|
|
|
return releases unless params[:tag].present?
|
|
|
|
|
|
|
|
releases.where(tag: params[:tag])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-12-13 06:08:53 -05:00
|
|
|
end
|