2019-10-09 17:06:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Serializes diffs with pagination data.
|
|
|
|
#
|
|
|
|
# Avoid adding more keys to this serializer as processing the
|
|
|
|
# diff file serialization is not cheap.
|
|
|
|
#
|
|
|
|
class PaginatedDiffEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
|
|
|
|
|
|
|
expose :diff_files do |diffs, options|
|
|
|
|
submodule_links = Gitlab::SubmoduleLinks.new(merge_request.project.repository)
|
2020-04-02 05:08:14 -04:00
|
|
|
|
|
|
|
DiffFileEntity.represent(diffs.diff_files,
|
2020-06-15 11:08:20 -04:00
|
|
|
options.merge(submodule_links: submodule_links, code_navigation_path: code_navigation_path(diffs)))
|
2019-10-09 17:06:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expose :pagination do
|
|
|
|
expose :current_page
|
|
|
|
expose :next_page
|
|
|
|
expose :total_pages
|
|
|
|
expose :next_page_href do |diffs|
|
|
|
|
next unless next_page
|
|
|
|
|
|
|
|
project = merge_request.target_project
|
|
|
|
|
|
|
|
diffs_batch_namespace_project_json_merge_request_path(
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project.to_param,
|
|
|
|
id: merge_request.iid,
|
|
|
|
page: next_page,
|
|
|
|
format: :json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-15 11:08:20 -04:00
|
|
|
def code_navigation_path(diffs)
|
2020-06-16 08:09:00 -04:00
|
|
|
return unless Feature.enabled?(:code_navigation, merge_request.project, default_enabled: true)
|
2020-06-15 11:08:20 -04:00
|
|
|
|
|
|
|
Gitlab::CodeNavigationPath.new(merge_request.project, diffs.diff_refs&.head_sha)
|
|
|
|
end
|
|
|
|
|
2019-10-09 17:06:24 -04:00
|
|
|
%i[current_page next_page total_pages].each do |method|
|
|
|
|
define_method method do
|
|
|
|
pagination_data[method]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_data
|
|
|
|
options.fetch(:pagination_data, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request
|
|
|
|
options[:merge_request]
|
|
|
|
end
|
|
|
|
end
|