Commit 99203bfe authored by Matija Čupić's avatar Matija Čupić

Destroy pipeline in service

Move all logic for destroying a Pipeline into a service so it's easily
reusable.
parent 5b45cd24
...@@ -144,6 +144,7 @@ class ProjectPolicy < BasePolicy ...@@ -144,6 +144,7 @@ class ProjectPolicy < BasePolicy
enable :destroy_merge_request enable :destroy_merge_request
enable :destroy_issue enable :destroy_issue
enable :remove_pages enable :remove_pages
enable :destroy_pipeline
enable :set_issue_iid enable :set_issue_iid
enable :set_issue_created_at enable :set_issue_created_at
......
# frozen_string_literal: true
module Ci
class DestroyPipelineService < BaseService
def execute(pipeline)
return false unless can?(current_user, :destroy_pipeline, project)
AuditEventService.new(current_user, pipeline).security_event
pipeline.destroy
end
end
end
...@@ -89,11 +89,11 @@ module API ...@@ -89,11 +89,11 @@ module API
requires :pipeline_id, type: Integer, desc: 'The pipeline ID' requires :pipeline_id, type: Integer, desc: 'The pipeline ID'
end end
delete ':id/pipelines/:pipeline_id' do delete ':id/pipelines/:pipeline_id' do
authorize! :admin_pipeline, user_project authorize! :destroy_pipeline, user_project
AuditEventService.new(current_user, user_project).security_event destroy_conditionally!(pipeline) do
::Ci::DestroyPipelineService.new(user_project, current_user).execute(pipeline)
destroy_conditionally!(pipeline) end
end end
desc 'Retry builds in the pipeline' do desc 'Retry builds in the pipeline' do
......
...@@ -440,34 +440,31 @@ describe API::Pipelines do ...@@ -440,34 +440,31 @@ describe API::Pipelines do
describe 'DELETE /projects/:id/pipelines/:pipeline_id' do describe 'DELETE /projects/:id/pipelines/:pipeline_id' do
context 'authorized user' do context 'authorized user' do
it 'deletes the pipeline' do let(:owner) { project.owner }
delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", user)
it 'destroys the pipeline' do
delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", owner)
expect(response).to have_gitlab_http_status(204) expect(response).to have_gitlab_http_status(204)
expect { pipeline.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { pipeline.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
it 'returns 404 when it does not exist' do it 'returns 404 when it does not exist' do
delete api("/projects/#{project.id}/pipelines/123456", user) delete api("/projects/#{project.id}/pipelines/123456", owner)
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
expect(json_response['message']).to eq '404 Not found' expect(json_response['message']).to eq '404 Not found'
end end
it 'logs an audit event' do it 'logs an audit event' do
expect { delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", user) }.to change { SecurityEvent.count }.by(1) expect { delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", owner) }.to change { SecurityEvent.count }.by(1)
end end
context 'when the pipeline has jobs' do context 'when the pipeline has jobs' do
let!(:pipeline) do
create(:ci_pipeline, project: project, sha: project.commit.id,
ref: project.default_branch, user: user)
end
let!(:build) { create(:ci_build, project: project, pipeline: pipeline) } let!(:build) { create(:ci_build, project: project, pipeline: pipeline) }
it 'deletes associated jobs' do it 'destroys associated jobs' do
delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", user) delete api("/projects/#{project.id}/pipelines/#{pipeline.id}", owner)
expect(response).to have_gitlab_http_status(204) expect(response).to have_gitlab_http_status(204)
expect { build.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { build.reload }.to raise_error(ActiveRecord::RecordNotFound)
...@@ -476,7 +473,7 @@ describe API::Pipelines do ...@@ -476,7 +473,7 @@ describe API::Pipelines do
end end
context 'unauthorized user' do context 'unauthorized user' do
it 'should not return a project pipeline' do it 'should return a 404' do
get api("/projects/#{project.id}/pipelines/#{pipeline.id}", non_member) get api("/projects/#{project.id}/pipelines/#{pipeline.id}", non_member)
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
......
# frozen_string_literal: true
require 'spec_helper'
describe ::Ci::DestroyPipelineService do
let(:project) { create(:project) }
let!(:pipeline) { create(:ci_pipeline, project: project) }
subject { described_class.new(project, user).execute(pipeline) }
context 'user is owner' do
let(:user) { project.owner }
it 'destroys the pipeline' do
subject
expect { pipeline.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'logs an audit event' do
expect { subject }.to change { SecurityEvent.count }.by(1)
end
context 'when the pipeline has jobs' do
let!(:build) { create(:ci_build, project: project, pipeline: pipeline) }
it 'destroys associated jobs' do
subject
expect { build.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'destroys associated stages' do
stages = pipeline.stages
subject
expect(stages).to all(raise_error(ActiveRecord::RecordNotFound))
end
context 'when job has artifacts' do
let!(:artifact) { create(:ci_job_artifact, :archive, job: build) }
it 'destroys associated artifacts' do
subject
expect { artifact.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
context 'user is not owner' do
let(:user) { create(:user) }
it 'returns false' do
is_expected.to eq(false)
end
it 'does not destroy the pipeline' do
subject
expect { pipeline.reload }.not_to raise_error
end
end
end
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