2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module IssuableCollections
|
|
|
|
extend ActiveSupport::Concern
|
2019-12-04 20:38:33 +05:30
|
|
|
include PaginatedCollection
|
2016-09-13 17:45:13 +05:30
|
|
|
include SortingHelper
|
2019-12-04 20:38:33 +05:30
|
|
|
include SortingPreference
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
included do
|
2018-03-17 18:26:18 +05:30
|
|
|
helper_method :finder
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def show_alert_if_search_is_disabled
|
2023-03-04 22:38:38 +05:30
|
|
|
if current_user || params[:search].blank? || !html_request? || Feature.disabled?(:disable_anonymous_search, type: :ops)
|
|
|
|
return
|
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
|
|
|
|
flash.now[:notice] = _('You must sign in to search for specific terms.')
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
def set_issuables_index
|
2021-11-11 11:23:49 +05:30
|
|
|
show_alert_if_search_is_disabled
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
@issuables = issuables_collection
|
2022-08-13 15:12:31 +05:30
|
|
|
set_pagination
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
return if redirect_out_of_range(@issuables, @total_pages)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
if params[:label_name].present? && @project
|
2018-03-17 18:26:18 +05:30
|
|
|
labels_params = { project_id: @project.id, title: params[:label_name] }
|
|
|
|
@labels = LabelsFinder.new(current_user, labels_params).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
@users = []
|
|
|
|
if params[:assignee_id].present?
|
|
|
|
assignee = User.find_by_id(params[:assignee_id])
|
|
|
|
@users.push(assignee) if assignee
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:author_id].present?
|
|
|
|
author = User.find_by_id(params[:author_id])
|
|
|
|
@users.push(author) if author
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_pagination
|
2021-01-03 14:25:43 +05:30
|
|
|
row_count = finder.row_count
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
@issuables = @issuables.page(params[:page])
|
2019-09-04 21:01:54 +05:30
|
|
|
@issuables = per_page_for_relative_position if params[:sort] == 'relative_position'
|
2021-01-03 14:25:43 +05:30
|
|
|
@issuables = @issuables.without_count if row_count == -1
|
2020-06-23 00:09:42 +05:30
|
|
|
@issuable_meta_data = Gitlab::IssuableMetadata.new(current_user, @issuables).data
|
2021-01-03 14:25:43 +05:30
|
|
|
@total_pages = page_count_for_relation(@issuables, row_count)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def issuables_collection
|
|
|
|
finder.execute.preload(preload_for_collection)
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def page_count_for_relation(relation, row_count)
|
|
|
|
limit = relation.limit_value.to_f
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
return 1 if limit == 0
|
2021-01-03 14:25:43 +05:30
|
|
|
return (params[:page] || 1).to_i + 1 if row_count == -1
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
(row_count.to_f / limit).ceil
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
# manual / relative_position sorting allows for 100 items on the page
|
|
|
|
def per_page_for_relative_position
|
|
|
|
@issuables.per(100) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def issuable_finder_for(finder_class)
|
2019-02-15 15:39:39 +05:30
|
|
|
finder_class.new(current_user, finder_options)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2019-02-15 15:39:39 +05:30
|
|
|
def finder_options
|
2020-07-28 23:09:34 +05:30
|
|
|
strong_memoize(:finder_options) do
|
|
|
|
params[:state] = default_state if params[:state].blank?
|
|
|
|
|
|
|
|
options = {
|
|
|
|
scope: params[:scope],
|
|
|
|
state: params[:state],
|
|
|
|
confidential: Gitlab::Utils.to_boolean(params[:confidential]),
|
|
|
|
sort: set_sort_order
|
|
|
|
}
|
|
|
|
|
|
|
|
# Used by view to highlight active option
|
|
|
|
@sort = options[:sort]
|
|
|
|
|
|
|
|
# When a user looks for an exact iid, we do not filter by search but only by iid
|
|
|
|
if params[:search] =~ /^#(?<iid>\d+)\z/
|
|
|
|
options[:iids] = Regexp.last_match[:iid]
|
|
|
|
params[:search] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if @project
|
|
|
|
options[:project_id] = @project.id
|
|
|
|
options[:attempt_project_search_optimizations] = true
|
|
|
|
elsif @group
|
|
|
|
options[:group_id] = @group.id
|
|
|
|
options[:include_subgroups] = true
|
|
|
|
options[:attempt_group_search_optimizations] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
params.permit(finder_type.valid_params).merge(options)
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def default_state
|
|
|
|
'opened'
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def legacy_sort_cookie_name
|
|
|
|
'issuable_sort'
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def default_sort_order
|
|
|
|
case params[:state]
|
2018-03-17 18:26:18 +05:30
|
|
|
when 'opened', 'all' then sort_value_created_date
|
2016-09-13 17:45:13 +05:30
|
|
|
when 'merged', 'closed' then sort_value_recently_updated
|
2018-03-17 18:26:18 +05:30
|
|
|
else sort_value_created_date
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def finder
|
2018-11-20 20:47:30 +05:30
|
|
|
@finder ||= issuable_finder_for(finder_type)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def collection_type
|
2019-10-12 21:52:04 +05:30
|
|
|
@collection_type ||= if finder_type <= IssuesFinder
|
2018-03-17 18:26:18 +05:30
|
|
|
'Issue'
|
2019-10-12 21:52:04 +05:30
|
|
|
elsif finder_type <= MergeRequestsFinder
|
2018-03-17 18:26:18 +05:30
|
|
|
'MergeRequest'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2018-03-17 18:26:18 +05:30
|
|
|
def preload_for_collection
|
2019-07-31 22:56:46 +05:30
|
|
|
common_attributes = [:author, :assignees, :labels, :milestone]
|
2018-03-17 18:26:18 +05:30
|
|
|
@preload_for_collection ||= case collection_type
|
|
|
|
when 'Issue'
|
2019-07-31 22:56:46 +05:30
|
|
|
common_attributes + [:project, project: :namespace]
|
2018-03-17 18:26:18 +05:30
|
|
|
when 'MergeRequest'
|
2020-07-28 23:09:34 +05:30
|
|
|
common_attributes + [
|
2021-02-22 17:27:13 +05:30
|
|
|
:target_project, :latest_merge_request_diff, :approvals, :approved_by_users, :reviewers,
|
2020-07-28 23:09:34 +05:30
|
|
|
source_project: :route, head_pipeline: :project, target_project: :namespace
|
|
|
|
]
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
IssuableCollections.prepend_mod_with('IssuableCollections')
|