2018-12-13 06:08:53 -05:00
# frozen_string_literal: true
module API
2020-10-14 20:08:42 -04:00
class Releases < :: API :: Base
2018-12-13 06:08:53 -05:00
include PaginationParams
2019-09-02 03:03:07 -04:00
RELEASE_ENDPOINT_REQUIREMENTS = API :: NAMESPACE_OR_PROJECT_REQUIREMENTS
2018-12-25 04:48:26 -05:00
. merge ( tag_name : API :: NO_SLASH_URL_PART_REGEX )
2021-04-30 02:10:26 -04:00
RELEASE_CLI_USER_AGENT = 'GitLab-release-cli'
2018-12-13 06:08:53 -05:00
2018-12-25 04:48:26 -05:00
before { authorize_read_releases! }
2018-12-13 06:08:53 -05:00
2021-04-30 02:10:26 -04:00
after { track_release_event }
2020-10-30 14:08:56 -04:00
feature_category :release_orchestration
2018-12-13 06:08:53 -05: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.'
2021-04-30 02:10:26 -04:00
named 'get_releases'
2018-12-13 06:08:53 -05:00
success Entities :: Release
end
params do
use :pagination
2020-10-12 14:08:31 -04:00
optional :order_by , type : String , values : %w[ released_at created_at ] , default : 'released_at' ,
desc : 'Return releases ordered by `released_at` or `created_at`.'
optional :sort , type : String , values : %w[ asc desc ] , default : 'desc' ,
desc : 'Return releases sorted in `asc` or `desc` order.'
2021-06-03 08:10:18 -04:00
optional :include_html_description , type : Boolean ,
desc : 'If `true`, a response includes HTML rendered markdown of the release description.'
2018-12-13 06:08:53 -05:00
end
2021-10-27 11:13:41 -04:00
route_setting :authentication , job_token_allowed : true
2018-12-13 06:08:53 -05:00
get ':id/releases' do
2020-10-12 14:08:31 -04:00
releases = :: ReleasesFinder . new ( user_project , current_user , declared_params . slice ( :order_by , :sort ) ) . execute
2018-12-13 06:08:53 -05:00
2021-05-10 23:10:35 -04:00
# We cache the serialized payload per user in order to avoid repeated renderings.
# Since the cached result could contain sensitive information,
# it will expire in a short interval.
present_cached paginate ( releases ) ,
with : Entities :: Release ,
# `current_user` could be absent if the releases are publicly accesible.
# We should not use `cache_key` for the user because the version/updated_at
# context is unnecessary here.
cache_context : - > ( _ ) { " user:{ #{ current_user & . id } } " } ,
expires_in : 5 . minutes ,
2021-06-03 08:10:18 -04:00
current_user : current_user ,
include_html_description : params [ :include_html_description ]
2018-12-13 06:08:53 -05:00
end
desc 'Get a single project release' do
detail 'This feature was introduced in GitLab 11.7.'
2021-04-30 02:10:26 -04:00
named 'get_release'
2018-12-13 06:08:53 -05:00
success Entities :: Release
end
params do
2018-12-25 04:48:26 -05:00
requires :tag_name , type : String , desc : 'The name of the tag' , as : :tag
2021-06-03 08:10:18 -04:00
optional :include_html_description , type : Boolean ,
desc : 'If `true`, a response includes HTML rendered markdown of the release description.'
2018-12-13 06:08:53 -05:00
end
2021-10-27 11:13:41 -04:00
route_setting :authentication , job_token_allowed : true
2019-09-02 03:03:07 -04:00
get ':id/releases/:tag_name' , requirements : RELEASE_ENDPOINT_REQUIREMENTS do
2019-05-03 09:29:20 -04:00
authorize_download_code!
2018-12-13 06:08:53 -05:00
2021-06-29 23:07:30 -04:00
not_found! unless release
2021-06-03 08:10:18 -04:00
present release , with : Entities :: Release , current_user : current_user , include_html_description : params [ :include_html_description ]
2018-12-13 06:08:53 -05:00
end
desc 'Create a new release' do
detail 'This feature was introduced in GitLab 11.7.'
2021-04-30 02:10:26 -04:00
named 'create_release'
2018-12-13 06:08:53 -05:00
success Entities :: Release
end
params do
2018-12-25 04:48:26 -05:00
requires :tag_name , type : String , desc : 'The name of the tag' , as : :tag
2019-11-12 16:06:30 -05:00
optional :name , type : String , desc : 'The name of the release'
2020-03-16 08:09:12 -04:00
optional :description , type : String , desc : 'The release notes'
2018-12-25 04:48:26 -05:00
optional :ref , type : String , desc : 'The commit sha or branch name'
2019-01-02 05:56:03 -05:00
optional :assets , type : Hash do
optional :links , type : Array do
2020-07-23 14:10:06 -04:00
requires :name , type : String , desc : 'The name of the link'
requires :url , type : String , desc : 'The URL of the link'
optional :filepath , type : String , desc : 'The filepath of the link'
optional :link_type , type : String , desc : 'The link type, one of: "runbook", "image", "package" or "other"'
2019-01-02 05:56:03 -05:00
end
2019-01-01 20:40:33 -05:00
end
2020-06-29 17:09:07 -04:00
optional :milestones , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , desc : 'The titles of the related milestones' , default : [ ]
2019-07-03 05:12:15 -04:00
optional :released_at , type : DateTime , desc : 'The date when the release will be/was ready. Defaults to the current time.'
2018-12-13 06:08:53 -05:00
end
2019-12-11 10:07:38 -05:00
route_setting :authentication , job_token_allowed : true
2018-12-13 06:08:53 -05:00
post ':id/releases' do
authorize_create_release!
2018-12-25 04:48:26 -05:00
result = :: Releases :: CreateService
. new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute
2018-12-13 06:08:53 -05:00
if result [ :status ] == :success
2020-01-10 13:07:43 -05:00
log_release_created_audit_event ( result [ :release ] )
2019-05-03 09:29:20 -04:00
present result [ :release ] , with : Entities :: Release , current_user : current_user
2018-12-13 06:08:53 -05:00
else
2018-12-25 04:48:26 -05:00
render_api_error! ( result [ :message ] , result [ :http_status ] )
2018-12-13 06:08:53 -05:00
end
end
desc 'Update a release' do
detail 'This feature was introduced in GitLab 11.7.'
2021-04-30 02:10:26 -04:00
named 'update_release'
2018-12-13 06:08:53 -05:00
success Entities :: Release
end
params do
2018-12-21 11:40:14 -05:00
requires :tag_name , type : String , desc : 'The name of the tag' , as : :tag
2018-12-25 04:48:26 -05:00
optional :name , type : String , desc : 'The name of the release'
optional :description , type : String , desc : 'Release notes with markdown support'
2019-07-16 07:47:46 -04:00
optional :released_at , type : DateTime , desc : 'The date when the release will be/was ready.'
2020-11-09 10:08:56 -05:00
optional :milestones , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , desc : 'The titles of the related milestones'
2018-12-13 06:08:53 -05:00
end
2021-10-27 11:13:41 -04:00
route_setting :authentication , job_token_allowed : true
2019-09-02 03:03:07 -04:00
put ':id/releases/:tag_name' , requirements : RELEASE_ENDPOINT_REQUIREMENTS do
2018-12-13 06:08:53 -05:00
authorize_update_release!
2018-12-25 04:48:26 -05:00
result = :: Releases :: UpdateService
. new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute
2018-12-13 06:08:53 -05:00
if result [ :status ] == :success
2020-01-10 13:07:43 -05:00
log_release_updated_audit_event
log_release_milestones_updated_audit_event if result [ :milestones_updated ]
2019-05-03 09:29:20 -04:00
present result [ :release ] , with : Entities :: Release , current_user : current_user
2018-12-13 06:08:53 -05:00
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
end
2018-12-25 02:36:25 -05:00
desc 'Delete a release' do
detail 'This feature was introduced in GitLab 11.7.'
2021-04-30 02:10:26 -04:00
named 'delete_release'
2018-12-25 02:36:25 -05:00
success Entities :: Release
end
params do
2019-01-16 07:09:29 -05:00
requires :tag_name , type : String , desc : 'The name of the tag' , as : :tag
2018-12-25 02:36:25 -05:00
end
2021-10-27 11:13:41 -04:00
route_setting :authentication , job_token_allowed : true
2019-09-02 03:03:07 -04:00
delete ':id/releases/:tag_name' , requirements : RELEASE_ENDPOINT_REQUIREMENTS do
2018-12-25 04:48:26 -05:00
authorize_destroy_release!
2018-12-25 02:36:25 -05:00
2018-12-25 04:48:26 -05:00
result = :: Releases :: DestroyService
. new ( user_project , current_user , declared_params ( include_missing : false ) )
. execute
2018-12-25 02:36:25 -05:00
if result [ :status ] == :success
2019-05-03 09:29:20 -04:00
present result [ :release ] , with : Entities :: Release , current_user : current_user
2018-12-25 02:36:25 -05:00
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
end
2018-12-13 06:08:53 -05:00
end
2018-12-25 04:48:26 -05: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 09:29:20 -04:00
def authorize_download_code!
2021-06-29 23:07:30 -04:00
authorize! :download_code , user_project
2019-05-03 09:29:20 -04:00
end
2020-03-25 14:08:10 -04:00
def authorize_create_evidence!
2020-10-07 11:08:40 -04:00
# extended in EE
2020-03-25 14:08:10 -04:00
end
2018-12-25 04:48:26 -05:00
def release
@release || = user_project . releases . find_by_tag ( params [ :tag ] )
end
2020-01-10 13:07:43 -05:00
def log_release_created_audit_event ( release )
2020-10-07 11:08:40 -04:00
# extended in EE
2020-01-10 13:07:43 -05:00
end
def log_release_updated_audit_event
2020-10-07 11:08:40 -04:00
# extended in EE
2020-01-10 13:07:43 -05:00
end
def log_release_milestones_updated_audit_event
2020-10-07 11:08:40 -04:00
# extended in EE
2020-01-10 13:07:43 -05:00
end
2021-04-30 02:10:26 -04:00
def release_cli?
request . env [ 'HTTP_USER_AGENT' ] & . include? ( RELEASE_CLI_USER_AGENT ) == true
end
def event_context
{
release_cli : release_cli?
}
end
def track_release_event
Gitlab :: Tracking . event ( options [ :for ] . name , options [ :route_options ] [ :named ] ,
project : user_project , user : current_user , ** event_context )
end
2018-12-25 04:48:26 -05:00
end
2018-12-13 06:08:53 -05:00
end
end
2020-01-10 13:07:43 -05:00
2021-05-11 17:10:21 -04:00
API :: Releases . prepend_mod_with ( 'API::Releases' )