summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2015-12-10 18:04:40 +0100
committerKamil Trzcinski <ayufan@ayufan.eu>2015-12-11 18:02:09 +0100
commit3d9ce37a48615032c786913acdde1eedba351361 (patch)
tree97999e407b8823327b4558060b370af829fdbd1f /lib/api
parent64bfd9d71a4017e0b5336a2c1565926f4b8beedd (diff)
downloadgitlab-ce-3d9ce37a48615032c786913acdde1eedba351361.tar.gz
Reimplement Trigger API
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/api.rb1
-rw-r--r--lib/api/entities.rb4
-rw-r--r--lib/api/triggers.rb48
3 files changed, 53 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb
index fe1bf8a4816..7834262d612 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -53,5 +53,6 @@ module API
mount Settings
mount Keys
mount Tags
+ mount Triggers
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 381babe291b..b1cd80bdf65 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -361,5 +361,9 @@ module API
end
end
end
+
+ class TriggerRequest < Grape::Entity
+ expose :id, :variables
+ end
end
end
diff --git a/lib/api/triggers.rb b/lib/api/triggers.rb
new file mode 100644
index 00000000000..b713c00e82a
--- /dev/null
+++ b/lib/api/triggers.rb
@@ -0,0 +1,48 @@
+module API
+ # Triggers API
+ class Triggers < Grape::API
+ resource :projects do
+ # Trigger a GitLab 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
+ # variables (optional) - The list of variables to be injected into build
+ # Example Request:
+ # POST /projects/:id/trigger/builds
+ post ":id/trigger/builds" do
+ required_attributes! [:ref, :token]
+
+ project = Project.find_with_namespace(id) || Project.find_by(id: 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