summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2017-08-09 13:11:09 +0200
committerZ.J. van de Weg <git@zjvandeweg.nl>2017-08-09 13:11:09 +0200
commitf7a636c9285d1861e6b759c018c4dadc31f3244b (patch)
tree4a924ecdfc053293245a6b9888fe7b6c53a266b2
parent932a6e69b882334dd7e8fdf158ebbab4c620a2b5 (diff)
downloadgitlab-ce-zj-triggers-cleanup.tar.gz
Cleanup around use of Triggerszj-triggers-cleanup
Now that the internals have changed for a while, and %10.0 is the next release, we remove it. Closes #35623
-rw-r--r--app/services/ci/create_pipeline_service.rb2
-rw-r--r--app/services/ci/create_trigger_request_service.rb19
-rw-r--r--lib/api/api.rb1
-rw-r--r--lib/api/v3/triggers.rb104
-rw-r--r--lib/ci/api/api.rb1
-rw-r--r--lib/ci/api/triggers.rb39
-rw-r--r--spec/requests/api/v3/triggers_spec.rb231
-rw-r--r--spec/services/ci/create_pipeline_service_spec.rb58
8 files changed, 4 insertions, 451 deletions
diff --git a/app/services/ci/create_pipeline_service.rb b/app/services/ci/create_pipeline_service.rb
index 884b681ff81..d913fd20728 100644
--- a/app/services/ci/create_pipeline_service.rb
+++ b/app/services/ci/create_pipeline_service.rb
@@ -15,7 +15,7 @@ module Ci
pipeline_schedule: schedule
)
- result = validate(current_user || trigger_request.trigger.owner,
+ result = validate(current_user,
ignore_skip_ci: ignore_skip_ci,
save_on_errors: save_on_errors)
diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb
deleted file mode 100644
index b2aa457bbd5..00000000000
--- a/app/services/ci/create_trigger_request_service.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-# This class is deprecated because we're closing Ci::TriggerRequest.
-# New class is PipelineTriggerService (app/services/ci/pipeline_trigger_service.rb)
-# which is integrated with Ci::PipelineVariable instaed of Ci::TriggerRequest.
-# We remove this class after we removed v1 and v3 API. This class is still being
-# referred by such legacy code.
-module Ci
- module CreateTriggerRequestService
- Result = Struct.new(:trigger_request, :pipeline)
-
- def self.execute(project, trigger, ref, variables = nil)
- trigger_request = trigger.trigger_requests.create(variables: variables)
-
- pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref)
- .execute(:trigger, ignore_skip_ci: true, trigger_request: trigger_request)
-
- Result.new(trigger_request, pipeline)
- end
- end
-end
diff --git a/lib/api/api.rb b/lib/api/api.rb
index 94df543853b..3b82dc516b0 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -42,7 +42,6 @@ module API
mount ::API::V3::Tags
mount ::API::V3::Templates
mount ::API::V3::Todos
- mount ::API::V3::Triggers
mount ::API::V3::Users
mount ::API::V3::Variables
end
diff --git a/lib/api/v3/triggers.rb b/lib/api/v3/triggers.rb
deleted file mode 100644
index e9d4c35307b..00000000000
--- a/lib/api/v3/triggers.rb
+++ /dev/null
@@ -1,104 +0,0 @@
-module API
- module V3
- class Triggers < Grape::API
- include PaginationParams
-
- params do
- requires :id, type: String, desc: 'The ID of a project'
- end
- resource :projects, requirements: { id: %r{[^/]+} } 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", requirements: { ref: /.+/ } 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
- result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref].to_s, variables)
- pipeline = result.pipeline
-
- if pipeline.persisted?
- present result.trigger_request, with: ::API::V3::Entities::TriggerRequest
- else
- render_validation_error!(pipeline)
- 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::V3::Entities::Trigger
- end
- params do
- requires :token, type: String, desc: 'The unique token of trigger'
- end
- delete ':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
-
- trigger.destroy
-
- present trigger, with: ::API::V3::Entities::Trigger
- end
- end
- end
- end
-end
diff --git a/lib/ci/api/api.rb b/lib/ci/api/api.rb
index 24bb3649a76..ccd4d8cc349 100644
--- a/lib/ci/api/api.rb
+++ b/lib/ci/api/api.rb
@@ -33,7 +33,6 @@ module Ci
mount ::Ci::API::Builds
mount ::Ci::API::Runners
- mount ::Ci::API::Triggers
end
end
end
diff --git a/lib/ci/api/triggers.rb b/lib/ci/api/triggers.rb
deleted file mode 100644
index 6225203f223..00000000000
--- a/lib/ci/api/triggers.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-module Ci
- module API
- class Triggers < Grape::API
- resource :projects do
- desc 'Trigger a GitLab CI project build' do
- success Entities::TriggerRequest
- end
- params do
- requires :id, type: Integer, desc: 'The ID of a CI project'
- requires :ref, type: String, desc: "The name of project's branch or tag"
- requires :token, type: String, desc: 'The unique token of the trigger'
- optional :variables, type: Hash, desc: 'Optional build variables'
- end
- post ":id/refs/:ref/trigger" do
- project = Project.find_by(ci_id: params[:id])
- trigger = Ci::Trigger.find_by_token(params[:token])
- 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
- result = Ci::CreateTriggerRequestService.execute(project, trigger, params[:ref], variables)
- pipeline = result.pipeline
-
- if pipeline.persisted?
- present result.trigger_request, with: Entities::TriggerRequest
- else
- render_validation_error!(pipeline)
- end
- end
- end
- end
- end
-end
diff --git a/spec/requests/api/v3/triggers_spec.rb b/spec/requests/api/v3/triggers_spec.rb
deleted file mode 100644
index 60212660fb6..00000000000
--- a/spec/requests/api/v3/triggers_spec.rb
+++ /dev/null
@@ -1,231 +0,0 @@
-require 'spec_helper'
-
-describe API::V3::Triggers do
- let(:user) { create(:user) }
- let(:user2) { create(:user) }
- let!(:trigger_token) { 'secure_token' }
- let!(:project) { create(:project, :repository, creator: user) }
- let!(:master) { create(:project_member, :master, user: user, project: project) }
- 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 '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']['base'])
- .to contain_exactly('Reference not found')
- 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
-
- context 'when triggering a pipeline from a trigger token' do
- it 'creates builds from the ref given in the URL, not in the body' 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
-
- context 'when ref contains a dot' do
- it 'creates builds from the ref given in the URL, not in the body' do
- project.repository.create_file(user, '.gitlab/gitlabhq/new_feature.md', 'something valid', message: 'new_feature', branch_name: 'v.1-branch')
-
- expect do
- post v3_api("/projects/#{project.id}/ref/v.1-branch/trigger/builds?token=#{trigger_token}"), { ref: 'refs/heads/other-branch' }
- end.to change(project.builds, :count).by(4)
-
- expect(response).to have_http_status(201)
- 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
- expect do
- delete v3_api("/projects/#{project.id}/triggers/#{trigger.token}", user)
-
- expect(response).to have_http_status(200)
- end.to change{project.triggers.count}.by(-1)
- end
-
- it 'responds with 404 Not Found if requesting non-existing trigger' do
- delete 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 delete trigger' do
- delete 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 delete trigger' do
- delete v3_api("/projects/#{project.id}/triggers/#{trigger.token}")
-
- expect(response).to have_http_status(401)
- end
- end
- end
-end
diff --git a/spec/services/ci/create_pipeline_service_spec.rb b/spec/services/ci/create_pipeline_service_spec.rb
index 730df4e0336..1cf28d47fdc 100644
--- a/spec/services/ci/create_pipeline_service_spec.rb
+++ b/spec/services/ci/create_pipeline_service_spec.rb
@@ -10,19 +10,14 @@ describe Ci::CreatePipelineService do
end
describe '#execute' do
- def execute_service(
- source: :push,
- after: project.commit.id,
- message: 'Message',
- ref: ref_name,
- trigger_request: nil)
+ def execute_service(source: :push, after: project.commit.id, message: 'Message',
+ ref: ref_name, trigger_request: nil)
params = { ref: ref,
before: '00000000',
after: after,
commits: [{ message: message }] }
- described_class.new(project, user, params).execute(
- source, trigger_request: trigger_request)
+ described_class.new(project, user, params).execute(source, trigger_request: trigger_request)
end
context 'valid params' do
@@ -366,53 +361,6 @@ describe Ci::CreatePipelineService do
expect(Ci::Pipeline.count).to eq(1)
end
end
-
- context 'when trigger belongs to no one' do
- let(:user) {}
- let(:trigger_request) { create(:ci_trigger_request) }
-
- it 'does not create a pipeline' do
- expect(execute_service(trigger_request: trigger_request))
- .not_to be_persisted
- expect(Ci::Pipeline.count).to eq(0)
- end
- end
-
- context 'when trigger belongs to a developer' do
- let(:user) {}
-
- let(:trigger_request) do
- create(:ci_trigger_request).tap do |request|
- user = create(:user)
- project.add_developer(user)
- request.trigger.update(owner: user)
- end
- end
-
- it 'does not create a pipeline' do
- expect(execute_service(trigger_request: trigger_request))
- .not_to be_persisted
- expect(Ci::Pipeline.count).to eq(0)
- end
- end
-
- context 'when trigger belongs to a master' do
- let(:user) {}
-
- let(:trigger_request) do
- create(:ci_trigger_request).tap do |request|
- user = create(:user)
- project.add_master(user)
- request.trigger.update(owner: user)
- end
- end
-
- it 'does not create a pipeline' do
- expect(execute_service(trigger_request: trigger_request))
- .to be_persisted
- expect(Ci::Pipeline.count).to eq(1)
- end
- end
end
context 'when ref is a protected branch' do