summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2017-03-05 18:49:30 +0100
committerKamil Trzcinski <ayufan@ayufan.eu>2017-03-05 18:49:30 +0100
commitc33f09d2549d2228a5ac7ceb7cb099774fbd826e (patch)
tree536228c12f3143563a00862338f9cb9f3a94df9f
parent8a5557ce514fc5bd1a8e4e236713fd0910ce7e78 (diff)
downloadgitlab-ce-c33f09d2549d2228a5ac7ceb7cb099774fbd826e.tar.gz
Update triggers API
-rw-r--r--lib/api/entities.rb4
-rw-r--r--lib/api/triggers.rb68
-rw-r--r--lib/api/v3/entities.rb8
-rw-r--r--lib/api/v3/triggers.rb77
-rw-r--r--spec/requests/api/v3/triggers_spec.rb171
5 files changed, 311 insertions, 17 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index d2d21f5d03a..82d05d85ead 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -592,10 +592,6 @@ module API
end
end
- class TriggerRequest < Grape::Entity
- expose :id, :variables
- end
-
class Runner < Grape::Entity
expose :id
expose :description
diff --git a/lib/api/triggers.rb b/lib/api/triggers.rb
index b7c9c5f2b7f..c324708a16d 100644
--- a/lib/api/triggers.rb
+++ b/lib/api/triggers.rb
@@ -6,7 +6,7 @@ module API
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do
- desc 'Trigger a GitLab project build' do
+ desc 'Trigger a GitLab project pipeline' do
success Entities::TriggerRequest
end
params do
@@ -14,7 +14,7 @@ module API
requires :token, type: String, desc: 'The unique token of trigger'
optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
end
- post ":id/(ref/:ref/)trigger/builds" do
+ post ":id/(ref/:ref/)trigger/pipeline" do
project = find_project(params[:id])
trigger = Ci::Trigger.find_by_token(params[:token].to_s)
not_found! unless project && trigger
@@ -29,9 +29,9 @@ module API
# 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
+ present trigger_request.pipeline, with: Entities::Pipeline
else
- errors = 'No builds created'
+ errors = 'No pipeline create'
render_api_error!(errors, 400)
end
end
@@ -55,13 +55,13 @@ module API
success Entities::Trigger
end
params do
- requires :token, type: String, desc: 'The unique token of trigger'
+ requires :trigger_id, type: Integer, desc: 'The trigger ID'
end
- get ':id/triggers/:token' do
+ get ':id/triggers/:trigger_id' do
authenticate!
authorize! :admin_build, user_project
- trigger = user_project.triggers.find_by(token: params[:token].to_s)
+ trigger = user_project.triggers.find(params[:trigger_id])
return not_found!('Trigger') unless trigger
present trigger, with: Entities::Trigger
@@ -70,26 +70,72 @@ module API
desc 'Create a trigger' do
success Entities::Trigger
end
+ params do
+ requires :description, type: String, desc: 'The trigger description'
+ end
post ':id/triggers' do
authenticate!
authorize! :admin_build, user_project
- trigger = user_project.triggers.create
+ trigger = user_project.triggers.create(
+ declared_params(include_missing: false).merge(owner: current_user))
+
+ if trigger.valid?
+ present trigger, with: Entities::Trigger
+ else
+ render_validation_error!(trigger)
+ end
+ end
+
+ desc 'Update a trigger' do
+ success Entities::Trigger
+ end
+ params do
+ requires :trigger_id, type: Integer, desc: 'The trigger ID'
+ optional :description, type: String, desc: 'The trigger description'
+ end
+ delete ':id/triggers/:trigger_id' do
+ authenticate!
+ authorize! :admin_build, user_project
+
+ trigger = user_project.triggers.find(params[:trigger_id])
+ return not_found!('Trigger') unless trigger
+ trigger = trigger.update(declared_params(include_missing: false))
present trigger, with: Entities::Trigger
end
+ desc 'Take ownership of trigger' do
+ success Entities::Trigger
+ end
+ params do
+ requires :trigger_id, type: Integer, desc: 'The trigger ID'
+ end
+ post ':id/triggers/:trigger_id/take' do
+ authenticate!
+ authorize! :admin_build, user_project
+
+ trigger = user_project.triggers.find(params[:trigger_id])
+ return not_found!('Trigger') unless trigger
+
+ if trigger.update(owner: current_user)
+ present trigger, with: Entities::Trigger
+ else
+ render_validation_error!(trigger)
+ end
+ end
+
desc 'Delete a trigger' do
success Entities::Trigger
end
params do
- requires :token, type: String, desc: 'The unique token of trigger'
+ requires :trigger_id, type: Integer, desc: 'The trigger ID'
end
- delete ':id/triggers/:token' do
+ delete ':id/triggers/:trigger_id' do
authenticate!
authorize! :admin_build, user_project
- trigger = user_project.triggers.find_by(token: params[:token].to_s)
+ trigger = user_project.triggers.find(params[:trigger_id])
return not_found!('Trigger') unless trigger
trigger.destroy
diff --git a/lib/api/v3/entities.rb b/lib/api/v3/entities.rb
index 270d99a2348..29a44d4c7e5 100644
--- a/lib/api/v3/entities.rb
+++ b/lib/api/v3/entities.rb
@@ -186,6 +186,14 @@ module API
class Environment < ::API::Entities::EnvironmentBasic
expose :project, using: Entities::Project
end
+
+ class Trigger < Grape::Entity
+ expose :token, :created_at, :updated_at, :deleted_at, :last_used
+ end
+
+ class TriggerRequest < Grape::Entity
+ expose :id, :variables
+ end
end
end
end
diff --git a/lib/api/v3/triggers.rb b/lib/api/v3/triggers.rb
index 4051d4bca8d..1dfdb6a5956 100644
--- a/lib/api/v3/triggers.rb
+++ b/lib/api/v3/triggers.rb
@@ -7,8 +7,81 @@ module API
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects do
+ desc 'Trigger a GitLab project build' do
+ success ::API::V3::Entities::TriggerRequest
+ end
+ params do
+ requires :ref, type: String, desc: 'The commit sha or name of a branch or tag'
+ requires :token, type: String, desc: 'The unique token of trigger'
+ optional :variables, type: Hash, desc: 'The list of variables to be injected into build'
+ end
+ post ":id/(ref/:ref/)trigger/builds" do
+ project = find_project(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].to_h
+ 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
+
+ # 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: ::API::V3::Entities::TriggerRequest
+ else
+ errors = 'No builds created'
+ render_api_error!(errors, 400)
+ end
+ end
+
+ desc 'Get triggers list' do
+ success ::API::V3::Entities::Trigger
+ end
+ params do
+ use :pagination
+ end
+ get ':id/triggers' do
+ authenticate!
+ authorize! :admin_build, user_project
+
+ triggers = user_project.triggers.includes(:trigger_requests)
+
+ present paginate(triggers), with: ::API::V3::Entities::Trigger
+ end
+
+ desc 'Get specific trigger of a project' do
+ success ::API::V3::Entities::Trigger
+ end
+ params do
+ requires :token, type: String, desc: 'The unique token of trigger'
+ end
+ get ':id/triggers/:token' do
+ authenticate!
+ authorize! :admin_build, user_project
+
+ trigger = user_project.triggers.find_by(token: params[:token].to_s)
+ return not_found!('Trigger') unless trigger
+
+ present trigger, with: ::API::V3::Entities::Trigger
+ end
+
+ desc 'Create a trigger' do
+ success ::API::V3::Entities::Trigger
+ end
+ post ':id/triggers' do
+ authenticate!
+ authorize! :admin_build, user_project
+
+ trigger = user_project.triggers.create
+
+ present trigger, with: ::API::V3::Entities::Trigger
+ end
+
desc 'Delete a trigger' do
- success ::API::Entities::Trigger
+ success ::API::V3::Entities::Trigger
end
params do
requires :token, type: String, desc: 'The unique token of trigger'
@@ -22,7 +95,7 @@ module API
trigger.destroy
- present trigger, with: ::API::Entities::Trigger
+ present trigger, with: ::API::V3::Entities::Trigger
end
end
end
diff --git a/spec/requests/api/v3/triggers_spec.rb b/spec/requests/api/v3/triggers_spec.rb
index 721ce4a361b..4819269d69f 100644
--- a/spec/requests/api/v3/triggers_spec.rb
+++ b/spec/requests/api/v3/triggers_spec.rb
@@ -11,6 +11,177 @@ describe API::V3::Triggers do
let!(:developer) { create(:project_member, :developer, user: user2, project: project) }
let!(:trigger) { create(:ci_trigger, project: project, token: trigger_token) }
+ describe 'POST /projects/:project_id/trigger' do
+ let!(:project2) { create(:project) }
+ let(:options) do
+ {
+ token: trigger_token
+ }
+ end
+
+ before do
+ stub_ci_pipeline_to_return_yaml_file
+ end
+
+ context 'Handles errors' do
+ it 'returns bad request if token is missing' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), ref: 'master'
+ expect(response).to have_http_status(400)
+ end
+
+ it 'returns not found if project is not found' do
+ post v3_api('/projects/0/trigger/builds'), options.merge(ref: 'master')
+ expect(response).to have_http_status(404)
+ end
+
+ it 'returns unauthorized if token is for different project' do
+ post v3_api("/projects/#{project2.id}/trigger/builds"), options.merge(ref: 'master')
+ expect(response).to have_http_status(401)
+ end
+ end
+
+ context 'Have a commit' do
+ let(:pipeline) { project.pipelines.last }
+
+ it 'creates builds' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'master')
+ expect(response).to have_http_status(201)
+ pipeline.builds.reload
+ expect(pipeline.builds.pending.size).to eq(2)
+ expect(pipeline.builds.size).to eq(5)
+ end
+
+ it 'creates builds on webhook from other gitlab repository and branch' do
+ expect do
+ post v3_api("/projects/#{project.id}/ref/master/trigger/builds?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
+ end.to change(project.builds, :count).by(5)
+ expect(response).to have_http_status(201)
+ end
+
+ it 'returns bad request with no builds created if there\'s no commit for that ref' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), options.merge(ref: 'other-branch')
+ expect(response).to have_http_status(400)
+ expect(json_response['message']).to eq('No builds created')
+ end
+
+ context 'Validates variables' do
+ let(:variables) do
+ { 'TRIGGER_KEY' => 'TRIGGER_VALUE' }
+ end
+
+ it 'validates variables to be a hash' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), options.merge(variables: 'value', ref: 'master')
+ expect(response).to have_http_status(400)
+ expect(json_response['error']).to eq('variables is invalid')
+ end
+
+ it 'validates variables needs to be a map of key-valued strings' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), options.merge(variables: { key: %w(1 2) }, ref: 'master')
+ expect(response).to have_http_status(400)
+ expect(json_response['message']).to eq('variables needs to be a map of key-valued strings')
+ end
+
+ it 'creates trigger request with variables' do
+ post v3_api("/projects/#{project.id}/trigger/builds"), options.merge(variables: variables, ref: 'master')
+ expect(response).to have_http_status(201)
+ pipeline.builds.reload
+ expect(pipeline.builds.first.trigger_request.variables).to eq(variables)
+ end
+ end
+ end
+ end
+
+ describe 'GET /projects/:id/triggers' do
+ context 'authenticated user with valid permissions' do
+ it 'returns list of triggers' do
+ get v3_api("/projects/#{project.id}/triggers", user)
+
+ expect(response).to have_http_status(200)
+ expect(response).to include_pagination_headers
+ expect(json_response).to be_a(Array)
+ expect(json_response[0]).to have_key('token')
+ end
+ end
+
+ context 'authenticated user with invalid permissions' do
+ it 'does not return triggers list' do
+ get v3_api("/projects/#{project.id}/triggers", user2)
+
+ expect(response).to have_http_status(403)
+ end
+ end
+
+ context 'unauthenticated user' do
+ it 'does not return triggers list' do
+ get v3_api("/projects/#{project.id}/triggers")
+
+ expect(response).to have_http_status(401)
+ end
+ end
+ end
+
+ describe 'GET /projects/:id/triggers/:token' do
+ context 'authenticated user with valid permissions' do
+ it 'returns trigger details' do
+ get v3_api("/projects/#{project.id}/triggers/#{trigger.token}", user)
+
+ expect(response).to have_http_status(200)
+ expect(json_response).to be_a(Hash)
+ end
+
+ it 'responds with 404 Not Found if requesting non-existing trigger' do
+ get v3_api("/projects/#{project.id}/triggers/abcdef012345", user)
+
+ expect(response).to have_http_status(404)
+ end
+ end
+
+ context 'authenticated user with invalid permissions' do
+ it 'does not return triggers list' do
+ get v3_api("/projects/#{project.id}/triggers/#{trigger.token}", user2)
+
+ expect(response).to have_http_status(403)
+ end
+ end
+
+ context 'unauthenticated user' do
+ it 'does not return triggers list' do
+ get v3_api("/projects/#{project.id}/triggers/#{trigger.token}")
+
+ expect(response).to have_http_status(401)
+ end
+ end
+ end
+
+ describe 'POST /projects/:id/triggers' do
+ context 'authenticated user with valid permissions' do
+ it 'creates trigger' do
+ expect do
+ post v3_api("/projects/#{project.id}/triggers", user)
+ end.to change{project.triggers.count}.by(1)
+
+ expect(response).to have_http_status(201)
+ expect(json_response).to be_a(Hash)
+ end
+ end
+
+ context 'authenticated user with invalid permissions' do
+ it 'does not create trigger' do
+ post v3_api("/projects/#{project.id}/triggers", user2)
+
+ expect(response).to have_http_status(403)
+ end
+ end
+
+ context 'unauthenticated user' do
+ it 'does not create trigger' do
+ post v3_api("/projects/#{project.id}/triggers")
+
+ expect(response).to have_http_status(401)
+ end
+ end
+ end
+
describe 'DELETE /projects/:id/triggers/:token' do
context 'authenticated user with valid permissions' do
it 'deletes trigger' do