2015-12-31 14:19:13 +00:00
|
|
|
module API
|
|
|
|
class Variables < Grape::API
|
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
|
|
|
|
2016-10-12 18:38:33 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
|
2017-03-15 18:09:24 +00:00
|
|
|
resource :projects, requirements: { id: %r{[^/]+} } 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
|
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
|
|
|
|
2016-01-13 11:47:11 +00:00
|
|
|
return 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
|
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'
|
2017-05-25 11:49:46 +00:00
|
|
|
optional :protected, type: String, desc: 'Whether the variable is protected'
|
2016-10-12 18:38:33 +00:00
|
|
|
end
|
2015-12-31 21:30:07 +00:00
|
|
|
post ':id/variables' do
|
2016-10-12 18:38:33 +00:00
|
|
|
variable = user_project.variables.create(declared(params, include_parent_namespaces: false).to_h)
|
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'
|
2017-05-25 11:49:46 +00:00
|
|
|
optional :protected, type: String, desc: 'Whether the variable is protected'
|
2016-10-12 18:38:33 +00:00
|
|
|
end
|
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
|
|
|
|
2015-12-31 16:03:11 +00:00
|
|
|
return not_found!('Variable') unless variable
|
|
|
|
|
2017-05-25 11:49:46 +00:00
|
|
|
if variable.update(declared_params(include_missing: false).except(:key))
|
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
|
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
|
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
|
|
|
|
2017-02-20 18:18:12 +00:00
|
|
|
variable.destroy
|
2015-12-31 15:56:03 +00:00
|
|
|
end
|
2015-12-31 14:19:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|