summaryrefslogtreecommitdiff
path: root/lib/ci/api/triggers.rb
diff options
context:
space:
mode:
authorPatricio Cano <suprnova32@gmail.com>2015-09-16 19:54:18 -0500
committerPatricio Cano <suprnova32@gmail.com>2015-09-16 19:54:18 -0500
commit1ddefa3beabed00f08c893ca66b9f004e3b4e4df (patch)
tree12c0546f4246b8ee5090c32996f3301207bcf93f /lib/ci/api/triggers.rb
parent1ef2ce95d507c3d21598f26dd8a0e77dfc3c33cf (diff)
parentcac969229aab74ab5dcd7682c4f1c3a74a17b9d6 (diff)
downloadgitlab-ce-1ddefa3beabed00f08c893ca66b9f004e3b4e4df.tar.gz
Merge branch 'master' into notification-levels
Diffstat (limited to 'lib/ci/api/triggers.rb')
-rw-r--r--lib/ci/api/triggers.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/ci/api/triggers.rb b/lib/ci/api/triggers.rb
new file mode 100644
index 00000000000..40907d6db54
--- /dev/null
+++ b/lib/ci/api/triggers.rb
@@ -0,0 +1,49 @@
+module Ci
+ module API
+ # Build Trigger API
+ class Triggers < Grape::API
+ resource :projects do
+ # Trigger a GitLab CI project build
+ #
+ # Parameters:
+ # id (required) - The ID of a CI project
+ # ref (required) - The name of project's branch or tag
+ # token (required) - The uniq token of trigger
+ # Example Request:
+ # POST /projects/:id/ref/:ref/trigger
+ post ":id/refs/:ref/trigger" do
+ required_attributes! [:token]
+
+ project = Ci::Project.find(params[:id])
+ trigger = Ci::Trigger.find_by_token(params[:token].to_s)
+ not_found! unless project && trigger
+ unauthorized! unless trigger.project == project
+
+ # validate variables
+ variables = params[:variables]
+ if variables
+ unless variables.is_a?(Hash)
+ render_api_error!('variables needs to be a hash', 400)
+ end
+
+ unless variables.all? { |key, value| key.is_a?(String) && value.is_a?(String) }
+ render_api_error!('variables needs to be a map of key-valued strings', 400)
+ end
+
+ # convert variables from Mash to Hash
+ variables = variables.to_h
+ end
+
+ # create request and trigger builds
+ trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables)
+ if trigger_request
+ present trigger_request, with: Entities::TriggerRequest
+ else
+ errors = 'No builds created'
+ render_api_error!(errors, 400)
+ end
+ end
+ end
+ end
+ end
+end