summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorTomasz Maczukin <tomasz@maczukin.pl>2016-01-14 13:09:54 +0100
committerTomasz Maczukin <tomasz@maczukin.pl>2016-01-14 13:09:54 +0100
commitaf3b97cdde6242b13da78f5f685975a98c35e322 (patch)
treeea6ad3f59a46147648e3eb2ca4c78d6494ccde19 /lib/api
parent41eedd45716cbb68fa8c6eaeb110ce2b1f612919 (diff)
parentf981da44ab88012db984e1457170067b345660c1 (diff)
downloadgitlab-ce-af3b97cdde6242b13da78f5f685975a98c35e322.tar.gz
Merge branch 'master' into ci/api-triggers
* master: Fix version Improve the consistency of commit titles, branch names, tag names, issue/MR titles, on their respective project pages Update CHANGELOG [ci skip] Add some cosmetic changes to variables API documentation [ci skip] Modify builds API documentation style [ci skip] Modify :ci_variable factory Add 'Build' prefix to Variables entry name in API docs index Fix some typos Add some fixes after review Remove blank line Update ./doc/api Change :variable_id to :key as resource ID in API Fix a typo in method description Add create feature to variables API Add missing 'not_found' checks in variables API Add delete feature to variables API Add update feature for variables API Add features for list and show details of variables in API Conflicts: doc/api/README.md lib/api/entities.rb
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/api.rb1
-rw-r--r--lib/api/entities.rb4
-rw-r--r--lib/api/variables.rb95
3 files changed, 100 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb
index 7834262d612..098dd975840 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -54,5 +54,6 @@ module API
mount Keys
mount Tags
mount Triggers
+ mount Variables
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 00c933237fe..d3803f4ec70 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -370,5 +370,9 @@ module API
class Trigger < Grape::Entity
expose :token, :created_at, :updated_at, :deleted_at, :last_used
end
+
+ class Variable < Grape::Entity
+ expose :key, :value
+ end
end
end
diff --git a/lib/api/variables.rb b/lib/api/variables.rb
new file mode 100644
index 00000000000..d9a055f6c92
--- /dev/null
+++ b/lib/api/variables.rb
@@ -0,0 +1,95 @@
+module API
+ # Projects variables API
+ class Variables < Grape::API
+ before { authenticate! }
+ before { authorize_admin_project }
+
+ resource :projects do
+ # Get project variables
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # page (optional) - The page number for pagination
+ # per_page (optional) - The value of items per page to show
+ # Example Request:
+ # GET /projects/:id/variables
+ get ':id/variables' do
+ variables = user_project.variables
+ present paginate(variables), with: Entities::Variable
+ end
+
+ # Get specific variable of a project
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # key (required) - The `key` of variable
+ # Example Request:
+ # GET /projects/:id/variables/:key
+ get ':id/variables/:key' do
+ key = params[:key]
+ variable = user_project.variables.find_by(key: key.to_s)
+
+ return not_found!('Variable') unless variable
+
+ present variable, with: Entities::Variable
+ end
+
+ # Create a new variable in project
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # key (required) - The key of variable
+ # value (required) - The value of variable
+ # Example Request:
+ # POST /projects/:id/variables
+ post ':id/variables' do
+ required_attributes! [:key, :value]
+
+ variable = user_project.variables.create(key: params[:key], value: params[:value])
+
+ if variable.valid?
+ present variable, with: Entities::Variable
+ else
+ render_validation_error!(variable)
+ end
+ end
+
+ # Update existing variable of a project
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # key (optional) - The `key` of variable
+ # value (optional) - New value for `value` field of variable
+ # Example Request:
+ # PUT /projects/:id/variables/:key
+ put ':id/variables/:key' do
+ variable = user_project.variables.find_by(key: params[:key].to_s)
+
+ return not_found!('Variable') unless variable
+
+ attrs = attributes_for_keys [:value]
+ if variable.update(attrs)
+ present variable, with: Entities::Variable
+ else
+ render_validation_error!(variable)
+ end
+ end
+
+ # Delete existing variable of a project
+ #
+ # Parameters:
+ # id (required) - The ID of a project
+ # key (required) - The ID of a variable
+ # Example Request:
+ # DELETE /projects/:id/variables/:key
+ delete ':id/variables/:key' do
+ variable = user_project.variables.find_by(key: params[:key].to_s)
+
+ return not_found!('Variable') unless variable
+ variable.destroy
+
+ present variable, with: Entities::Variable
+ end
+ end
+ end
+end