2018-09-29 22:34:47 +00:00
# frozen_string_literal: true
2015-12-31 14:19:13 +00:00
module API
2020-06-29 21:09:07 +00:00
class Variables < Grape :: API :: Instance
2016-11-21 19:15:46 +00:00
include PaginationParams
2015-12-31 14:19:13 +00:00
before { authenticate! }
2016-02-01 22:58:04 +00:00
before { authorize! :admin_build , user_project }
2015-12-31 14:19:13 +00:00
2019-03-04 13:50:15 +00:00
helpers do
def filter_variable_parameters ( params )
# This method exists so that EE can more easily filter out certain
# parameters, without having to modify the source code directly.
params
end
end
2016-10-12 18:38:33 +00:00
params do
requires :id , type : String , desc : 'The ID of a project'
end
2019-01-16 12:09:29 +00:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2016-10-12 18:38:33 +00:00
desc 'Get project variables' do
success Entities :: Variable
end
params do
2016-11-21 19:15:46 +00:00
use :pagination
2016-10-12 18:38:33 +00:00
end
2015-12-31 14:19:13 +00:00
get ':id/variables' do
variables = user_project . variables
present paginate ( variables ) , with : Entities :: Variable
end
2016-10-12 18:38:33 +00:00
desc 'Get a specific variable from a project' do
success Entities :: Variable
end
params do
requires :key , type : String , desc : 'The key of the variable'
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2016-01-07 12:49:38 +00:00
get ':id/variables/:key' do
key = params [ :key ]
2016-11-24 12:30:53 +00:00
variable = user_project . variables . find_by ( key : key )
2015-12-31 14:19:13 +00:00
2018-04-18 09:19:40 +00:00
break not_found! ( 'Variable' ) unless variable
2015-12-31 16:03:11 +00:00
2016-01-13 11:47:11 +00:00
present variable , with : Entities :: Variable
2015-12-31 14:19:13 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2015-12-31 15:25:49 +00:00
2016-10-12 18:38:33 +00:00
desc 'Create a new variable in a project' do
success Entities :: Variable
end
params do
requires :key , type : String , desc : 'The key of the variable'
requires :value , type : String , desc : 'The value of the variable'
2019-05-27 00:51:48 +00:00
optional :protected , type : Boolean , desc : 'Whether the variable is protected'
2019-05-26 22:29:41 +00:00
optional :masked , type : Boolean , desc : 'Whether the variable is masked'
2020-06-26 00:09:13 +00:00
optional :variable_type , type : String , values : :: Ci :: Variable . variable_types . keys , desc : 'The type of variable, must be one of env_var or file. Defaults to env_var'
2019-08-08 18:51:52 +00:00
optional :environment_scope , type : String , desc : 'The environment_scope of the variable'
2016-10-12 18:38:33 +00:00
end
2015-12-31 21:30:07 +00:00
post ':id/variables' do
2017-07-06 07:45:38 +00:00
variable_params = declared_params ( include_missing : false )
2019-03-04 13:50:15 +00:00
variable_params = filter_variable_parameters ( variable_params )
2017-07-06 07:45:38 +00:00
variable = user_project . variables . create ( variable_params )
2015-12-31 21:30:07 +00:00
2016-01-13 11:47:11 +00:00
if variable . valid?
present variable , with : Entities :: Variable
else
render_validation_error! ( variable )
end
2015-12-31 21:30:07 +00:00
end
2016-10-12 18:38:33 +00:00
desc 'Update an existing variable from a project' do
success Entities :: Variable
end
params do
optional :key , type : String , desc : 'The key of the variable'
optional :value , type : String , desc : 'The value of the variable'
2019-05-27 00:51:48 +00:00
optional :protected , type : Boolean , desc : 'Whether the variable is protected'
2019-05-26 22:29:41 +00:00
optional :masked , type : Boolean , desc : 'Whether the variable is masked'
2020-06-26 00:09:13 +00:00
optional :variable_type , type : String , values : :: Ci :: Variable . variable_types . keys , desc : 'The type of variable, must be one of env_var or file'
2019-08-08 18:51:52 +00:00
optional :environment_scope , type : String , desc : 'The environment_scope of the variable'
2016-10-12 18:38:33 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2016-01-07 12:49:38 +00:00
put ':id/variables/:key' do
2016-10-12 18:38:33 +00:00
variable = user_project . variables . find_by ( key : params [ :key ] )
2015-12-31 15:25:49 +00:00
2018-04-18 09:19:40 +00:00
break not_found! ( 'Variable' ) unless variable
2015-12-31 16:03:11 +00:00
2017-07-06 07:45:38 +00:00
variable_params = declared_params ( include_missing : false ) . except ( :key )
2019-03-04 13:50:15 +00:00
variable_params = filter_variable_parameters ( variable_params )
2017-07-06 07:45:38 +00:00
if variable . update ( variable_params )
2016-01-13 11:47:11 +00:00
present variable , with : Entities :: Variable
else
render_validation_error! ( variable )
end
2015-12-31 15:25:49 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2015-12-31 15:56:03 +00:00
2016-10-12 18:38:33 +00:00
desc 'Delete an existing variable from a project' do
success Entities :: Variable
end
params do
requires :key , type : String , desc : 'The key of the variable'
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2016-01-07 12:49:38 +00:00
delete ':id/variables/:key' do
2016-10-12 18:38:33 +00:00
variable = user_project . variables . find_by ( key : params [ :key ] )
2017-02-20 19:32:44 +00:00
not_found! ( 'Variable' ) unless variable
2015-12-31 21:30:07 +00:00
2020-01-16 18:08:46 +00:00
# Variables don't have a timestamp. Therefore, destroy unconditionally.
2017-02-20 18:18:12 +00:00
variable . destroy
2020-01-16 18:08:46 +00:00
no_content!
2015-12-31 15:56:03 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2015-12-31 14:19:13 +00:00
end
end
end