diff options
author | Robert Speicher <robert@gitlab.com> | 2018-09-13 17:53:05 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-09-13 17:53:05 +0300 |
commit | c7d1eef671dbf598814a6c2ff1f81b924583ae8a (patch) | |
tree | 23988df1d29e933943858a66821ba7f493976b3e /app/finders | |
parent | ff5d8b635f234241441009e41af7b61f5804b2c2 (diff) | |
parent | 2039c8280db1646845c33d6c5a74e5f23ca6f4de (diff) |
Merge branch 'rubocop-code-reuse' into 'master'
Add RuboCop cops to enforce code reusing rules
See merge request gitlab-org/gitlab-ce!21391
Diffstat (limited to 'app/finders')
35 files changed, 219 insertions, 13 deletions
diff --git a/app/finders/admin/projects_finder.rb b/app/finders/admin/projects_finder.rb index 543bf1a1415..6b805c432f6 100644 --- a/app/finders/admin/projects_finder.rb +++ b/app/finders/admin/projects_finder.rb @@ -6,6 +6,7 @@ class Admin::ProjectsFinder @current_user = current_user end + # rubocop: disable CodeReuse/ActiveRecord def execute items = Project.without_deleted.with_statistics.with_route items = by_namespace_id(items) @@ -19,6 +20,7 @@ class Admin::ProjectsFinder items = items.includes(namespace: [:owner, :route]) sort(items).page(params[:page]) end + # rubocop: enable CodeReuse/ActiveRecord private @@ -26,9 +28,11 @@ class Admin::ProjectsFinder params[:namespace_id].present? ? items.in_namespace(params[:namespace_id]) : items end + # rubocop: disable CodeReuse/ActiveRecord def by_visibilty_level(items) params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items end + # rubocop: enable CodeReuse/ActiveRecord def by_with_push(items) params[:with_push].present? ? items.with_push : items @@ -38,9 +42,11 @@ class Admin::ProjectsFinder params[:abandoned].present? ? items.abandoned : items end + # rubocop: disable CodeReuse/ActiveRecord def by_last_repository_check_failed(items) params[:last_repository_check_failed].present? ? items.where(last_repository_check_failed: true) : items end + # rubocop: enable CodeReuse/ActiveRecord def by_archived(items) if params[:archived] == 'only' diff --git a/app/finders/autocomplete/users_finder.rb b/app/finders/autocomplete/users_finder.rb index b2557469079..e2283f3266e 100644 --- a/app/finders/autocomplete/users_finder.rb +++ b/app/finders/autocomplete/users_finder.rb @@ -44,6 +44,7 @@ module Autocomplete # Returns the users based on the input parameters, as an Array. # # This method is separate so it is easier to extend in EE. + # rubocop: disable CodeReuse/ActiveRecord def limited_users # When changing the order of these method calls, make sure that # reorder_by_name() is called _before_ optionally_search(), otherwise @@ -61,6 +62,7 @@ module Autocomplete .limit(LIMIT) .to_a end + # rubocop: enable CodeReuse/ActiveRecord def prepend_current_user? filter_by_current_user.present? && current_user @@ -70,6 +72,7 @@ module Autocomplete author_id.present? && current_user end + # rubocop: disable CodeReuse/ActiveRecord def find_users if project project.authorized_users.union_with_user(author_id) @@ -81,5 +84,6 @@ module Autocomplete User.none end end + # rubocop: enable CodeReuse/ActiveRecord end end diff --git a/app/finders/concerns/custom_attributes_filter.rb b/app/finders/concerns/custom_attributes_filter.rb index 5bbf9ca242d..022a19b0a7f 100644 --- a/app/finders/concerns/custom_attributes_filter.rb +++ b/app/finders/concerns/custom_attributes_filter.rb @@ -1,4 +1,5 @@ module CustomAttributesFilter + # rubocop: disable CodeReuse/ActiveRecord def by_custom_attributes(items) return items unless params[:custom_attributes].is_a?(Hash) return items unless Ability.allowed?(current_user, :read_custom_attribute) @@ -17,4 +18,5 @@ module CustomAttributesFilter scope.where('EXISTS (?)', custom_attributes.where(key: key, value: value)) end end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/concerns/finder_methods.rb b/app/finders/concerns/finder_methods.rb index 2e905fa5750..ce0ec214b8a 100644 --- a/app/finders/concerns/finder_methods.rb +++ b/app/finders/concerns/finder_methods.rb @@ -1,11 +1,15 @@ module FinderMethods + # rubocop: disable CodeReuse/ActiveRecord def find_by!(*args) raise_not_found_unless_authorized execute.find_by!(*args) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def find_by(*args) if_authorized execute.find_by(*args) end + # rubocop: enable CodeReuse/ActiveRecord def find(*args) raise_not_found_unless_authorized model.find(*args) diff --git a/app/finders/concerns/finder_with_cross_project_access.rb b/app/finders/concerns/finder_with_cross_project_access.rb index 92bf98d7cd2..f70a0b60864 100644 --- a/app/finders/concerns/finder_with_cross_project_access.rb +++ b/app/finders/concerns/finder_with_cross_project_access.rb @@ -14,6 +14,7 @@ module FinderWithCrossProjectAccess end override :execute + # rubocop: disable CodeReuse/ActiveRecord def execute(*args) check = Gitlab::CrossProjectAccess.find_check(self) original = super @@ -27,6 +28,7 @@ module FinderWithCrossProjectAccess original end end + # rubocop: enable CodeReuse/ActiveRecord # We can skip the cross project check for finding indivitual records. # this would be handled by the `can?(:read_*, result)` call in `FinderMethods` diff --git a/app/finders/contributed_projects_finder.rb b/app/finders/contributed_projects_finder.rb index a685719555c..1447def0cdf 100644 --- a/app/finders/contributed_projects_finder.rb +++ b/app/finders/contributed_projects_finder.rb @@ -10,11 +10,13 @@ class ContributedProjectsFinder < UnionFinder # visible by this user. # # Returns an ActiveRecord::Relation. + # rubocop: disable CodeReuse/ActiveRecord def execute(current_user = nil) segments = all_projects(current_user) find_union(segments, Project).includes(:namespace).order_id_desc end + # rubocop: enable CodeReuse/ActiveRecord private diff --git a/app/finders/environments_finder.rb b/app/finders/environments_finder.rb index a59f8c1efa3..95af6214c7d 100644 --- a/app/finders/environments_finder.rb +++ b/app/finders/environments_finder.rb @@ -5,6 +5,7 @@ class EnvironmentsFinder @project, @current_user, @params = project, current_user, params end + # rubocop: disable CodeReuse/ActiveRecord def execute deployments = project.deployments deployments = @@ -42,6 +43,7 @@ class EnvironmentsFinder environments end + # rubocop: enable CodeReuse/ActiveRecord private diff --git a/app/finders/events_finder.rb b/app/finders/events_finder.rb index 8676925a540..a8d4f56a84c 100644 --- a/app/finders/events_finder.rb +++ b/app/finders/events_finder.rb @@ -36,32 +36,42 @@ class EventsFinder private + # rubocop: disable CodeReuse/ActiveRecord def by_current_user_access(events) - events.merge(ProjectsFinder.new(current_user: current_user).execute) + events.merge(ProjectsFinder.new(current_user: current_user).execute) # rubocop: disable CodeReuse/Finder .joins(:project) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_action(events) return events unless Event::ACTIONS[params[:action]] events.where(action: Event::ACTIONS[params[:action]]) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_target_type(events) return events unless Event::TARGET_TYPES[params[:target_type]] events.where(target_type: Event::TARGET_TYPES[params[:target_type]]) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_created_at_before(events) return events unless params[:before] events.where('events.created_at < ?', params[:before].beginning_of_day) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_created_at_after(events) return events unless params[:after] events.where('events.created_at > ?', params[:after].end_of_day) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/fork_projects_finder.rb b/app/finders/fork_projects_finder.rb index 28d1b31868e..8713d43136f 100644 --- a/app/finders/fork_projects_finder.rb +++ b/app/finders/fork_projects_finder.rb @@ -1,6 +1,8 @@ class ForkProjectsFinder < ProjectsFinder + # rubocop: disable CodeReuse/ActiveRecord def initialize(project, params: {}, current_user: nil) project_ids = project.forks.includes(:creator).select(:id) super(params: params, current_user: current_user, project_ids_relation: project_ids) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/group_descendants_finder.rb b/app/finders/group_descendants_finder.rb index 2300b7fd114..15060a99930 100644 --- a/app/finders/group_descendants_finder.rb +++ b/app/finders/group_descendants_finder.rb @@ -61,12 +61,16 @@ class GroupDescendantsFinder end def direct_child_groups + # rubocop: disable CodeReuse/Finder GroupsFinder.new(current_user, parent: parent_group, all_available: true).execute + # rubocop: enable CodeReuse/Finder end + # rubocop: disable CodeReuse/ActiveRecord def all_visible_descendant_groups + # rubocop: disable CodeReuse/Finder groups_table = Group.arel_table visible_to_user = groups_table[:visibility_level] .in(Gitlab::VisibilityLevel.levels_for_user(current_user)) @@ -84,7 +88,9 @@ class GroupDescendantsFinder hierarchy_for_parent .descendants .where(visible_to_user) + # rubocop: enable CodeReuse/Finder end + # rubocop: enable CodeReuse/ActiveRecord def subgroups_matching_filter all_visible_descendant_groups @@ -101,24 +107,29 @@ class GroupDescendantsFinder # # So when searching 'project', on the 'subgroup' page we want to preload # 'nested-group' but not 'subgroup' or 'root' + # rubocop: disable CodeReuse/ActiveRecord def ancestors_of_groups(base_for_ancestors) group_ids = base_for_ancestors.except(:select, :sort).select(:id) Gitlab::GroupHierarchy.new(Group.where(id: group_ids)) .base_and_ancestors(upto: parent_group.id) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def ancestors_of_filtered_projects projects_to_load_ancestors_of = projects.where.not(namespace: parent_group) groups_to_load_ancestors_of = Group.where(id: projects_to_load_ancestors_of.select(:namespace_id)) ancestors_of_groups(groups_to_load_ancestors_of) .with_selects_for_list(archived: params[:archived]) end + # rubocop: enable CodeReuse/ActiveRecord def ancestors_of_filtered_subgroups ancestors_of_groups(subgroups) .with_selects_for_list(archived: params[:archived]) end + # rubocop: disable CodeReuse/ActiveRecord def subgroups return Group.none unless Group.supports_nested_groups? @@ -132,22 +143,29 @@ class GroupDescendantsFinder groups.with_selects_for_list(archived: params[:archived]).order_by(sort) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/Finder def direct_child_projects GroupProjectsFinder.new(group: parent_group, current_user: current_user, params: params, options: { only_owned: true }) .execute end + # rubocop: enable CodeReuse/Finder # Finds all projects nested under `parent_group` or any of its descendant # groups + # rubocop: disable CodeReuse/ActiveRecord def projects_matching_filter + # rubocop: disable CodeReuse/Finder projects_nested_in_group = Project.where(namespace_id: hierarchy_for_parent.base_and_descendants.select(:id)) params_with_search = params.merge(search: params[:filter]) ProjectsFinder.new(params: params_with_search, current_user: current_user, project_ids_relation: projects_nested_in_group).execute + # rubocop: enable CodeReuse/Finder end + # rubocop: enable CodeReuse/ActiveRecord def projects projects = if params[:filter] @@ -163,7 +181,9 @@ class GroupDescendantsFinder params.fetch(:sort, 'id_asc') end + # rubocop: disable CodeReuse/ActiveRecord def hierarchy_for_parent @hierarchy ||= Gitlab::GroupHierarchy.new(Group.where(id: parent_group.id)) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/group_finder.rb b/app/finders/group_finder.rb index 24c84d2d1aa..8fde3a00af7 100644 --- a/app/finders/group_finder.rb +++ b/app/finders/group_finder.rb @@ -5,6 +5,7 @@ class GroupFinder @current_user = current_user end + # rubocop: disable CodeReuse/ActiveRecord def execute(*params) group = Group.find_by(*params) @@ -14,4 +15,5 @@ class GroupFinder nil end end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/group_members_finder.rb b/app/finders/group_members_finder.rb index 2a656c0d31c..cab1ff8fb98 100644 --- a/app/finders/group_members_finder.rb +++ b/app/finders/group_members_finder.rb @@ -3,6 +3,7 @@ class GroupMembersFinder @group = group end + # rubocop: disable CodeReuse/ActiveRecord def execute(include_descendants: false) group_members = @group.members wheres = [] @@ -29,4 +30,5 @@ class GroupMembersFinder GroupMember.where(wheres.join(' OR ')) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/group_projects_finder.rb b/app/finders/group_projects_finder.rb index b6bdb2b7b0f..52a347fa598 100644 --- a/app/finders/group_projects_finder.rb +++ b/app/finders/group_projects_finder.rb @@ -82,6 +82,7 @@ class GroupProjectsFinder < ProjectsFinder options.fetch(:include_subgroups, false) end + # rubocop: disable CodeReuse/ActiveRecord def owned_projects if include_subgroups? Project.where(namespace_id: group.self_and_descendants.select(:id)) @@ -89,6 +90,7 @@ class GroupProjectsFinder < ProjectsFinder group.projects end end + # rubocop: enable CodeReuse/ActiveRecord def shared_projects group.shared_projects diff --git a/app/finders/groups_finder.rb b/app/finders/groups_finder.rb index 0eeba1d2428..436abb332ff 100644 --- a/app/finders/groups_finder.rb +++ b/app/finders/groups_finder.rb @@ -38,6 +38,7 @@ class GroupsFinder < UnionFinder attr_reader :current_user, :params + # rubocop: disable CodeReuse/ActiveRecord def all_groups return [owned_groups] if params[:owned] return [groups_with_min_access_level] if min_access_level? @@ -49,6 +50,7 @@ class GroupsFinder < UnionFinder groups << Group.none if groups.empty? groups end + # rubocop: enable CodeReuse/ActiveRecord def groups_for_ancestors current_user.authorized_groups @@ -58,6 +60,7 @@ class GroupsFinder < UnionFinder current_user.groups end + # rubocop: disable CodeReuse/ActiveRecord def groups_with_min_access_level groups = current_user .groups @@ -67,16 +70,21 @@ class GroupsFinder < UnionFinder .new(groups) .base_and_descendants end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_parent(groups) return groups unless params[:parent] groups.where(parent: params[:parent]) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def owned_groups current_user&.owned_groups || Group.none end + # rubocop: enable CodeReuse/ActiveRecord def include_public_groups? current_user.nil? || all_available? diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index 372e2a96c2c..3308fd6d697 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -109,6 +109,7 @@ class IssuableFinder # (even if that query is slower than any of the individual state queries) and # grouping and counting within that query. # + # rubocop: disable CodeReuse/ActiveRecord def count_by_state count_params = params.merge(state: nil, sort: nil) finder = self.class.new(current_user, count_params) @@ -132,6 +133,7 @@ class IssuableFinder counts.with_indifferent_access end + # rubocop: enable CodeReuse/ActiveRecord def group return @group if defined?(@group) @@ -157,6 +159,7 @@ class IssuableFinder @project = project end + # rubocop: disable CodeReuse/ActiveRecord def projects(items = nil) return @projects = project if project? @@ -165,13 +168,14 @@ class IssuableFinder current_user.authorized_projects elsif group finder_options = { include_subgroups: params[:include_subgroups], only_owned: true } - GroupProjectsFinder.new(group: group, current_user: current_user, options: finder_options).execute + GroupProjectsFinder.new(group: group, current_user: current_user, options: finder_options).execute # rubocop: disable CodeReuse/Finder else - ProjectsFinder.new(current_user: current_user).execute + ProjectsFinder.new(current_user: current_user).execute # rubocop: disable CodeReuse/Finder end @projects = projects.with_feature_available_for_user(klass, current_user).reorder(nil) end + # rubocop: enable CodeReuse/ActiveRecord def search params[:search].presence @@ -185,6 +189,7 @@ class IssuableFinder milestones? && params[:milestone_title] == Milestone::None.title end + # rubocop: disable CodeReuse/ActiveRecord def milestones return @milestones if defined?(@milestones) @@ -200,11 +205,12 @@ class IssuableFinder search_params = { title: params[:milestone_title], project_ids: project_id, group_ids: group_id } - MilestonesFinder.new(search_params).execute + MilestonesFinder.new(search_params).execute # rubocop: disable CodeReuse/Finder else Milestone.none end end + # rubocop: enable CodeReuse/ActiveRecord def labels? params[:label_name].present? @@ -214,16 +220,18 @@ class IssuableFinder labels? && params[:label_name].include?(Label::None.title) end + # rubocop: disable CodeReuse/ActiveRecord def labels return @labels if defined?(@labels) @labels = if labels? && !filter_by_no_label? - LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute(skip_authorization: true) + LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute(skip_authorization: true) # rubocop: disable CodeReuse/Finder else Label.none end end + # rubocop: enable CodeReuse/ActiveRecord def assignee_id? params[:assignee_id].present? && params[:assignee_id] != NONE @@ -238,6 +246,7 @@ class IssuableFinder params[:assignee_id] == NONE || params[:assignee_username] == NONE end + # rubocop: disable CodeReuse/ActiveRecord def assignee return @assignee if defined?(@assignee) @@ -250,6 +259,7 @@ class IssuableFinder nil end end + # rubocop: enable CodeReuse/ActiveRecord def author_id? params[:author_id].present? && params[:author_id] != NONE @@ -264,6 +274,7 @@ class IssuableFinder params[:author_id] == NONE || params[:author_username] == NONE end + # rubocop: disable CodeReuse/ActiveRecord def author return @author if defined?(@author) @@ -276,6 +287,7 @@ class IssuableFinder nil end end + # rubocop: enable CodeReuse/ActiveRecord private @@ -283,6 +295,7 @@ class IssuableFinder klass.all end + # rubocop: disable CodeReuse/ActiveRecord def by_scope(items) return items.none if current_user_related? && !current_user @@ -295,6 +308,7 @@ class IssuableFinder items end end + # rubocop: enable CodeReuse/ActiveRecord def by_updated_at(items) items = items.updated_after(params[:updated_after]) if params[:updated_after].present? @@ -303,6 +317,7 @@ class IssuableFinder items end + # rubocop: disable CodeReuse/ActiveRecord def by_state(items) case params[:state].to_s when 'closed' @@ -317,12 +332,14 @@ class IssuableFinder items end end + # rubocop: enable CodeReuse/ActiveRecord def by_group(items) # Selection by group is already covered by `by_project` and `projects` items end + # rubocop: disable CodeReuse/ActiveRecord def by_project(items) items = if project? @@ -335,6 +352,7 @@ class IssuableFinder items end + # rubocop: enable CodeReuse/ActiveRecord def use_cte_for_search? return false unless search @@ -343,6 +361,7 @@ class IssuableFinder params[:use_cte_for_search] end + # rubocop: disable CodeReuse/ActiveRecord def by_search(items) return items unless search @@ -355,17 +374,23 @@ class IssuableFinder items.full_search(search) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_iids(items) params[:iids].present? ? items.where(iid: params[:iids]) : items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def sort(items) # Ensure we always have an explicit sort order (instead of inheriting # multiple orders when combining ActiveRecord::Relation objects). params[:sort] ? items.sort_by_attribute(params[:sort], excluded_labels: label_names) : items.reorder(id: :desc) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_assignee(items) if assignee items = items.where(assignee_id: assignee.id) @@ -377,7 +402,9 @@ class IssuableFinder items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_author(items) if author items = items.where(author_id: author.id) @@ -389,6 +416,7 @@ class IssuableFinder items end + # rubocop: enable CodeReuse/ActiveRecord def filter_by_upcoming_milestone? params[:milestone_title] == Milestone::Upcoming.name @@ -398,6 +426,7 @@ class IssuableFinder params[:milestone_title] == Milestone::Started.name end + # rubocop: disable CodeReuse/ActiveRecord def by_milestone(items) if milestones? if filter_by_no_milestone? @@ -414,6 +443,7 @@ class IssuableFinder items end + # rubocop: enable CodeReuse/ActiveRecord def by_label(items) return items unless labels? diff --git a/app/finders/issues_finder.rb b/app/finders/issues_finder.rb index 24a6b9349a0..c347ef1ec19 100644 --- a/app/finders/issues_finder.rb +++ b/app/finders/issues_finder.rb @@ -29,10 +29,13 @@ class IssuesFinder < IssuableFinder @scalar_params ||= super + [:due_date] end + # rubocop: disable CodeReuse/ActiveRecord def klass Issue.includes(:author) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def with_confidentiality_access_check return Issue.all if user_can_see_all_confidential_issues? return Issue.where('issues.confidential IS NOT TRUE') if user_cannot_see_confidential_issues? @@ -46,6 +49,7 @@ class IssuesFinder < IssuableFinder user_id: current_user.id, project_ids: current_user.authorized_projects(CONFIDENTIAL_ACCESS_LEVEL).select(:id)) end + # rubocop: enable CodeReuse/ActiveRecord private @@ -125,6 +129,7 @@ class IssuesFinder < IssuableFinder current_user.blank? end + # rubocop: disable CodeReuse/ActiveRecord def by_assignee(items) if assignee items.assigned_to(assignee) @@ -136,4 +141,5 @@ class IssuesFinder < IssuableFinder items end end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/labels_finder.rb b/app/finders/labels_finder.rb index 8418577dab2..17c55f8db50 100644 --- a/app/finders/labels_finder.rb +++ b/app/finders/labels_finder.rb @@ -10,6 +10,7 @@ class LabelsFinder < UnionFinder @params = params end + # rubocop: disable CodeReuse/ActiveRecord def execute(skip_authorization: false) @skip_authorization = skip_authorization items = find_union(label_ids, Label) || Label.none @@ -17,11 +18,13 @@ class LabelsFinder < UnionFinder items = by_search(items) sort(items) end + # rubocop: enable CodeReuse/ActiveRecord private attr_reader :current_user, :params, :skip_authorization + # rubocop: disable CodeReuse/ActiveRecord def label_ids label_ids = [] @@ -52,7 +55,9 @@ class LabelsFinder < UnionFinder label_ids end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def sort(items) if params[:sort] items.order_by(params[:sort]) @@ -60,13 +65,16 @@ class LabelsFinder < UnionFinder items.reorder(title: :asc) end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def with_title(items) return items if title.nil? return items.none if title.blank? items.where(title: title) end + # rubocop: enable CodeReuse/ActiveRecord def by_search(labels) return labels unless search? @@ -138,13 +146,14 @@ class LabelsFinder < UnionFinder @project end + # rubocop: disable CodeReuse/ActiveRecord def projects return @projects if defined?(@projects) @projects = if skip_authorization Project.all else - ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute + ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute # rubocop: disable CodeReuse/Finder end @projects = @projects.in_namespace(params[:group_id]) if group? @@ -153,6 +162,7 @@ class LabelsFinder < UnionFinder @projects end + # rubocop: enable CodeReuse/ActiveRecord def authorized_to_read_labels?(label_parent) return true if skip_authorization diff --git a/app/finders/members_finder.rb b/app/finders/members_finder.rb index 4c893ae2de6..58e3606692d 100644 --- a/app/finders/members_finder.rb +++ b/app/finders/members_finder.rb @@ -7,12 +7,13 @@ class MembersFinder @group = project.group end + # rubocop: disable CodeReuse/ActiveRecord def execute(include_descendants: false) project_members = project.project_members project_members = project_members.non_invite unless can?(current_user, :admin_project, project) if group - group_members = GroupMembersFinder.new(group).execute(include_descendants: include_descendants) + group_members = GroupMembersFinder.new(group).execute(include_descendants: include_descendants) # rubocop: disable CodeReuse/Finder group_members = group_members.non_invite union = Gitlab::SQL::Union.new([project_members, group_members], remove_duplicates: false) @@ -24,6 +25,7 @@ class MembersFinder project_members end end + # rubocop: enable CodeReuse/ActiveRecord def can?(*args) Ability.allowed?(*args) diff --git a/app/finders/merge_request_target_project_finder.rb b/app/finders/merge_request_target_project_finder.rb index 188ec447a94..66d4c4018f3 100644 --- a/app/finders/merge_request_target_project_finder.rb +++ b/app/finders/merge_request_target_project_finder.rb @@ -8,6 +8,7 @@ class MergeRequestTargetProjectFinder @source_project = source_project end + # rubocop: disable CodeReuse/ActiveRecord def execute if @source_project.fork_network @source_project.fork_network.projects @@ -18,4 +19,5 @@ class MergeRequestTargetProjectFinder Project.where(id: source_project) end end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/merge_requests_finder.rb b/app/finders/merge_requests_finder.rb index 40089c082c1..c6403f8f25d 100644 --- a/app/finders/merge_requests_finder.rb +++ b/app/finders/merge_requests_finder.rb @@ -41,19 +41,23 @@ class MergeRequestsFinder < IssuableFinder @source_branch ||= params[:source_branch].presence end + # rubocop: disable CodeReuse/ActiveRecord def by_source_branch(items) return items unless source_branch items.where(source_branch: source_branch) end + # rubocop: enable CodeReuse/ActiveRecord def target_branch @target_branch ||= params[:target_branch].presence end + # rubocop: disable CodeReuse/ActiveRecord def by_target_branch(items) return items unless target_branch items.where(target_branch: target_branch) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/milestones_finder.rb b/app/finders/milestones_finder.rb index f5d2b9f253a..7dc2a5b0b66 100644 --- a/app/finders/milestones_finder.rb +++ b/app/finders/milestones_finder.rb @@ -18,6 +18,7 @@ class MilestonesFinder @params = params end + # rubocop: disable CodeReuse/ActiveRecord def execute return Milestone.none if project_ids.empty? && group_ids.empty? @@ -28,6 +29,7 @@ class MilestonesFinder order(items) end + # rubocop: enable CodeReuse/ActiveRecord private @@ -35,6 +37,7 @@ class MilestonesFinder items.for_projects_and_groups(project_ids, group_ids) end + # rubocop: disable CodeReuse/ActiveRecord def by_title(items) if params[:title] items.where(title: params[:title]) @@ -42,13 +45,16 @@ class MilestonesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord def by_state(items) Milestone.filter_by_state(items, params[:state]) end + # rubocop: disable CodeReuse/ActiveRecord def order(items) order_statement = Gitlab::Database.nulls_last_order('due_date', 'ASC') items.reorder(order_statement).order('title ASC') end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/notes_finder.rb b/app/finders/notes_finder.rb index 9b7a35fb3b5..580984329a1 100644 --- a/app/finders/notes_finder.rb +++ b/app/finders/notes_finder.rb @@ -65,21 +65,23 @@ class NotesFinder @params[:target_type] end + # rubocop: disable CodeReuse/ActiveRecord def notes_of_any_type types = %w(commit issue merge_request snippet) note_relations = types.map { |t| notes_for_type(t) } note_relations.map! { |notes| search(notes) } - UnionFinder.new.find_union(note_relations, Note.includes(:author)) + UnionFinder.new.find_union(note_relations, Note.includes(:author)) # rubocop: disable CodeReuse/Finder end + # rubocop: enable CodeReuse/ActiveRecord def noteables_for_type(noteable_type) case noteable_type when "issue" - IssuesFinder.new(@current_user, project_id: @project.id).execute + IssuesFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder when "merge_request" - MergeRequestsFinder.new(@current_user, project_id: @project.id).execute + MergeRequestsFinder.new(@current_user, project_id: @project.id).execute # rubocop: disable CodeReuse/Finder when "snippet", "project_snippet" - SnippetsFinder.new(@current_user, project: @project).execute + SnippetsFinder.new(@current_user, project: @project).execute # rubocop: disable CodeReuse/Finder when "personal_snippet" PersonalSnippet.all else @@ -87,6 +89,7 @@ class NotesFinder end end + # rubocop: disable CodeReuse/ActiveRecord def notes_for_type(noteable_type) if noteable_type == "commit" if Ability.allowed?(@current_user, :download_code, @project) @@ -99,6 +102,7 @@ class NotesFinder @project.notes.where(noteable_type: finder.base_class.name, noteable_id: finder.reorder(nil)) end end + # rubocop: enable CodeReuse/ActiveRecord def notes_on_target if target.respond_to?(:related_notes) diff --git a/app/finders/personal_access_tokens_finder.rb b/app/finders/personal_access_tokens_finder.rb index d975f354a88..6b610ffc687 100644 --- a/app/finders/personal_access_tokens_finder.rb +++ b/app/finders/personal_access_tokens_finder.rb @@ -16,11 +16,13 @@ class PersonalAccessTokensFinder private + # rubocop: disable CodeReuse/ActiveRecord def by_user(tokens) return tokens unless @params[:user] tokens.where(user: @params[:user]) end + # rubocop: enable CodeReuse/ActiveRecord def by_impersonation(tokens) case @params[:impersonation] diff --git a/app/finders/personal_projects_finder.rb b/app/finders/personal_projects_finder.rb index a56a3a1e1a9..16e4e060786 100644 --- a/app/finders/personal_projects_finder.rb +++ b/app/finders/personal_projects_finder.rb @@ -15,6 +15,7 @@ class PersonalProjectsFinder < UnionFinder # min_access_level: integer # # Returns an ActiveRecord::Relation. + # rubocop: disable CodeReuse/ActiveRecord def execute(current_user = nil) return Project.none unless can?(current_user, :read_user_profile, @user) @@ -22,6 +23,7 @@ class PersonalProjectsFinder < UnionFinder find_union(segments, Project).includes(:namespace).order_updated_desc end + # rubocop: enable CodeReuse/ActiveRecord private diff --git a/app/finders/pipeline_schedules_finder.rb b/app/finders/pipeline_schedules_finder.rb index 2ac4289fbbe..d131e1659c1 100644 --- a/app/finders/pipeline_schedules_finder.rb +++ b/app/finders/pipeline_schedules_finder.rb @@ -6,6 +6,7 @@ class PipelineSchedulesFinder @pipeline_schedules = project.pipeline_schedules end + # rubocop: disable CodeReuse/ActiveRecord def execute(scope: nil) scoped_schedules = case scope @@ -19,4 +20,5 @@ class PipelineSchedulesFinder scoped_schedules.order(id: :desc) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/pipelines_finder.rb b/app/finders/pipelines_finder.rb index a99a889a7e9..b03d730f975 100644 --- a/app/finders/pipelines_finder.rb +++ b/app/finders/pipelines_finder.rb @@ -10,6 +10,7 @@ class PipelinesFinder @params = params end + # rubocop: disable CodeReuse/ActiveRecord def execute unless Ability.allowed?(current_user, :read_pipeline, project) return Ci::Pipeline.none @@ -25,16 +26,21 @@ class PipelinesFinder items = by_yaml_errors(items) sort_items(items) end + # rubocop: enable CodeReuse/ActiveRecord private + # rubocop: disable CodeReuse/ActiveRecord def ids_for_ref(refs) pipelines.where(ref: refs).group(:ref).select('max(id)') end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def from_ids(ids) pipelines.unscoped.where(id: ids) end + # rubocop: enable CodeReuse/ActiveRecord def branches project.repository.branch_names @@ -61,12 +67,15 @@ class PipelinesFinder end end + # rubocop: disable CodeReuse/ActiveRecord def by_status(items) return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) items.where(status: params[:status]) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_ref(items) if params[:ref].present? items.where(ref: params[:ref]) @@ -74,7 +83,9 @@ class PipelinesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_sha(items) if params[:sha].present? items.where(sha: params[:sha]) @@ -82,7 +93,9 @@ class PipelinesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_name(items) if params[:name].present? items.joins(:user).where(users: { name: params[:name] }) @@ -90,7 +103,9 @@ class PipelinesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_username(items) if params[:username].present? items.joins(:user).where(users: { username: params[:username] }) @@ -98,7 +113,9 @@ class PipelinesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_yaml_errors(items) case Gitlab::Utils.to_boolean(params[:yaml_errors]) when true @@ -109,7 +126,9 @@ class PipelinesFinder items end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def sort_items(items) order_by = if ALLOWED_INDEXED_COLUMNS.include?(params[:order_by]) params[:order_by] @@ -125,4 +144,5 @@ class PipelinesFinder items.order(order_by => sort) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index 0398ccce93b..0f2fdf317a4 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -35,7 +35,7 @@ class ProjectsFinder < UnionFinder user = params.delete(:user) collection = if user - PersonalProjectsFinder.new(user, finder_params).execute(current_user) + PersonalProjectsFinder.new(user, finder_params).execute(current_user) # rubocop: disable CodeReuse/Finder else init_collection end @@ -64,6 +64,7 @@ class ProjectsFinder < UnionFinder end end + # rubocop: disable CodeReuse/ActiveRecord def collection_with_user if owned_projects? current_user.owned_projects @@ -77,8 +78,10 @@ class ProjectsFinder < UnionFinder end end end + # rubocop: enable CodeReuse/ActiveRecord # Builds a collection for an anonymous user. + # rubocop: disable CodeReuse/ActiveRecord def collection_without_user if private_only? || owned_projects? || min_access_level? Project.none @@ -86,6 +89,7 @@ class ProjectsFinder < UnionFinder Project.public_to_user end end + # rubocop: enable CodeReuse/ActiveRecord def owned_projects? params[:owned].present? @@ -99,9 +103,11 @@ class ProjectsFinder < UnionFinder params[:min_access_level].present? end + # rubocop: disable CodeReuse/ActiveRecord def by_ids(items) project_ids_relation ? items.where(id: project_ids_relation) : items end + # rubocop: enable CodeReuse/ActiveRecord def union(items) find_union(items, Project).with_route @@ -119,9 +125,11 @@ class ProjectsFinder < UnionFinder params[:trending].present? ? items.trending : items end + # rubocop: disable CodeReuse/ActiveRecord def by_visibilty_level(items) params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items end + # rubocop: enable CodeReuse/ActiveRecord def by_tags(items) params[:tag].present? ? items.tagged_with(params[:tag]) : items diff --git a/app/finders/runner_jobs_finder.rb b/app/finders/runner_jobs_finder.rb index 52340f94523..d4f2474197f 100644 --- a/app/finders/runner_jobs_finder.rb +++ b/app/finders/runner_jobs_finder.rb @@ -14,9 +14,11 @@ class RunnerJobsFinder private + # rubocop: disable CodeReuse/ActiveRecord def by_status(items) return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) items.where(status: params[:status]) end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb index 9d3772d7541..32da3e8f404 100644 --- a/app/finders/snippets_finder.rb +++ b/app/finders/snippets_finder.rb @@ -41,6 +41,7 @@ class SnippetsFinder < UnionFinder end end + # rubocop: disable CodeReuse/ActiveRecord def authorized_snippets_from_project if can?(current_user, :read_project_snippet, project) if project.team.member?(current_user) @@ -52,7 +53,9 @@ class SnippetsFinder < UnionFinder Snippet.none end end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def authorized_snippets # This query was intentionally converted to a raw one to get it work in Rails 5.0. # In Rails 5.0 and 5.1 there's a bug: https://github.com/rails/arel/issues/531 @@ -60,6 +63,7 @@ class SnippetsFinder < UnionFinder Snippet.where("#{feature_available_projects} OR #{not_project_related}") .public_or_visible_to_user(current_user) end + # rubocop: enable CodeReuse/ActiveRecord # Returns a collection of projects that is either public or visible to the # logged in user. @@ -68,6 +72,7 @@ class SnippetsFinder < UnionFinder # the query, e.g. to apply .with_feature_available_for_user on top of it. # This is useful for performance as we can stick those additional filters # at the bottom of e.g. the UNION. + # rubocop: disable CodeReuse/ActiveRecord def projects_for_user return yield(Project.public_to_user) unless current_user @@ -86,6 +91,7 @@ class SnippetsFinder < UnionFinder Project.from("(#{union.to_sql}) AS #{Project.table_name}") end + # rubocop: enable CodeReuse/ActiveRecord def feature_available_projects # Don't return any project related snippets if the user cannot read cross project @@ -109,6 +115,7 @@ class SnippetsFinder < UnionFinder Snippet.arel_table end + # rubocop: disable CodeReuse/ActiveRecord def by_visibility(items) visibility = params[:visibility] || visibility_from_scope @@ -116,12 +123,15 @@ class SnippetsFinder < UnionFinder items.where(visibility_level: visibility) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_author(items) return items unless params[:author] items.where(author_id: params[:author].id) end + # rubocop: enable CodeReuse/ActiveRecord def visibility_from_scope case params[:scope].to_s diff --git a/app/finders/template_finder.rb b/app/finders/template_finder.rb index ea0251bffb6..7cdc7a32acc 100644 --- a/app/finders/template_finder.rb +++ b/app/finders/template_finder.rb @@ -8,7 +8,7 @@ class TemplateFinder class << self def build(type, params = {}) if type == :licenses - LicenseTemplateFinder.new(params) + LicenseTemplateFinder.new(params) # rubocop: disable CodeReuse/Finder else new(type, params) end diff --git a/app/finders/todos_finder.rb b/app/finders/todos_finder.rb index 6e9c8ea6fde..6481893a195 100644 --- a/app/finders/todos_finder.rb +++ b/app/finders/todos_finder.rb @@ -120,6 +120,7 @@ class TodosFinder params[:sort] ? items.sort_by_attribute(params[:sort]) : items.order_id_desc end + # rubocop: disable CodeReuse/ActiveRecord def by_action(items) if action? items = items.where(action: to_action_id) @@ -127,7 +128,9 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_action_id(items) if action_id? items = items.where(action: action_id) @@ -135,7 +138,9 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_author(items) if author? items = items.where(author_id: author.try(:id)) @@ -143,7 +148,9 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_project(items) if project? items = items.where(project: project) @@ -151,7 +158,9 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_group(items) if group? groups = group.self_and_descendants @@ -164,6 +173,7 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord def by_state(items) case params[:state].to_s @@ -174,6 +184,7 @@ class TodosFinder end end + # rubocop: disable CodeReuse/ActiveRecord def by_type(items) if type? items = items.where(target_type: type) @@ -181,4 +192,5 @@ class TodosFinder items end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/union_finder.rb b/app/finders/union_finder.rb index 33cd1a491f3..91b845871a8 100644 --- a/app/finders/union_finder.rb +++ b/app/finders/union_finder.rb @@ -1,4 +1,5 @@ class UnionFinder + # rubocop: disable CodeReuse/ActiveRecord def find_union(segments, klass) if segments.length > 1 union = Gitlab::SQL::Union.new(segments.map { |s| s.select(:id) }) @@ -8,4 +9,5 @@ class UnionFinder segments.first end end + # rubocop: enable CodeReuse/ActiveRecord end diff --git a/app/finders/user_finder.rb b/app/finders/user_finder.rb index 484a93c9873..815388c894e 100644 --- a/app/finders/user_finder.rb +++ b/app/finders/user_finder.rb @@ -14,9 +14,11 @@ class UserFinder end # Tries to find a User, returning nil if none could be found. + # rubocop: disable CodeReuse/ActiveRecord def execute User.find_by(id: params[:id]) end + # rubocop: enable CodeReuse/ActiveRecord # Tries to find a User, raising a `ActiveRecord::RecordNotFound` if it could # not be found. diff --git a/app/finders/user_recent_events_finder.rb b/app/finders/user_recent_events_finder.rb index b874f6959c9..7405d6d27d9 100644 --- a/app/finders/user_recent_events_finder.rb +++ b/app/finders/user_recent_events_finder.rb @@ -21,6 +21,7 @@ class UserRecentEventsFinder @params = params end + # rubocop: disable CodeReuse/ActiveRecord def execute return Event.none unless can?(current_user, :read_user_profile, target_user) @@ -29,9 +30,11 @@ class UserRecentEventsFinder .with_associations .limit_recent(LIMIT, params[:offset]) end + # rubocop: enable CodeReuse/ActiveRecord private + # rubocop: disable CodeReuse/ActiveRecord def recent_events(offset) sql = <<~SQL (#{projects}) AS projects_for_join @@ -42,10 +45,13 @@ class UserRecentEventsFinder # Workaround for https://github.com/rails/rails/issues/24193 Event.from([Arel.sql(sql)]) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def target_events Event.where(author: target_user) end + # rubocop: enable CodeReuse/ActiveRecord def projects target_user.project_interactions.to_sql diff --git a/app/finders/users_finder.rb b/app/finders/users_finder.rb index 65824a51919..46672301851 100644 --- a/app/finders/users_finder.rb +++ b/app/finders/users_finder.rb @@ -41,11 +41,13 @@ class UsersFinder private + # rubocop: disable CodeReuse/ActiveRecord def by_username(users) return users unless params[:username] users.where(username: params[:username]) end + # rubocop: enable CodeReuse/ActiveRecord def by_search(users) return users unless params[:search].present? @@ -65,18 +67,22 @@ class UsersFinder users.active end + # rubocop: disable CodeReuse/ActiveRecord def by_external_identity(users) return users unless current_user&.admin? && params[:extern_uid] && params[:provider] users.joins(:identities).merge(Identity.with_extern_uid(params[:provider], params[:extern_uid])) end + # rubocop: enable CodeReuse/ActiveRecord + # rubocop: disable CodeReuse/ActiveRecord def by_external(users) return users = users.where.not(external: true) unless current_user&.admin? return users unless params[:external] users.external end + # rubocop: enable CodeReuse/ActiveRecord def by_2fa(users) case params[:two_factor] |