gitlab-org--gitlab-foss/lib/api/tags.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
4.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module API
class Tags < ::API::Base
include PaginationParams
TAG_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
before do
authorize! :download_code, user_project
not_found! unless user_project.repo_exists?
end
2016-10-13 15:18:06 +00:00
params do
requires :id, type: String, desc: 'The ID of a project'
end
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
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.'
2019-01-21 10:01:00 +00:00
optional :search, type: String, desc: 'Return list of tags matching the search criteria'
optional :page_token, type: String, desc: 'Name of tag to start the paginaition from'
use :pagination
end
get ':id/repository/tags', feature_category: :source_code_management, urgency: :low do
tags_finder = ::TagsFinder.new(user_project.repository,
2019-01-21 10:01:00 +00:00
sort: "#{params[:order_by]}_#{params[:sort]}",
search: params[:search],
page_token: params[:page_token],
per_page: params[:per_page])
2017-12-14 13:42:15 +00:00
paginated_tags = Gitlab::Pagination::GitalyKeysetPager.new(self, user_project).paginate(tags_finder)
present_cached paginated_tags, with: Entities::Tag, project: user_project, cache_context: -> (_tag) { user_project.cache_key }
rescue Gitlab::Git::InvalidPageToken => e
unprocessable_entity!(e.message)
rescue Gitlab::Git::CommandError
service_unavailable!
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
get ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :source_code_management 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'
end
post ':id/repository/tags', :release_orchestration do
authorize_admin_tag
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])
if result[:status] == :success
present result[:tag],
2017-10-05 08:48:05 +00:00
with: Entities::Tag,
project: user_project
else
render_api_error!(result[:message], 400)
end
end
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
delete ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :source_code_management do
authorize_admin_tag
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)
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
end
end
end