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 ...@@ -10,8 +10,7 @@ module Projects
format.json do format.json do
::Gitlab::UsageCounters::DependencyList.increment(project.id) ::Gitlab::UsageCounters::DependencyList.increment(project.id)
render json: ::DependencyListSerializer.new(project: project) render json: serializer.represent(dependencies, build: build)
.represent(paginated_dependencies, build: build)
end end
end end
end end
...@@ -27,17 +26,18 @@ module Projects ...@@ -27,17 +26,18 @@ module Projects
.last .last
end end
def collect_dependencies
found_dependencies = build&.success? ? service.execute : []
::Gitlab::DependenciesCollection.new(found_dependencies)
end
def ensure_dependency_list_feature_available def ensure_dependency_list_feature_available
render_404 unless ::Feature.enabled?(:dependency_list) && render_404 unless ::Feature.enabled?(:dependency_list) &&
project.feature_available?(:dependency_list) project.feature_available?(:dependency_list)
end end
def dependencies def dependencies
@dependencies ||= build&.success? ? service.execute : [] @dependencies ||= collect_dependencies
end
def paginated_dependencies
params[:page] ? Kaminari.paginate_array(dependencies).page(params[:page]) : dependencies
end end
def pipeline def pipeline
...@@ -51,6 +51,12 @@ module Projects ...@@ -51,6 +51,12 @@ module Projects
end end
end end
def serializer
serializer = ::DependencyListSerializer.new(project: project)
serializer = serializer.with_pagination(request, response) if params[:page]
serializer
end
def service def service
::Security::DependencyListService.new(pipeline: pipeline, params: query_params) ::Security::DependencyListService.new(pipeline: pipeline, params: query_params)
end end
......
# frozen_string_literal: true # frozen_string_literal: true
class DependencyListSerializer < BaseSerializer class DependencyListSerializer < BaseSerializer
include WithPagination
entity DependencyListEntity entity DependencyListEntity
end 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 ...@@ -83,6 +83,7 @@ describe Projects::Security::DependenciesController do
it 'returns paginated list' do it 'returns paginated list' do
expect(json_response['dependencies'].length).to eq(1) expect(json_response['dependencies'].length).to eq(1)
expect(response).to include_pagination_headers
end end
end 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