Commit ccce5aa0 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'dependency-list-pagination-bug' into 'master'

Add pagination data to dependency list response

See merge request gitlab-org/gitlab-ee!14115
parents 98b859ef b17cc509
......@@ -10,8 +10,7 @@ module Projects
format.json do
::Gitlab::UsageCounters::DependencyList.increment(project.id)
render json: ::DependencyListSerializer.new(project: project)
.represent(paginated_dependencies, build: build)
render json: serializer.represent(dependencies, build: build)
end
end
end
......@@ -27,17 +26,18 @@ module Projects
.last
end
def collect_dependencies
found_dependencies = build&.success? ? service.execute : []
::Gitlab::DependenciesCollection.new(found_dependencies)
end
def ensure_dependency_list_feature_available
render_404 unless ::Feature.enabled?(:dependency_list) &&
project.feature_available?(:dependency_list)
end
def dependencies
@dependencies ||= build&.success? ? service.execute : []
end
def paginated_dependencies
params[:page] ? Kaminari.paginate_array(dependencies).page(params[:page]) : dependencies
@dependencies ||= collect_dependencies
end
def pipeline
......@@ -51,6 +51,12 @@ module Projects
end
end
def serializer
serializer = ::DependencyListSerializer.new(project: project)
serializer = serializer.with_pagination(request, response) if params[:page]
serializer
end
def service
::Security::DependencyListService.new(pipeline: pipeline, params: query_params)
end
......
# frozen_string_literal: true
class DependencyListSerializer < BaseSerializer
include WithPagination
entity DependencyListEntity
end
# frozen_string_literal: true
module Gitlab
class DependenciesCollection
include Enumerable
def initialize(dependencies)
@collection = dependencies
end
def each
collection.each { |item| yield item }
end
def page(number)
Kaminari.paginate_array(collection).page(number)
end
def to_ary
collection
end
private
attr_reader :collection
end
end
......@@ -83,6 +83,7 @@ describe Projects::Security::DependenciesController do
it 'returns paginated list' do
expect(json_response['dependencies'].length).to eq(1)
expect(response).to include_pagination_headers
end
end
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::DependenciesCollection do
let(:collection) { described_class.new(fake_dependencies) }
let(:fake_dependencies) { Array.new(42, :dependency) }
it 'responds to each' do
expect(collection).to respond_to(:each)
end
describe '#page' do
subject { collection.page(3) }
it 'returns paginated collection' do
expect(subject.length).to eq(2)
end
end
describe '#to_ary' do
subject { collection.to_ary }
it 'returns Array' do
is_expected.to be_an(Array)
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