Commit 3221f9f3 authored by Kerri Miller's avatar Kerri Miller Committed by Nick Thomas

Allow batch_diffs to filter returned diffs by file path

parent 98c9897a
......@@ -20,7 +20,10 @@ class Projects::MergeRequests::DiffsController < Projects::MergeRequests::Applic
end
def diffs_batch
diffs = @compare.diffs_in_batch(params[:page], params[:per_page], diff_options: diff_options)
diff_options_hash = diff_options
diff_options_hash[:paths] = params[:paths] if params[:paths]
diffs = @compare.diffs_in_batch(params[:page], params[:per_page], diff_options: diff_options_hash)
positions = @merge_request.note_positions_for_paths(diffs.diff_file_paths, current_user)
environment = @merge_request.environments_for(current_user, latest: true).last
......
......@@ -8,6 +8,10 @@ class MergeRequestDiffFile < ApplicationRecord
belongs_to :merge_request_diff, inverse_of: :merge_request_diff_files
alias_attribute :index, :relative_order
scope :by_paths, ->(paths) do
where("new_path in (?) OR old_path in (?)", paths, paths)
end
def utf8_diff
return '' if diff.blank?
......
......@@ -11,8 +11,13 @@ class PaginatedDiffEntity < Grape::Entity
expose :diff_files do |diffs, options|
submodule_links = Gitlab::SubmoduleLinks.new(merge_request.project.repository)
DiffFileEntity.represent(diffs.diff_files,
options.merge(submodule_links: submodule_links, code_navigation_path: code_navigation_path(diffs)))
DiffFileEntity.represent(
diffs.diff_files,
options.merge(
submodule_links: submodule_links,
code_navigation_path: code_navigation_path(diffs)
)
)
end
expose :pagination do
......
......@@ -18,10 +18,8 @@ module Gitlab
def initialize(merge_request_diff, batch_page, batch_size, diff_options:)
super(merge_request_diff, diff_options: diff_options)
batch_page ||= DEFAULT_BATCH_PAGE
batch_size ||= DEFAULT_BATCH_SIZE
@paginated_collection = load_paginated_collection(batch_page, batch_size, diff_options)
@paginated_collection = relation.page(batch_page).per(batch_size)
@pagination_data = {
current_page: @paginated_collection.current_page,
next_page: @paginated_collection.next_page,
......@@ -63,6 +61,18 @@ module Gitlab
def relation
@merge_request_diff.merge_request_diff_files
end
def load_paginated_collection(batch_page, batch_size, diff_options)
batch_page ||= DEFAULT_BATCH_PAGE
batch_size ||= DEFAULT_BATCH_SIZE
paths = diff_options&.fetch(:paths, nil)
paginated_collection = relation.page(batch_page).per(batch_size)
paginated_collection = paginated_collection.by_paths(paths) if paths
paginated_collection
end
end
end
end
......
......@@ -454,6 +454,31 @@ RSpec.describe Projects::MergeRequests::DiffsController do
it_behaves_like 'successful request'
end
context 'with paths param' do
let(:example_file_path) { "README" }
let(:file_path_option) { { paths: [example_file_path] } }
subject do
go(file_path_option)
end
it_behaves_like 'serializes diffs with expected arguments' do
let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch }
let(:expected_options) do
collection_arguments(current_page: 1, total_pages: 1)
end
end
it_behaves_like 'successful request'
it 'filters down the response to the expected file path' do
subject
expect(json_response["diff_files"].size).to eq(1)
expect(json_response["diff_files"].first["file_path"]).to eq(example_file_path)
end
end
context 'with default params' do
subject { go }
......
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