Commit 67b06454 authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera Committed by Mayra Cabrera

Add pagination and new routing

- routes for project
- routes for groups
- pagination in serialiser
- project controller
- group controller
parent 1965d323
......@@ -14,13 +14,24 @@ module Groups
track_event(:list_repositories)
render json: ContainerRepositoriesSerializer
serializer = ContainerRepositoriesSerializer
.new(current_user: current_user)
.represent_read_only(@images)
if Feature.enabled?(:vue_container_registry_explorer)
render json: serializer.with_pagination(request, response)
.represent_read_only(@images)
else
render json: serializer.represent_read_only(@images)
end
end
end
end
# The show action renders index to allow frontend routing to work on page refresh
def show
render :index
end
private
def feature_flag_group_container_registry_browser!
......
......@@ -14,13 +14,23 @@ module Projects
track_event(:list_repositories)
render json: ContainerRepositoriesSerializer
serializer = ContainerRepositoriesSerializer
.new(project: project, current_user: current_user)
.represent(@images)
if Feature.enabled?(:vue_container_registry_explorer)
render json: serializer.with_pagination(request, response).represent(@images)
else
render json: serializer.represent(@images)
end
end
end
end
# The show action renders index to allow frontend routing to work on page refresh
def show
render :index
end
def destroy
DeleteContainerRepositoryWorker.perform_async(current_user.id, image.id)
track_event(:delete_repository)
......
# frozen_string_literal: true
class ContainerRepositoriesSerializer < BaseSerializer
include WithPagination
entity ContainerRepositoryEntity
def represent_read_only(resource)
......
---
title: Add show routes for group and project repositories_controllers and add pagination
to the index responses
merge_request: 23151
author:
type: added
......@@ -76,7 +76,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
end
end
resources :container_registries, only: [:index], controller: 'registry/repositories'
resources :container_registries, only: [:index, :show], controller: 'registry/repositories'
end
scope(path: '*id',
......
......@@ -371,7 +371,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
end
resources :container_registry, only: [:index, :destroy],
resources :container_registry, only: [:index, :destroy, :show],
controller: 'registry/repositories'
namespace :registry do
......
......@@ -14,7 +14,7 @@ describe Groups::Registry::RepositoriesController do
sign_in(user)
end
context 'GET #index' do
shared_examples 'renders a list of repositories' do
context 'when container registry is enabled' do
it 'show index page' do
expect(Gitlab::Tracking).not_to receive(:event)
......@@ -33,6 +33,7 @@ describe Groups::Registry::RepositoriesController do
}
expect(response).to match_response_schema('registry/repositories')
expect(response).to include_pagination_headers
end
it 'returns a list of projects for json format' do
......@@ -89,5 +90,29 @@ describe Groups::Registry::RepositoriesController do
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with :vue_container_registry_explorer feature flag disabled' do
before do
stub_feature_flags(vue_container_registry_explorer: false)
end
it 'has the correct response schema' do
get :index, params: {
group_id: group,
format: :json
}
expect(response).to match_response_schema('registry/repositories')
expect(response).not_to include_pagination_headers
end
end
end
context 'GET #index' do
it_behaves_like 'renders a list of repositories'
end
context 'GET #show' do
it_behaves_like 'renders a list of repositories'
end
end
......@@ -16,7 +16,7 @@ describe Projects::Registry::RepositoriesController do
project.add_developer(user)
end
describe 'GET index' do
shared_examples 'renders a list of repositories' do
context 'when root container repository exists' do
before do
create(:container_repository, :root, project: project)
......@@ -58,6 +58,7 @@ describe Projects::Registry::RepositoriesController do
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('registry/repositories')
expect(response).to include_pagination_headers
end
end
......@@ -84,9 +85,33 @@ describe Projects::Registry::RepositoriesController do
end
end
end
context 'with :vue_container_registry_explorer feature flag disabled' do
before do
stub_feature_flags(vue_container_registry_explorer: false)
stub_container_registry_tags(repository: project.full_path,
tags: %w[rc1 latest])
end
it 'json has a list of projects' do
go_to_index(format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('registry/repositories')
expect(response).not_to include_pagination_headers
end
end
end
describe 'GET #index' do
it_behaves_like 'renders a list of repositories'
end
describe 'GET #show' do
it_behaves_like 'renders a list of repositories'
end
describe 'DELETE destroy' do
describe 'DELETE #destroy' do
context 'when root container repository exists' do
let!(:repository) do
create(:container_repository, :root, project: project)
......@@ -115,7 +140,7 @@ describe Projects::Registry::RepositoriesController do
end
context 'when user does not have access to registry' do
describe 'GET index' do
describe 'GET #index' do
it 'responds with 404' do
go_to_index
......
# frozen_string_literal: true
require 'spec_helper'
describe ContainerRepositoriesSerializer do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:resource) { create(:container_repository, name: 'image', project: project) }
let(:params) { { current_user: user, project: project } }
before do
project.add_developer(user)
stub_container_registry_config(enabled: true)
stub_container_registry_tags(repository: /image/, tags: %w(rootA latest))
end
describe '#represent' do
subject do
described_class.new(params).represent(resource)
end
it 'has basic attributes' do
expect(subject).to include(:id, :name, :path, :location, :created_at, :tags_path, :destroy_path)
end
end
describe '#represent_read_only' do
subject do
described_class.new(current_user: user, project: project).represent_read_only(resource)
end
it 'does not include destroy_path' do
expect(subject).to include(:id, :name, :path, :location, :created_at, :tags_path)
expect(subject).not_to include(:destroy_path)
end
end
describe '#with_pagination' do
let(:request) do
double(
url: "#{Gitlab.config.gitlab.url}:8080/#{project.namespace_id}/#{project.id}/container_registry?#{query.to_query}",
query_parameters: query
)
end
let(:response) { spy('response') }
let(:resource) { ContainerRepository.all }
let(:query) { { page: 1, per_page: 2 } }
let(:serializer) do
described_class
.new(current_user: user, project: project)
.with_pagination(request, response)
end
subject do
serializer.represent(resource)
end
it 'creates a paginated serializer' do
expect(serializer).to be_paginated
end
context 'when multiple ContainerRepository objects are serialized' do
before do
create_list(:container_repository, 5, project: project)
end
it 'serializes appropriate number of objects' do
expect(subject.count).to be 2
end
it 'appends relevant headers' do
expect(response).to include_pagination_headers
expect(response).to receive(:[]=).with('X-Total', '5')
expect(response).to receive(:[]=).with('X-Total-Pages', '3')
expect(response).to receive(:[]=).with('X-Per-Page', '2')
subject
end
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