2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-12 14:41:13 +00:00
|
|
|
module API
|
|
|
|
class Tags < Grape::API
|
2017-01-17 04:45:07 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2018-11-08 12:18:17 +00:00
|
|
|
TAG_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
|
2017-07-26 13:53:07 +00:00
|
|
|
|
2015-11-12 14:41:13 +00:00
|
|
|
before { authorize! :download_code, user_project }
|
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 12:18:17 +00:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-10-13 15:18:06 +00:00
|
|
|
desc 'Get a project repository tags' do
|
2017-10-05 08:48:05 +00:00
|
|
|
success Entities::Tag
|
2016-10-13 15:18:06 +00:00
|
|
|
end
|
2017-01-17 04:45:07 +00:00
|
|
|
params do
|
2017-12-14 13:42:15 +00:00
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc',
|
|
|
|
desc: 'Return tags sorted in updated by `asc` or `desc` order.'
|
|
|
|
optional :order_by, type: String, values: %w[name updated], default: 'updated',
|
|
|
|
desc: 'Return tags ordered by `name` or `updated` fields.'
|
2017-01-17 04:45:07 +00:00
|
|
|
use :pagination
|
|
|
|
end
|
2017-07-26 13:53:07 +00:00
|
|
|
get ':id/repository/tags' do
|
2017-12-14 13:42:15 +00:00
|
|
|
tags = ::Kaminari.paginate_array(::TagsFinder.new(user_project.repository, sort: "#{params[:order_by]}_#{params[:sort]}").execute)
|
|
|
|
|
2017-10-05 08:48:05 +00:00
|
|
|
present paginate(tags), with: Entities::Tag, project: user_project
|
2015-11-12 14:41:13 +00:00
|
|
|
end
|
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
desc 'Get a single repository tag' do
|
2017-10-05 08:48:05 +00:00
|
|
|
success Entities::Tag
|
2016-10-13 15:18:06 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
end
|
2017-07-26 13:53:07 +00:00
|
|
|
get ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS do
|
2016-04-06 11:59:50 +00:00
|
|
|
tag = user_project.repository.find_tag(params[:tag_name])
|
|
|
|
not_found!('Tag') unless tag
|
|
|
|
|
2017-10-05 08:48:05 +00:00
|
|
|
present tag, with: Entities::Tag, project: user_project
|
2016-04-06 11:59:50 +00:00
|
|
|
end
|
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
desc 'Create a new repository tag' do
|
2017-10-05 08:48:05 +00:00
|
|
|
success Entities::Tag
|
2016-10-13 15:18:06 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
requires :ref, type: String, desc: 'The commit sha or branch name'
|
|
|
|
optional :message, type: String, desc: 'Specifying a message creates an annotated tag'
|
|
|
|
optional :release_description, type: String, desc: 'Specifying release notes stored in the GitLab database'
|
|
|
|
end
|
2015-11-12 14:41:13 +00:00
|
|
|
post ':id/repository/tags' do
|
|
|
|
authorize_push_project
|
2016-10-13 15:18:06 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
result = ::Tags::CreateService.new(user_project, current_user)
|
|
|
|
.execute(params[:tag_name], params[:ref], params[:message], params[:release_description])
|
2015-11-12 14:41:13 +00:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
present result[:tag],
|
2017-10-05 08:48:05 +00:00
|
|
|
with: Entities::Tag,
|
2015-11-12 14:41:13 +00:00
|
|
|
project: user_project
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
|
|
|
end
|
2015-11-12 15:26:39 +00:00
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
desc 'Delete a repository tag'
|
|
|
|
params do
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
end
|
2017-07-26 13:53:07 +00:00
|
|
|
delete ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS do
|
2016-01-08 09:10:04 +00:00
|
|
|
authorize_push_project
|
2016-10-13 15:18:06 +00:00
|
|
|
|
2017-03-02 13:21:36 +00:00
|
|
|
tag = user_project.repository.find_tag(params[:tag_name])
|
|
|
|
not_found!('Tag') unless tag
|
|
|
|
|
|
|
|
commit = user_project.repository.commit(tag.dereferenced_target)
|
|
|
|
|
2017-08-30 12:33:39 +00:00
|
|
|
destroy_conditionally!(commit, last_updated: commit.authored_date) do
|
2017-03-02 13:21:36 +00:00
|
|
|
result = ::Tags::DestroyService.new(user_project, current_user)
|
|
|
|
.execute(params[:tag_name])
|
2016-01-08 09:10:04 +00:00
|
|
|
|
2017-03-02 13:21:36 +00:00
|
|
|
if result[:status] != :success
|
|
|
|
render_api_error!(result[:message], result[:return_code])
|
|
|
|
end
|
2016-01-08 09:10:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
desc 'Add a release note to a tag' do
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
requires :description, type: String, desc: 'Release notes with markdown support'
|
|
|
|
end
|
2017-07-26 13:53:07 +00:00
|
|
|
post ':id/repository/tags/:tag_name/release', requirements: TAG_ENDPOINT_REQUIREMENTS do
|
2015-11-12 15:26:39 +00:00
|
|
|
authorize_push_project
|
2016-10-13 15:18:06 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
result = CreateReleaseService.new(user_project, current_user)
|
|
|
|
.execute(params[:tag_name], params[:description])
|
2015-11-12 15:26:39 +00:00
|
|
|
|
2015-11-21 17:08:45 +00:00
|
|
|
if result[:status] == :success
|
|
|
|
present result[:release], with: Entities::Release
|
|
|
|
else
|
2015-11-21 21:24:34 +00:00
|
|
|
render_api_error!(result[:message], result[:http_status])
|
2015-11-21 17:08:45 +00:00
|
|
|
end
|
2015-11-12 15:26:39 +00:00
|
|
|
end
|
2015-11-21 21:34:53 +00:00
|
|
|
|
2016-10-13 15:18:06 +00:00
|
|
|
desc "Update a tag's release note" do
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag'
|
|
|
|
requires :description, type: String, desc: 'Release notes with markdown support'
|
|
|
|
end
|
2017-07-26 13:53:07 +00:00
|
|
|
put ':id/repository/tags/:tag_name/release', requirements: TAG_ENDPOINT_REQUIREMENTS do
|
2015-11-21 21:34:53 +00:00
|
|
|
authorize_push_project
|
2016-10-13 15:18:06 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
result = UpdateReleaseService.new(user_project, current_user)
|
|
|
|
.execute(params[:tag_name], params[:description])
|
2015-11-21 21:34:53 +00:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
present result[:release], with: Entities::Release
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], result[:http_status])
|
|
|
|
end
|
|
|
|
end
|
2015-11-12 14:41:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|