debian-mirror-gitlab/app/services/boards/issues/list_service.rb

143 lines
4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Boards
module Issues
2018-03-17 18:26:18 +05:30
class ListService < Boards::BaseService
2018-11-18 11:00:15 +05:30
include Gitlab::Utils::StrongMemoize
2020-03-13 15:44:24 +05:30
def self.valid_params
IssuesFinder.valid_params
end
2016-09-13 17:45:13 +05:30
def execute
2020-04-08 14:13:33 +05:30
return fetch_issues.order_closed_date_desc if list&.closed?
2020-03-13 15:44:24 +05:30
fetch_issues.order_by_position_and_priority(with_cte: can_attempt_search_optimization?)
2018-11-18 11:00:15 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def metadata
2019-12-21 20:55:43 +05:30
issues = Issue.arel_table
2018-11-18 11:00:15 +05:30
keys = metadata_fields.keys
2019-12-21 20:55:43 +05:30
# TODO: eliminate need for SQL literal fragment
columns = Arel.sql(metadata_fields.values_at(*keys).join(', '))
results = Issue.where(id: fetch_issues.select(issues[:id])).pluck(columns)
2018-11-18 11:00:15 +05:30
Hash[keys.zip(results.flatten)]
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
private
2018-11-18 11:00:15 +05:30
def metadata_fields
{ size: 'COUNT(*)' }
end
# We memoize the query here since the finder methods we use are quite complex. This does not memoize the result of the query.
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
def fetch_issues
strong_memoize(:fetch_issues) do
issues = IssuesFinder.new(current_user, filter_params).execute
filter(issues).reorder(nil)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-18 11:00:15 +05:30
2018-11-08 19:23:39 +05:30
def filter(issues)
issues = without_board_labels(issues) unless list&.movable? || list&.closed?
issues = with_list_label(issues) if list&.label?
issues
end
2016-11-03 12:29:30 +05:30
def board
2018-03-17 18:26:18 +05:30
@board ||= parent.boards.find(params[:board_id])
2016-11-03 12:29:30 +05:30
end
2016-09-13 17:45:13 +05:30
def list
2017-08-17 22:00:37 +05:30
return @list if defined?(@list)
@list = board.lists.find(params[:id]) if params.key?(:id)
end
2016-09-13 17:45:13 +05:30
def filter_params
2018-03-17 18:26:18 +05:30
set_parent
2016-09-13 17:45:13 +05:30
set_state
2018-05-09 12:01:36 +05:30
set_scope
2018-12-05 23:21:45 +05:30
set_non_archived
2020-03-13 15:44:24 +05:30
set_attempt_search_optimizations
2016-09-13 17:45:13 +05:30
params
end
2018-03-17 18:26:18 +05:30
def set_parent
2018-03-27 19:54:05 +05:30
if parent.is_a?(Group)
params[:group_id] = parent.id
else
params[:project_id] = parent.id
end
2016-09-13 17:45:13 +05:30
end
def set_state
2017-08-17 22:00:37 +05:30
params[:state] = list && list.closed? ? 'closed' : 'opened'
2016-09-13 17:45:13 +05:30
end
2018-05-09 12:01:36 +05:30
def set_scope
params[:include_subgroups] = board.group_board?
end
2018-12-05 23:21:45 +05:30
def set_non_archived
params[:non_archived] = parent.is_a?(Group)
end
2020-03-13 15:44:24 +05:30
def set_attempt_search_optimizations
return unless can_attempt_search_optimization?
if board.group_board?
params[:attempt_group_search_optimizations] = true
else
params[:attempt_project_search_optimizations] = true
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
def board_label_ids
@board_label_ids ||= board.lists.movable.pluck(:label_id)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
def without_board_labels(issues)
return issues unless board_label_ids.any?
2018-11-08 19:23:39 +05:30
issues.where.not('EXISTS (?)', issues_label_links.limit(1))
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def issues_label_links
LabelLink.where("label_links.target_type = 'Issue' AND label_links.target_id = issues.id").where(label_id: board_label_ids)
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
def with_list_label(issues)
2018-11-08 19:23:39 +05:30
issues.where('EXISTS (?)', LabelLink.where("label_links.target_type = 'Issue' AND label_links.target_id = issues.id")
.where("label_links.label_id = ?", list.label_id).limit(1))
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2020-03-13 15:44:24 +05:30
def board_group
board.group_board? ? parent : parent.group
end
def can_attempt_search_optimization?
params[:search].present? &&
2020-04-22 19:07:51 +05:30
Feature.enabled?(:board_search_optimization, board_group, default_enabled: true)
2020-03-13 15:44:24 +05:30
end
2016-09-13 17:45:13 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
Boards::Issues::ListService.prepend_if_ee('EE::Boards::Issues::ListService')