summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-07-07 23:21:09 -0700
committerStan Hu <stanhu@gmail.com>2017-07-08 22:19:34 -0700
commit88c4248ac72ab1e520edddfa184000d59c509e8f (patch)
tree2263d6046632bc0162e7b7c352fff429cf4ce9c9 /lib/api
parent420f6b5474e49e17226415250846e48fe514fe0d (diff)
downloadgitlab-ce-88c4248ac72ab1e520edddfa184000d59c509e8f.tar.gz
Remove remaining N+1 queries in merge requests API with emojis and labels
Closes #34159
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/entities.rb20
-rw-r--r--lib/api/merge_requests.rb8
2 files changed, 23 insertions, 5 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index f4796f311a5..945f2821d72 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -316,10 +316,26 @@ module API
class MergeRequestBasic < ProjectEntity
expose :target_branch, :source_branch
- expose :upvotes, :downvotes
+ expose :upvotes do |merge_request, options|
+ if options[:issuable_metadata]
+ options[:issuable_metadata][merge_request.id].upvotes
+ else
+ merge_request.upvotes
+ end
+ end
+ expose :downvotes do |merge_request, options|
+ if options[:issuable_metadata]
+ options[:issuable_metadata][merge_request.id].downvotes
+ else
+ merge_request.downvotes
+ end
+ end
expose :author, :assignee, using: Entities::UserBasic
expose :source_project_id, :target_project_id
- expose :label_names, as: :labels
+ expose :labels do |merge_request, options|
+ # Avoids an N+1 query since labels are preloaded
+ merge_request.labels.map(&:title).sort
+ end
expose :work_in_progress?, as: :work_in_progress
expose :milestone, using: Entities::Milestone
expose :merge_when_pipeline_succeeds
diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb
index d419d345ec5..4ad1eef4ff1 100644
--- a/lib/api/merge_requests.rb
+++ b/lib/api/merge_requests.rb
@@ -10,6 +10,8 @@ module API
resource :projects, requirements: { id: %r{[^/]+} } do
include TimeTrackingEndpoints
+ helpers ::Gitlab::IssuableMetadata
+
helpers do
def handle_merge_request_errors!(errors)
if errors[:project_access].any?
@@ -42,8 +44,7 @@ module API
args[:label_name] = args.delete(:labels)
merge_requests = MergeRequestsFinder.new(current_user, args).execute
- .inc_notes_with_associations
- .preload(:target_project, :author, :assignee, :milestone, :merge_request_diff)
+ .preload(:notes, :target_project, :author, :assignee, :milestone, :merge_request_diff, :labels)
merge_requests.reorder(args[:order_by] => args[:sort])
end
@@ -82,8 +83,9 @@ module API
authorize! :read_merge_request, user_project
merge_requests = find_merge_requests(project_id: user_project.id)
+ issuable_metadata = issuable_meta_data(merge_requests, 'MergeRequest')
- present paginate(merge_requests), with: Entities::MergeRequestBasic, current_user: current_user, project: user_project
+ present paginate(merge_requests), with: Entities::MergeRequestBasic, current_user: current_user, project: user_project, issuable_metadata: issuable_metadata
end
desc 'Create a merge request' do