Commit 8ba9c2bd authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add ContainerTag and ContainerRepository frontend API

parent ee3cf5d6
...@@ -10,31 +10,9 @@ module Projects ...@@ -10,31 +10,9 @@ module Projects
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json do
# Remove code below render json: ContainerRepositoriesSerializer
render json: [ .new(project: project, current_user: current_user)
{ .represent(@images)
name: 'gitlab-org/omnibus-gitlab/foo',
tags_path: 'foo',
destroy_path: 'bar',
location: 'foo',
id: '134',
destroy_path: 'bar'
},
{
name: 'gitlab-org/omnibus-gitlab',
tags_path: 'foo',
destroy_path: 'bar',
location: 'foo',
id: '123',
},
{
name: 'gitlab-org/omnibus-gitlab/bar',
tags_path: 'foo',
destroy_path: 'bar',
location: 'foo',
id: '973',
}
]
end end
end end
end end
...@@ -42,25 +20,11 @@ module Projects ...@@ -42,25 +20,11 @@ module Projects
def destroy def destroy
if image.destroy if image.destroy
respond_to do |format| respond_to do |format|
# TODO: @Kamil, I don't think this is used ever. Should we keep it or remove it?
format.html do
redirect_to project_container_registry_index_path(@project),
status: 302,
notice: 'Image repository has been removed successfully!'
end
format.json { head :no_content } format.json { head :no_content }
end end
else else
respond_to do |format| respond_to do |format|
# TODO: @Kamil, I don't think this is used ever. Should we keep it or remove it? format.json { head :bad_request }
format.html do
redirect_to project_container_registry_index_path(@project),
status: 302,
alert: 'Failed to remove image repository!'
end
format.json { head :no_content }
end end
end end
end end
......
...@@ -3,20 +3,35 @@ module Projects ...@@ -3,20 +3,35 @@ module Projects
class TagsController < ::Projects::Registry::ApplicationController class TagsController < ::Projects::Registry::ApplicationController
before_action :authorize_update_container_image!, only: [:destroy] before_action :authorize_update_container_image!, only: [:destroy]
def index
respond_to do |format|
format.json do
render json: ContainerTagsSerializer
.new(project: @project, current_user: @current_user)
.with_pagination(request, response)
.represent(tags)
end
end
end
def destroy def destroy
respond_to do |format|
format.json do
if tag.delete if tag.delete
redirect_to project_container_registry_index_path(@project), format.json { head :no_content }
status: 302,
notice: 'Registry tag has been removed successfully!'
else else
redirect_to project_container_registry_index_path(@project), format.json { head :bad_request }
status: 302, end
alert: 'Failed to remove registry tag!' end
end end
end end
private private
def tags
Kaminari::PaginatableArray.new(image.tags, limit: 15)
end
def image def image
@image ||= project.container_repositories @image ||= project.container_repositories
.find(params[:repository_id]) .find(params[:repository_id])
......
class ContainerRepositoriesSerializer < BaseSerializer
entity ContainerRepositoryEntity
end
class ContainerRepositoryEntity < Grape::Entity
include RequestAwareEntity
expose :id, :path, :location
expose :tags_path do |repository|
project_registry_repository_tags_path(project, repository, format: :json)
end
expose :destroy_path, if: -> (*) { can_destroy? } do |repository|
project_container_registry_path(project, repository, format: :json)
end
private
alias_method :repository, :object
def project
request.project
end
def can_destroy?
can?(request.current_user, :update_container_image, project)
end
end
class ContainerTagEntity < Grape::Entity
include RequestAwareEntity
expose :name, :location, :revision, :total_size, :created_at
expose :destroy_path, if: -> (*) { can_destroy? } do |tag|
project_registry_repository_tag_path(project, tag.repository, tag.name, format: :json)
end
private
alias_method :tag, :object
def project
request.project
end
def can_destroy?
# TODO: We check permission against @project, not tag,
# as tag is no AR object that is attached to project
can?(request.current_user, :update_container_image, project)
end
end
class ContainerTagsSerializer < BaseSerializer
entity ContainerTagEntity
def with_pagination(request, response)
tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
end
def paginated?
@paginator.present?
end
def represent(resource, opts = {})
resource = @paginator.paginate(resource) if paginated?
super(resource, opts)
end
end
...@@ -271,7 +271,7 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -271,7 +271,7 @@ constraints(ProjectUrlConstrainer.new) do
namespace :registry do namespace :registry do
resources :repository, only: [] do resources :repository, only: [] do
resources :tags, only: [:destroy], resources :tags, only: [:index, :destroy],
constraints: { id: Gitlab::Regex.container_registry_tag_regex } constraints: { id: Gitlab::Regex.container_registry_tag_regex }
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