Commit fd33e500 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '23315-routing-changes-and-pagination-for-container-registry' into 'master'

Add pagination and new routing for docker container registry

See merge request gitlab-org/gitlab!23151
parents 789ca265 67b06454
...@@ -14,13 +14,24 @@ module Groups ...@@ -14,13 +14,24 @@ module Groups
track_event(:list_repositories) track_event(:list_repositories)
render json: ContainerRepositoriesSerializer serializer = ContainerRepositoriesSerializer
.new(current_user: current_user) .new(current_user: current_user)
if Feature.enabled?(:vue_container_registry_explorer)
render json: serializer.with_pagination(request, response)
.represent_read_only(@images) .represent_read_only(@images)
else
render json: serializer.represent_read_only(@images)
end
end end
end end
end end
# The show action renders index to allow frontend routing to work on page refresh
def show
render :index
end
private private
def feature_flag_group_container_registry_browser! def feature_flag_group_container_registry_browser!
......
...@@ -14,13 +14,23 @@ module Projects ...@@ -14,13 +14,23 @@ module Projects
track_event(:list_repositories) track_event(:list_repositories)
render json: ContainerRepositoriesSerializer serializer = ContainerRepositoriesSerializer
.new(project: project, current_user: current_user) .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 end
end end
# The show action renders index to allow frontend routing to work on page refresh
def show
render :index
end
def destroy def destroy
DeleteContainerRepositoryWorker.perform_async(current_user.id, image.id) DeleteContainerRepositoryWorker.perform_async(current_user.id, image.id)
track_event(:delete_repository) track_event(:delete_repository)
......
# frozen_string_literal: true # frozen_string_literal: true
class ContainerRepositoriesSerializer < BaseSerializer class ContainerRepositoriesSerializer < BaseSerializer
include WithPagination
entity ContainerRepositoryEntity entity ContainerRepositoryEntity
def represent_read_only(resource) 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 ...@@ -76,7 +76,7 @@ constraints(::Constraints::GroupUrlConstrainer.new) do
end end
end end
resources :container_registries, only: [:index], controller: 'registry/repositories' resources :container_registries, only: [:index, :show], controller: 'registry/repositories'
end end
scope(path: '*id', scope(path: '*id',
......
...@@ -371,7 +371,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -371,7 +371,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end end
end end
resources :container_registry, only: [:index, :destroy], resources :container_registry, only: [:index, :destroy, :show],
controller: 'registry/repositories' controller: 'registry/repositories'
namespace :registry do namespace :registry do
......
...@@ -14,7 +14,7 @@ describe Groups::Registry::RepositoriesController do ...@@ -14,7 +14,7 @@ describe Groups::Registry::RepositoriesController do
sign_in(user) sign_in(user)
end end
context 'GET #index' do shared_examples 'renders a list of repositories' do
context 'when container registry is enabled' do context 'when container registry is enabled' do
it 'show index page' do it 'show index page' do
expect(Gitlab::Tracking).not_to receive(:event) expect(Gitlab::Tracking).not_to receive(:event)
...@@ -33,6 +33,7 @@ describe Groups::Registry::RepositoriesController do ...@@ -33,6 +33,7 @@ describe Groups::Registry::RepositoriesController do
} }
expect(response).to match_response_schema('registry/repositories') expect(response).to match_response_schema('registry/repositories')
expect(response).to include_pagination_headers
end end
it 'returns a list of projects for json format' do it 'returns a list of projects for json format' do
...@@ -89,5 +90,29 @@ describe Groups::Registry::RepositoriesController do ...@@ -89,5 +90,29 @@ describe Groups::Registry::RepositoriesController do
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
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
end end
...@@ -16,7 +16,7 @@ describe Projects::Registry::RepositoriesController do ...@@ -16,7 +16,7 @@ describe Projects::Registry::RepositoriesController do
project.add_developer(user) project.add_developer(user)
end end
describe 'GET index' do shared_examples 'renders a list of repositories' do
context 'when root container repository exists' do context 'when root container repository exists' do
before do before do
create(:container_repository, :root, project: project) create(:container_repository, :root, project: project)
...@@ -58,6 +58,7 @@ describe Projects::Registry::RepositoriesController do ...@@ -58,6 +58,7 @@ describe Projects::Registry::RepositoriesController do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('registry/repositories') expect(response).to match_response_schema('registry/repositories')
expect(response).to include_pagination_headers
end end
end end
...@@ -84,9 +85,33 @@ describe Projects::Registry::RepositoriesController do ...@@ -84,9 +85,33 @@ describe Projects::Registry::RepositoriesController do
end end
end 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 end
describe 'DELETE destroy' do describe 'DELETE #destroy' do
context 'when root container repository exists' do context 'when root container repository exists' do
let!(:repository) do let!(:repository) do
create(:container_repository, :root, project: project) create(:container_repository, :root, project: project)
...@@ -115,7 +140,7 @@ describe Projects::Registry::RepositoriesController do ...@@ -115,7 +140,7 @@ describe Projects::Registry::RepositoriesController do
end end
context 'when user does not have access to registry' do context 'when user does not have access to registry' do
describe 'GET index' do describe 'GET #index' do
it 'responds with 404' do it 'responds with 404' do
go_to_index 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