debian-mirror-gitlab/app/finders/issuable_finder.rb

409 lines
10 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
# IssuableFinder
2014-09-02 18:07:02 +05:30
#
# Used to filter Issues and MergeRequests collections by set of params
#
# Arguments:
# klass - actual class like Issue or MergeRequest
# current_user - which user use
# params:
# scope: 'created-by-me' or 'assigned-to-me' or 'all'
2017-01-15 13:20:01 +05:30
# state: 'opened' or 'closed' or 'all'
2014-09-02 18:07:02 +05:30
# group_id: integer
# project_id: integer
2015-09-11 14:41:01 +05:30
# milestone_title: string
2014-09-02 18:07:02 +05:30
# assignee_id: integer
# search: string
# label_name: string
# sort: string
2017-08-17 22:00:37 +05:30
# non_archived: boolean
# iids: integer[]
2014-09-02 18:07:02 +05:30
#
2015-04-26 12:48:37 +05:30
class IssuableFinder
2017-08-17 22:00:37 +05:30
NONE = '0'.freeze
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
attr_accessor :current_user, :params
2017-01-15 13:20:01 +05:30
def initialize(current_user, params = {})
2014-09-02 18:07:02 +05:30
@current_user = current_user
@params = params
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
def execute
2014-09-02 18:07:02 +05:30
items = init_collection
items = by_scope(items)
items = by_state(items)
items = by_group(items)
items = by_search(items)
items = by_assignee(items)
2015-04-26 12:48:37 +05:30
items = by_author(items)
2016-06-02 11:05:42 +05:30
items = by_due_date(items)
2017-08-17 22:00:37 +05:30
items = by_non_archived(items)
items = by_iids(items)
items = by_milestone(items)
items = by_label(items)
# Filtering by project HAS TO be the last because we use the project IDs yielded by the issuable query thus far
items = by_project(items)
2015-10-24 18:46:33 +05:30
sort(items)
2014-09-02 18:07:02 +05:30
end
2017-01-15 13:20:01 +05:30
def find(*params)
execute.find(*params)
end
def find_by(*params)
execute.find_by(*params)
end
2017-08-17 22:00:37 +05:30
# We often get counts for each state by running a query per state, and
# counting those results. This is typically slower than running one query
# (even if that query is slower than any of the individual state queries) and
# grouping and counting within that query.
#
def count_by_state
count_params = params.merge(state: nil, sort: nil)
labels_count = label_names.any? ? label_names.count : 1
finder = self.class.new(current_user, count_params)
counts = Hash.new(0)
# Searching by label includes a GROUP BY in the query, but ours will be last
# because it is added last. Searching by multiple labels also includes a row
# per issuable, so we have to count those in Ruby - which is bad, but still
# better than performing multiple queries.
#
finder.execute.reorder(nil).group(:state).count.each do |key, value|
counts[Array(key).last.to_sym] += value / labels_count
end
counts[:all] = counts.values.sum
counts[:opened] += counts[:reopened]
counts
end
2017-01-15 13:20:01 +05:30
def find_by!(*params)
execute.find_by!(*params)
end
2015-09-11 14:41:01 +05:30
def group
return @group if defined?(@group)
@group =
if params[:group_id].present?
Group.find(params[:group_id])
else
nil
end
end
2015-10-24 18:46:33 +05:30
def project?
params[:project_id].present?
end
2015-09-11 14:41:01 +05:30
def project
return @project if defined?(@project)
2016-11-24 13:41:30 +05:30
project = Project.find(params[:project_id])
project = nil unless Ability.allowed?(current_user, :"read_#{klass.to_ability_name}", project)
2015-12-23 02:04:40 +05:30
2016-11-24 13:41:30 +05:30
@project = project
2015-10-24 18:46:33 +05:30
end
2017-08-17 22:00:37 +05:30
def projects(items = nil)
2016-11-24 13:41:30 +05:30
return @projects = project if project?
2015-10-24 18:46:33 +05:30
2016-11-24 13:41:30 +05:30
projects =
if current_user && params[:authorized_only].presence && !current_user_related?
current_user.authorized_projects
elsif group
2017-08-17 22:00:37 +05:30
GroupProjectsFinder.new(group: group, current_user: current_user).execute
2016-11-24 13:41:30 +05:30
else
2017-08-17 22:00:37 +05:30
ProjectsFinder.new(current_user: current_user, project_ids_relation: item_project_ids(items)).execute
2016-11-24 13:41:30 +05:30
end
@projects = projects.with_feature_available_for_user(klass, current_user).reorder(nil)
2015-09-11 14:41:01 +05:30
end
def search
params[:search].presence
end
def milestones?
params[:milestone_title].present?
end
2015-10-24 18:46:33 +05:30
def filter_by_no_milestone?
milestones? && params[:milestone_title] == Milestone::None.title
end
2015-09-11 14:41:01 +05:30
def milestones
return @milestones if defined?(@milestones)
@milestones =
2015-10-24 18:46:33 +05:30
if milestones?
scope = Milestone.where(project_id: projects)
scope.where(title: params[:milestone_title])
2015-09-11 14:41:01 +05:30
else
2016-09-13 17:45:13 +05:30
Milestone.none
2015-09-11 14:41:01 +05:30
end
end
2015-10-24 18:46:33 +05:30
def labels?
params[:label_name].present?
end
def filter_by_no_label?
2016-06-02 11:05:42 +05:30
labels? && params[:label_name].include?(Label::None.title)
2015-10-24 18:46:33 +05:30
end
2016-04-02 18:10:28 +05:30
def labels
return @labels if defined?(@labels)
2016-11-03 12:29:30 +05:30
@labels =
if labels? && !filter_by_no_label?
2016-11-24 13:41:30 +05:30
LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute(skip_authorization: true)
2016-11-03 12:29:30 +05:30
else
Label.none
2016-04-02 18:10:28 +05:30
end
end
2017-08-17 22:00:37 +05:30
def assignee_id?
params[:assignee_id].present? && params[:assignee_id] != NONE
end
def assignee_username?
params[:assignee_username].present? && params[:assignee_username] != NONE
end
def no_assignee?
# Assignee_id takes precedence over assignee_username
params[:assignee_id] == NONE || params[:assignee_username] == NONE
2015-09-11 14:41:01 +05:30
end
def assignee
return @assignee if defined?(@assignee)
@assignee =
2017-08-17 22:00:37 +05:30
if assignee_id?
User.find_by(id: params[:assignee_id])
elsif assignee_username?
User.find_by(username: params[:assignee_username])
2015-09-11 14:41:01 +05:30
else
nil
end
end
2017-08-17 22:00:37 +05:30
def author_id?
params[:author_id].present? && params[:author_id] != NONE
end
def author_username?
params[:author_username].present? && params[:author_username] != NONE
end
def no_author?
# author_id takes precedence over author_username
params[:author_id] == NONE || params[:author_username] == NONE
2015-09-11 14:41:01 +05:30
end
def author
return @author if defined?(@author)
@author =
2017-08-17 22:00:37 +05:30
if author_id?
User.find_by(id: params[:author_id])
elsif author_username?
User.find_by(username: params[:author_username])
2015-09-11 14:41:01 +05:30
else
nil
end
end
2014-09-02 18:07:02 +05:30
private
def init_collection
2015-10-24 18:46:33 +05:30
klass.all
2014-09-02 18:07:02 +05:30
end
def by_scope(items)
case params[:scope]
2016-06-02 11:05:42 +05:30
when 'created-by-me', 'authored'
2014-09-02 18:07:02 +05:30
items.where(author_id: current_user.id)
2016-06-02 11:05:42 +05:30
when 'assigned-to-me'
2017-08-17 22:00:37 +05:30
items.assigned_to(current_user)
2014-09-02 18:07:02 +05:30
else
2016-06-02 11:05:42 +05:30
items
2014-09-02 18:07:02 +05:30
end
end
def by_state(items)
2017-01-15 13:20:01 +05:30
case params[:state].to_s
when 'closed'
items.closed
when 'merged'
items.respond_to?(:merged) ? items.merged : items.closed
when 'opened'
items.opened
2014-09-02 18:07:02 +05:30
else
2016-11-03 12:29:30 +05:30
items
2014-09-02 18:07:02 +05:30
end
end
def by_group(items)
2016-06-02 11:05:42 +05:30
# Selection by group is already covered by `by_project` and `projects`
2014-09-02 18:07:02 +05:30
items
end
def by_project(items)
2015-10-24 18:46:33 +05:30
items =
2015-12-23 02:04:40 +05:30
if project?
2017-08-17 22:00:37 +05:30
items.of_projects(projects(items)).references_project
elsif projects(items)
items.merge(projects(items).reorder(nil)).join_project
2015-10-24 18:46:33 +05:30
else
items.none
end
2014-09-02 18:07:02 +05:30
items
end
def by_search(items)
2017-08-17 22:00:37 +05:30
search ? items.full_search(search) : items
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
def by_iids(items)
params[:iids].present? ? items.where(iid: params[:iids]) : items
2014-09-02 18:07:02 +05:30
end
def sort(items)
2015-12-23 02:04:40 +05:30
# Ensure we always have an explicit sort order (instead of inheriting
# multiple orders when combining ActiveRecord::Relation objects).
params[:sort] ? items.sort(params[:sort], excluded_labels: label_names) : items.reorder(id: :desc)
2014-09-02 18:07:02 +05:30
end
def by_assignee(items)
2017-08-17 22:00:37 +05:30
if assignee
items = items.where(assignee_id: assignee.id)
elsif no_assignee?
items = items.where(assignee_id: nil)
elsif assignee_id? || assignee_username? # assignee not found
items = items.none
2015-04-26 12:48:37 +05:30
end
items
end
def by_author(items)
2017-08-17 22:00:37 +05:30
if author
items = items.where(author_id: author.id)
elsif no_author?
items = items.where(author_id: nil)
elsif author_id? || author_username? # author not found
items = items.none
2014-09-02 18:07:02 +05:30
end
items
end
2016-06-02 11:05:42 +05:30
def filter_by_upcoming_milestone?
params[:milestone_title] == Milestone::Upcoming.name
end
2017-08-17 22:00:37 +05:30
def filter_by_started_milestone?
params[:milestone_title] == Milestone::Started.name
end
2015-10-24 18:46:33 +05:30
def by_milestone(items)
if milestones?
if filter_by_no_milestone?
items = items.left_joins_milestones.where(milestone_id: [-1, nil])
2016-06-02 11:05:42 +05:30
elsif filter_by_upcoming_milestone?
2017-08-17 22:00:37 +05:30
upcoming_ids = Milestone.upcoming_ids_by_projects(projects(items))
items = items.left_joins_milestones.where(milestone_id: upcoming_ids)
2017-08-17 22:00:37 +05:30
elsif filter_by_started_milestone?
items = items.left_joins_milestones.where('milestones.start_date <= NOW()')
2015-10-24 18:46:33 +05:30
else
items = items.with_milestone(params[:milestone_title])
2017-08-17 22:00:37 +05:30
items_projects = projects(items)
2015-10-24 18:46:33 +05:30
2017-08-17 22:00:37 +05:30
if items_projects
items = items.where(milestones: { project_id: items_projects })
2015-10-24 18:46:33 +05:30
end
end
end
items
end
2014-09-02 18:07:02 +05:30
def by_label(items)
2015-10-24 18:46:33 +05:30
if labels?
if filter_by_no_label?
2016-06-02 11:05:42 +05:30
items = items.without_label
2015-10-24 18:46:33 +05:30
else
items = items.with_label(label_names, params[:sort])
2017-08-17 22:00:37 +05:30
items_projects = projects(items)
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
if items_projects
label_ids = LabelsFinder.new(current_user, project_ids: items_projects).execute(skip_authorization: true).select(:id)
2016-11-03 12:29:30 +05:30
items = items.where(labels: { id: label_ids })
2015-10-24 18:46:33 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
items
end
2016-06-02 11:05:42 +05:30
def by_due_date(items)
if due_date?
if filter_by_no_due_date?
items = items.without_due_date
elsif filter_by_overdue?
items = items.due_before(Date.today)
elsif filter_by_due_this_week?
items = items.due_between(Date.today.beginning_of_week, Date.today.end_of_week)
elsif filter_by_due_this_month?
items = items.due_between(Date.today.beginning_of_month, Date.today.end_of_month)
end
end
items
end
def filter_by_no_due_date?
due_date? && params[:due_date] == Issue::NoDueDate.name
end
def filter_by_overdue?
due_date? && params[:due_date] == Issue::Overdue.name
end
def filter_by_due_this_week?
due_date? && params[:due_date] == Issue::DueThisWeek.name
end
def filter_by_due_this_month?
due_date? && params[:due_date] == Issue::DueThisMonth.name
end
def due_date?
params[:due_date].present? && klass.column_names.include?('due_date')
end
2016-04-02 18:10:28 +05:30
def label_names
if labels?
params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
else
[]
end
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
def by_non_archived(items)
params[:non_archived].present? ? items.non_archived : items
end
2015-04-26 12:48:37 +05:30
def current_user_related?
params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me'
end
2014-09-02 18:07:02 +05:30
end