Commit f735b614 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Created scopes to hold reusable queries

Extracted queries from the finder, and added as scopes in merge-requests
model
parent 050fb309
......@@ -42,37 +42,24 @@ module MergeRequests
# Merge Requests without any approval
def without_approvals(items)
items
.left_outer_joins(:approvals)
.joins('LEFT OUTER JOIN approvals ON approvals.merge_request_id = merge_requests.id')
.where(approvals: { id: nil })
items.without_approvals
end
# Merge Requests with any number of approvals
def with_any_approvals(items)
items.select_from_union([
items.joins(:approvals),
items.joins('INNER JOIN approvals ON approvals.merge_request_id = merge_requests.id')
items.with_approvals
])
end
# Merge Requests approved by given usernames
def find_approved_by_names(items)
find_approved_by_query(items, :username, usernames)
items.approved_by_users_with_usernames(usernames)
end
# Merge Requests approved by given user IDs
def find_approved_by_ids(items)
find_approved_by_query(items, :id, ids)
end
def find_approved_by_query(items, field, values)
items
.joins(:approvals)
.joins(approvals: [:user])
.where(users: { field => values })
.group('merge_requests.id')
.having("COUNT(users.id) = ?", values.size)
items.approved_by_users_with_ids(ids)
end
end
end
......@@ -6,4 +6,6 @@ class Approval < ApplicationRecord
validates :merge_request_id, presence: true
validates :user_id, presence: true, uniqueness: { scope: [:merge_request_id] }
scope :with_user, -> { joins(:user) }
end
......@@ -41,6 +41,23 @@ module EE
delegate :sha, to: :base_pipeline, prefix: :base_pipeline, allow_nil: true
delegate :merge_requests_author_approval?, to: :target_project, allow_nil: true
scope :without_approvals, -> { left_outer_joins(:approvals).where(approvals: { id: nil }) }
scope :with_approvals, -> { joins(:approvals) }
scope :approved_by_users_with_ids, -> (user_ids) do
with_approvals
.merge(Approval.with_user)
.where(users: { id: user_ids })
.group(:id)
.having("COUNT(users.id) = ?", user_ids.size)
end
scope :approved_by_users_with_usernames, -> (usernames) do
with_approvals
.merge(Approval.with_user)
.where(users: { username: usernames })
.group(:id)
.having("COUNT(users.id) = ?", usernames.size)
end
participant :participant_approvers
accepts_nested_attributes_for :approval_rules, allow_destroy: true
......
......@@ -19,7 +19,7 @@ module EE
optional :approver_ids, types: [String, Array], array_none_any: true,
desc: 'Return merge requests which have specified the users with the given IDs as an individual approver'
optional :approved_by_ids, types: [String, Array], array_none_any: true,
desc: 'Return merge requests which have been approved by the specified the users with the given IDs'
desc: 'Return merge requests which have been approved by the specified users with the given IDs'
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment