summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2015-08-06 16:44:45 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2015-08-21 14:49:36 +0100
commitae8f755fc81160b695e44bbfb00b1225527bf5f9 (patch)
tree755e60bad6f63014e3ae7eea8cd0ad018a611956 /lib
parent1ba150e1cf346239eafec3d530b3de3325501893 (diff)
downloadgitlab-ci-ae8f755fc81160b695e44bbfb00b1225527bf5f9.tar.gz
Initial support for build triggers
Diffstat (limited to 'lib')
-rw-r--r--lib/api/api.rb1
-rw-r--r--lib/api/entities.rb5
-rw-r--r--lib/api/triggers.rb47
3 files changed, 53 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb
index d1127ed..6645ff1 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -30,5 +30,6 @@ module API
mount Runners
mount Projects
mount Forks
+ mount Triggers
end
end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 464667f..3423f4a 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -30,5 +30,10 @@ module API
class WebHook < Grape::Entity
expose :id, :project_id, :url
end
+
+ class TriggerRequest < Grape::Entity
+ expose :id, :variables
+ expose :commit, using: Commit
+ end
end
end
diff --git a/lib/api/triggers.rb b/lib/api/triggers.rb
new file mode 100644
index 0000000..1e4935a
--- /dev/null
+++ b/lib/api/triggers.rb
@@ -0,0 +1,47 @@
+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 = Project.find(params[:id])
+ trigger = 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 = 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