Commit 9eb5ebf9 authored by Micael Bergeron's avatar Micael Bergeron

Remove the injection of `Gitlab::IssuableMetadata` in the API

With the BatchLoader implementation in the
`Gitlab::Entities::IssuableEntity` it was not necessary anymore to
sent this object around.
parent daa21395
......@@ -42,7 +42,7 @@ module API
get ':id/(-/)epics' do
epics = paginate(find_epics(finder_params: { group_id: user_group.id })).with_api_entity_associations
# issuable_metadata is the standard used by the Todo API
# issuable_metadata has to be set because `Entities::Epic` doesn't inherit from `Entities::IssuableEntity`
extra_options = { issuable_metadata: Gitlab::IssuableMetadata.new(current_user, epics).data, with_labels_details: declared_params[:with_labels_details] }
present epics, epic_options.merge(extra_options)
end
......
......@@ -14,19 +14,20 @@ module API
super
end
def issuable_metadata(user: nil)
# Because of the presence of the `user` parameter, we can't
# use the same lazy association.
return Gitlab::IssuableMetadata.new(user, [object]).data[object.id] if user
lazy_issuable_metadata
def issuable_metadata
options.dig(:issuable_metadata, object.id) || lazy_issuable_metadata
end
protected
# This method will preload the `issuable_metadata` for the current
# entity according to the current top-level entity options, such
# as the current_user.
def lazy_issuable_metadata
BatchLoader.for(object).batch(key: :issuable_metadata) do |models, loader|
issuable_metadata = Gitlab::IssuableMetadata.new(nil, models)
BatchLoader.for(object).batch(key: [current_user, :issuable_metadata]) do |models, loader, args|
current_user = args[:key].first
issuable_metadata = Gitlab::IssuableMetadata.new(current_user, models)
metadata_by_id = issuable_metadata.data
models.each do |issuable|
......@@ -34,6 +35,12 @@ module API
end
end
end
private
def current_user
options[:current_user]
end
end
end
end
......@@ -22,7 +22,7 @@ module API
end
expose(:user_notes_count) { |issue, options| issuable_metadata.user_notes_count }
expose(:merge_requests_count) { |issue, options| issuable_metadata(user: options[:current_user]).merge_requests_count }
expose(:merge_requests_count) { |issue, options| issuable_metadata.merge_requests_count }
expose(:upvotes) { |issue, options| issuable_metadata.upvotes }
expose(:downvotes) { |issue, options| issuable_metadata.downvotes }
expose :due_date
......
......@@ -26,14 +26,7 @@ module API
expose(:upvotes) { |merge_request, options| issuable_metadata.upvotes }
expose(:downvotes) { |merge_request, options| issuable_metadata.downvotes }
with_options using: Entities::UserBasic do
expose :author, as: :author
expose :assignees
expose :assignee do |merge_request, options|
merge_request.assignees.first
end
end
expose :author, :assignees, :assignee, using: Entities::UserBasic
expose :source_project_id, :target_project_id
expose :labels do |merge_request, options|
if options[:with_labels_details]
......
......@@ -107,7 +107,6 @@ module API
with: Entities::Issue,
with_labels_details: declared_params[:with_labels_details],
current_user: current_user,
issuable_metadata: Gitlab::IssuableMetadata.new(current_user, issues).data,
include_subscribed: false
}
......@@ -133,7 +132,6 @@ module API
with: Entities::Issue,
with_labels_details: declared_params[:with_labels_details],
current_user: current_user,
issuable_metadata: Gitlab::IssuableMetadata.new(current_user, issues).data,
include_subscribed: false,
group: user_group
}
......@@ -170,7 +168,6 @@ module API
with_labels_details: declared_params[:with_labels_details],
current_user: current_user,
project: user_project,
issuable_metadata: Gitlab::IssuableMetadata.new(current_user, issues).data,
include_subscribed: false
}
......
......@@ -93,7 +93,6 @@ module API
if params[:view] == 'simple'
options[:with] = Entities::MergeRequestSimple
else
options[:issuable_metadata] = Gitlab::IssuableMetadata.new(current_user, merge_requests).data
options[:skip_merge_status_recheck] = !declared_params[:with_merge_status_recheck]
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