Commit b8c88a83 authored by Robert Schilling's avatar Robert Schilling

Grapfiy the CI::Triggers API

parent a7a08738
module Ci module Ci
module API module API
# Build Trigger API
class Triggers < Grape::API class Triggers < Grape::API
resource :projects do resource :projects do
# Trigger a GitLab CI project build desc 'Trigger a GitLab CI project build' do
# success Entities::TriggerRequest
# Parameters: end
# id (required) - The ID of a CI project params do
# ref (required) - The name of project's branch or tag requires :id, type: Integer, desc: 'The ID of a CI project'
# token (required) - The uniq token of trigger requires :ref, type: String, desc: "The name of project's branch or tag"
# Example Request: requires :token, type: String, desc: 'The unique token of the trigger'
# POST /projects/:id/ref/:ref/trigger optional :variables, type: Hash, desc: 'Optional build variables'
end
post ":id/refs/:ref/trigger" do post ":id/refs/:ref/trigger" do
required_attributes! [:token] project = Project.find_by(ci_id: params[:id])
trigger = Ci::Trigger.find_by_token(params[:token])
project = Project.find_by(ci_id: params[:id].to_i)
trigger = Ci::Trigger.find_by_token(params[:token].to_s)
not_found! unless project && trigger not_found! unless project && trigger
unauthorized! unless trigger.project == project unauthorized! unless trigger.project == project
# validate variables # Validate variables
variables = params[:variables] variables = params[:variables].to_h
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) } 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) render_api_error!('variables needs to be a map of key-valued strings', 400)
end end
# convert variables from Mash to Hash
variables = variables.to_h
end
# create request and trigger builds # create request and trigger builds
trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref].to_s, variables) trigger_request = Ci::CreateTriggerRequestService.new.execute(project, trigger, params[:ref], variables)
if trigger_request if trigger_request
present trigger_request, with: Entities::TriggerRequest present trigger_request, with: Entities::TriggerRequest
else else
......
...@@ -60,7 +60,8 @@ describe Ci::API::Triggers do ...@@ -60,7 +60,8 @@ describe Ci::API::Triggers do
it 'validates variables to be a hash' do it 'validates variables to be a hash' do
post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: 'value') post ci_api("/projects/#{project.ci_id}/refs/master/trigger"), options.merge(variables: 'value')
expect(response).to have_http_status(400) expect(response).to have_http_status(400)
expect(json_response['message']).to eq('variables needs to be a hash')
expect(json_response['error']).to eq('variables is invalid')
end end
it 'validates variables needs to be a map of key-valued strings' do it 'validates variables needs to be a map of key-valued strings' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment