2018-12-13 11:08:53 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
class Releases < Grape::API
|
|
|
|
include PaginationParams
|
|
|
|
|
2019-09-02 07:03:07 +00:00
|
|
|
RELEASE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS
|
2018-12-25 09:48:26 +00:00
|
|
|
.merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
|
2018-12-13 11:08:53 +00:00
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
before { authorize_read_releases! }
|
2018-12-13 11:08:53 +00:00
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
|
|
|
desc 'Get a project releases' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.7.'
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get ':id/releases' do
|
2018-12-21 14:51:46 +00:00
|
|
|
releases = ::ReleasesFinder.new(user_project, current_user).execute
|
2018-12-13 11:08:53 +00:00
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
present paginate(releases), with: Entities::Release, current_user: current_user
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Get a single project release' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.7.'
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
2018-12-25 09:48:26 +00:00
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
2019-09-02 07:03:07 +00:00
|
|
|
get ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMENTS do
|
2019-05-03 13:29:20 +00:00
|
|
|
authorize_download_code!
|
2018-12-13 11:08:53 +00:00
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
present release, with: Entities::Release, current_user: current_user
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Create a new release' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.7.'
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
2018-12-25 09:48:26 +00:00
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
|
2019-11-12 21:06:30 +00:00
|
|
|
optional :name, type: String, desc: 'The name of the release'
|
2020-03-16 12:09:12 +00:00
|
|
|
optional :description, type: String, desc: 'The release notes'
|
2018-12-25 09:48:26 +00:00
|
|
|
optional :ref, type: String, desc: 'The commit sha or branch name'
|
2019-01-02 10:56:03 +00:00
|
|
|
optional :assets, type: Hash do
|
|
|
|
optional :links, type: Array do
|
|
|
|
requires :name, type: String
|
|
|
|
requires :url, type: String
|
|
|
|
end
|
2019-01-02 01:40:33 +00:00
|
|
|
end
|
2019-09-16 15:06:26 +00:00
|
|
|
optional :milestones, type: Array, desc: 'The titles of the related milestones', default: []
|
2019-07-03 09:12:15 +00:00
|
|
|
optional :released_at, type: DateTime, desc: 'The date when the release will be/was ready. Defaults to the current time.'
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
2019-12-11 15:07:38 +00:00
|
|
|
route_setting :authentication, job_token_allowed: true
|
2018-12-13 11:08:53 +00:00
|
|
|
post ':id/releases' do
|
|
|
|
authorize_create_release!
|
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
result = ::Releases::CreateService
|
|
|
|
.new(user_project, current_user, declared_params(include_missing: false))
|
|
|
|
.execute
|
2018-12-13 11:08:53 +00:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2020-01-10 18:07:43 +00:00
|
|
|
log_release_created_audit_event(result[:release])
|
2020-02-01 00:08:41 +00:00
|
|
|
create_evidence!
|
2020-01-10 18:07:43 +00:00
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
present result[:release], with: Entities::Release, current_user: current_user
|
2018-12-13 11:08:53 +00:00
|
|
|
else
|
2018-12-25 09:48:26 +00:00
|
|
|
render_api_error!(result[:message], result[:http_status])
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Update a release' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.7.'
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
2018-12-21 16:40:14 +00:00
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
|
2018-12-25 09:48:26 +00:00
|
|
|
optional :name, type: String, desc: 'The name of the release'
|
|
|
|
optional :description, type: String, desc: 'Release notes with markdown support'
|
2019-07-16 11:47:46 +00:00
|
|
|
optional :released_at, type: DateTime, desc: 'The date when the release will be/was ready.'
|
2019-09-16 15:06:26 +00:00
|
|
|
optional :milestones, type: Array, desc: 'The titles of the related milestones'
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
2019-09-02 07:03:07 +00:00
|
|
|
put ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMENTS do
|
2018-12-13 11:08:53 +00:00
|
|
|
authorize_update_release!
|
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
result = ::Releases::UpdateService
|
|
|
|
.new(user_project, current_user, declared_params(include_missing: false))
|
|
|
|
.execute
|
2018-12-13 11:08:53 +00:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2020-01-10 18:07:43 +00:00
|
|
|
log_release_updated_audit_event
|
|
|
|
log_release_milestones_updated_audit_event if result[:milestones_updated]
|
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
present result[:release], with: Entities::Release, current_user: current_user
|
2018-12-13 11:08:53 +00:00
|
|
|
else
|
|
|
|
render_api_error!(result[:message], result[:http_status])
|
|
|
|
end
|
|
|
|
end
|
2018-12-25 07:36:25 +00:00
|
|
|
|
|
|
|
desc 'Delete a release' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.7.'
|
|
|
|
success Entities::Release
|
|
|
|
end
|
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
|
2018-12-25 07:36:25 +00:00
|
|
|
end
|
2019-09-02 07:03:07 +00:00
|
|
|
delete ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMENTS do
|
2018-12-25 09:48:26 +00:00
|
|
|
authorize_destroy_release!
|
2018-12-25 07:36:25 +00:00
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
result = ::Releases::DestroyService
|
|
|
|
.new(user_project, current_user, declared_params(include_missing: false))
|
|
|
|
.execute
|
2018-12-25 07:36:25 +00:00
|
|
|
|
|
|
|
if result[:status] == :success
|
2019-05-03 13:29:20 +00:00
|
|
|
present result[:release], with: Entities::Release, current_user: current_user
|
2018-12-25 07:36:25 +00:00
|
|
|
else
|
|
|
|
render_api_error!(result[:message], result[:http_status])
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
2018-12-25 09:48:26 +00:00
|
|
|
|
|
|
|
helpers do
|
|
|
|
def authorize_create_release!
|
|
|
|
authorize! :create_release, user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_read_releases!
|
|
|
|
authorize! :read_release, user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_read_release!
|
|
|
|
authorize! :read_release, release
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_update_release!
|
|
|
|
authorize! :update_release, release
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_destroy_release!
|
|
|
|
authorize! :destroy_release, release
|
|
|
|
end
|
|
|
|
|
2019-05-03 13:29:20 +00:00
|
|
|
def authorize_download_code!
|
|
|
|
authorize! :download_code, release
|
|
|
|
end
|
|
|
|
|
2018-12-25 09:48:26 +00:00
|
|
|
def release
|
|
|
|
@release ||= user_project.releases.find_by_tag(params[:tag])
|
|
|
|
end
|
2020-01-10 18:07:43 +00:00
|
|
|
|
|
|
|
def log_release_created_audit_event(release)
|
|
|
|
# This is a separate method so that EE can extend its behaviour
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_release_updated_audit_event
|
|
|
|
# This is a separate method so that EE can extend its behaviour
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_release_milestones_updated_audit_event
|
|
|
|
# This is a separate method so that EE can extend its behaviour
|
|
|
|
end
|
2020-02-01 00:08:41 +00:00
|
|
|
|
|
|
|
def create_evidence!
|
|
|
|
return if release.historical_release?
|
|
|
|
|
|
|
|
if release.upcoming_release?
|
2020-02-14 21:09:08 +00:00
|
|
|
CreateEvidenceWorker.perform_at(release.released_at, release.id) # rubocop:disable CodeReuse/Worker
|
2020-02-01 00:08:41 +00:00
|
|
|
else
|
2020-02-14 21:09:08 +00:00
|
|
|
CreateEvidenceWorker.perform_async(release.id) # rubocop:disable CodeReuse/Worker
|
2020-02-01 00:08:41 +00:00
|
|
|
end
|
|
|
|
end
|
2018-12-25 09:48:26 +00:00
|
|
|
end
|
2018-12-13 11:08:53 +00:00
|
|
|
end
|
|
|
|
end
|
2020-01-10 18:07:43 +00:00
|
|
|
|
|
|
|
API::Releases.prepend_if_ee('EE::API::Releases')
|