debian-mirror-gitlab/lib/api/tags.rb

183 lines
6.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-11-26 14:37:03 +05:30
module API
2021-01-03 14:25:43 +05:30
class Tags < ::API::Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2019-02-15 15:39:39 +05:30
TAG_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
2017-09-10 17:25:29 +05:30
2015-11-26 14:37:03 +05:30
before { authorize! :download_code, user_project }
2017-08-17 22:00:37 +05:30
params do
requires :id, type: String, desc: 'The ID of a project'
end
2019-02-15 15:39:39 +05:30
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
desc 'Get a project repository tags' do
2018-03-17 18:26:18 +05:30
success Entities::Tag
2017-08-17 22:00:37 +05:30
end
params do
2018-03-17 18:26:18 +05:30
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-03-02 22:35:43 +05:30
optional :search, type: String, desc: 'Return list of tags matching the search criteria'
2017-08-17 22:00:37 +05:30
use :pagination
end
2021-01-29 00:20:46 +05:30
get ':id/repository/tags', feature_category: :source_code_management do
2019-03-02 22:35:43 +05:30
tags = ::TagsFinder.new(user_project.repository,
sort: "#{params[:order_by]}_#{params[:sort]}",
search: params[:search]).execute
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
paginated_tags = paginate(::Kaminari.paginate_array(tags))
if Feature.enabled?(:api_caching_tags, user_project, type: :development)
present_cached paginated_tags, with: Entities::Tag, project: user_project, cache_context: -> (_tag) { user_project.cache_key }
else
present paginated_tags, with: Entities::Tag, project: user_project
end
2015-11-26 14:37:03 +05:30
end
2017-08-17 22:00:37 +05:30
desc 'Get a single repository tag' do
2018-03-17 18:26:18 +05:30
success Entities::Tag
2017-08-17 22:00:37 +05:30
end
params do
requires :tag_name, type: String, desc: 'The name of the tag'
end
2021-01-29 00:20:46 +05:30
get ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :source_code_management do
2016-06-02 11:05:42 +05:30
tag = user_project.repository.find_tag(params[:tag_name])
not_found!('Tag') unless tag
2018-03-17 18:26:18 +05:30
present tag, with: Entities::Tag, project: user_project
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
desc 'Create a new repository tag' do
2019-02-15 15:39:39 +05:30
detail 'This optional release_description parameter was deprecated in GitLab 11.7.'
2018-03-17 18:26:18 +05:30
success Entities::Tag
2017-08-17 22:00:37 +05:30
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'
2019-02-15 15:39:39 +05:30
optional :release_description, type: String, desc: 'Specifying release notes stored in the GitLab database (deprecated in GitLab 11.7)'
2017-08-17 22:00:37 +05:30
end
2021-01-29 00:20:46 +05:30
post ':id/repository/tags', :release_orchestration do
2019-09-30 21:07:59 +05:30
authorize_admin_tag
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
result = ::Tags::CreateService.new(user_project, current_user)
2019-02-15 15:39:39 +05:30
.execute(params[:tag_name], params[:ref], params[:message])
2015-11-26 14:37:03 +05:30
if result[:status] == :success
2019-02-15 15:39:39 +05:30
# Release creation with Tags API was deprecated in GitLab 11.7
if params[:release_description].present?
release_create_params = {
tag: params[:tag_name],
name: params[:tag_name], # Name can be specified in new API
description: params[:release_description]
}
::Releases::CreateService
.new(user_project, current_user, release_create_params)
.execute
end
2015-11-26 14:37:03 +05:30
present result[:tag],
2018-03-17 18:26:18 +05:30
with: Entities::Tag,
2015-11-26 14:37:03 +05:30
project: user_project
else
render_api_error!(result[:message], 400)
end
end
2017-08-17 22:00:37 +05:30
desc 'Delete a repository tag'
params do
requires :tag_name, type: String, desc: 'The name of the tag'
end
2021-01-29 00:20:46 +05:30
delete ':id/repository/tags/:tag_name', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :source_code_management do
2019-09-30 21:07:59 +05:30
authorize_admin_tag
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
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
result = ::Tags::DestroyService.new(user_project, current_user)
.execute(params[:tag_name])
2018-03-17 18:26:18 +05:30
if result[:status] != :success
render_api_error!(result[:message], result[:return_code])
end
end
end
2017-08-17 22:00:37 +05:30
desc 'Add a release note to a tag' do
2019-02-15 15:39:39 +05:30
detail 'This feature was deprecated in GitLab 11.7.'
success Entities::TagRelease
2017-08-17 22:00:37 +05:30
end
params do
2019-02-15 15:39:39 +05:30
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
2017-08-17 22:00:37 +05:30
requires :description, type: String, desc: 'Release notes with markdown support'
end
2021-01-29 00:20:46 +05:30
post ':id/repository/tags/:tag_name/release', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :release_orchestration do
2019-02-15 15:39:39 +05:30
authorize_create_release!
##
# Legacy API does not support tag auto creation.
not_found!('Tag') unless user_project.repository.find_tag(params[:tag])
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
release_create_params = {
tag: params[:tag],
name: params[:tag], # Name can be specified in new API
description: params[:description]
}
result = ::Releases::CreateService
.new(user_project, current_user, release_create_params)
.execute
2015-11-26 14:37:03 +05:30
if result[:status] == :success
2019-02-15 15:39:39 +05:30
present result[:release], with: Entities::TagRelease
2015-11-26 14:37:03 +05:30
else
render_api_error!(result[:message], result[:http_status])
end
end
2017-08-17 22:00:37 +05:30
desc "Update a tag's release note" do
2019-02-15 15:39:39 +05:30
detail 'This feature was deprecated in GitLab 11.7.'
success Entities::TagRelease
2017-08-17 22:00:37 +05:30
end
params do
2019-02-15 15:39:39 +05:30
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
2017-08-17 22:00:37 +05:30
requires :description, type: String, desc: 'Release notes with markdown support'
end
2021-01-29 00:20:46 +05:30
put ':id/repository/tags/:tag_name/release', requirements: TAG_ENDPOINT_REQUIREMENTS, feature_category: :release_orchestration do
2019-02-15 15:39:39 +05:30
authorize_update_release!
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
result = ::Releases::UpdateService
.new(user_project, current_user, declared_params(include_missing: false))
.execute
2015-11-26 14:37:03 +05:30
if result[:status] == :success
2019-02-15 15:39:39 +05:30
present result[:release], with: Entities::TagRelease
2015-11-26 14:37:03 +05:30
else
render_api_error!(result[:message], result[:http_status])
end
end
end
2019-02-15 15:39:39 +05:30
helpers do
def authorize_create_release!
authorize! :create_release, user_project
end
def authorize_update_release!
authorize! :update_release, release
end
def release
@release ||= user_project.releases.find_by_tag(params[:tag])
end
end
2015-11-26 14:37:03 +05:30
end
end