Commit c0db4400 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Preserve base service abstraction for retry services

parent 21401a39
...@@ -63,7 +63,9 @@ module Ci ...@@ -63,7 +63,9 @@ module Ci
end end
def retry(build, current_user) def retry(build, current_user)
Ci::RetryBuildService.new(build, current_user).retry! Ci::RetryBuildService
.new(build.project, current_user)
.execute(build)
end end
end end
......
module Ci module Ci
class RetryBuildService class RetryBuildService < ::BaseService
include Gitlab::Allowable def execute(build)
self.retry(build).tap do |new_build|
MergeRequests::AddTodoWhenBuildFailsService
.new(build.project, current_user)
.close(new_build)
def initialize(build, user) build.pipeline
@build = build .mark_as_processable_after_stage(build.stage_idx)
@user = user end
@pipeline = build.pipeline
end end
def retry! def retry(build)
reprocess!.tap do |new_build| self.reprocess(build).tap do |new_build|
new_build.enqueue! new_build.enqueue!
MergeRequests::AddTodoWhenBuildFailsService
.new(@build.project, @user)
.close(new_build)
@pipeline.mark_as_processable_after_stage(@build.stage_idx)
end end
end end
def reprocess! def reprocess(build)
unless can?(@user, :update_build, @build) unless can?(current_user, :update_build, build)
raise Gitlab::Access::AccessDeniedError raise Gitlab::Access::AccessDeniedError
end end
Ci::Build.create( Ci::Build.create(
ref: @build.ref, ref: build.ref,
tag: @build.tag, tag: build.tag,
options: @build.options, options: build.options,
commands: @build.commands, commands: build.commands,
tag_list: @build.tag_list, tag_list: build.tag_list,
project: @build.project, project: build.project,
pipeline: @build.pipeline, pipeline: build.pipeline,
name: @build.name, name: build.name,
allow_failure: @build.allow_failure, allow_failure: build.allow_failure,
stage: @build.stage, stage: build.stage,
stage_idx: @build.stage_idx, stage_idx: build.stage_idx,
trigger_request: @build.trigger_request, trigger_request: build.trigger_request,
yaml_variables: @build.yaml_variables, yaml_variables: build.yaml_variables,
when: @build.when, when: build.when,
environment: @build.environment, environment: build.environment,
user: @user) user: current_user)
end end
end end
end end
module Ci module Ci
class RetryPipelineService class RetryPipelineService < ::BaseService
include Gitlab::Allowable def execute(pipeline)
def initialize(pipeline, user)
@pipeline = pipeline @pipeline = pipeline
@user = user
end
def execute unless can?(current_user, :update_pipeline, pipeline)
unless can?(@user, :update_pipeline, @pipeline)
raise Gitlab::Access::AccessDeniedError raise Gitlab::Access::AccessDeniedError
end end
## ##
# Reprocess builds in subsequent stages if any # Reprocess builds in subsequent stages
#
# TODO, refactor.
# #
@pipeline.builds pipeline.builds
.where('stage_idx > ?', resume_stage.index) .where('stage_idx > ?', resume_stage.index)
.failed_or_canceled.find_each do |build| .failed_or_canceled.find_each do |build|
Ci::RetryBuildService.new(build, @user).reprocess! Ci::RetryBuildService
.new(project, current_user)
.reprocess(build)
end end
## ##
# Retry builds in the first unsuccessful stage # Retry builds in the first unsuccessful stage
# #
resume_stage.builds.failed_or_canceled.find_each do |build| resume_stage.builds.failed_or_canceled.find_each do |build|
Ci::Build.retry(build, @user) Ci::RetryBuildService
.new(project, current_user)
.retry(build)
end end
##
# Mark skipped builds as processable again
#
pipeline.mark_as_processable_after_stage(resume_stage.index)
end end
private private
......
...@@ -7,13 +7,13 @@ describe Ci::RetryBuildService, :services do ...@@ -7,13 +7,13 @@ describe Ci::RetryBuildService, :services do
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
let(:service) do let(:service) do
described_class.new(build, user) described_class.new(project, user)
end end
describe '#retry!' do describe '#execute' do
let(:new_build) { service.retry! } let(:new_build) { service.execute(build) }
context 'when user has ability to retry build' do context 'when user has ability to execute build' do
before do before do
project.team << [user, :developer] project.team << [user, :developer]
end end
...@@ -30,7 +30,7 @@ describe Ci::RetryBuildService, :services do ...@@ -30,7 +30,7 @@ describe Ci::RetryBuildService, :services do
expect(MergeRequests::AddTodoWhenBuildFailsService) expect(MergeRequests::AddTodoWhenBuildFailsService)
.to receive_message_chain(:new, :close) .to receive_message_chain(:new, :close)
service.retry! service.execute(build)
end end
context 'when there are subsequent builds that are skipped' do context 'when there are subsequent builds that are skipped' do
...@@ -39,16 +39,16 @@ describe Ci::RetryBuildService, :services do ...@@ -39,16 +39,16 @@ describe Ci::RetryBuildService, :services do
end end
it 'resumes pipeline processing in subsequent stages' do it 'resumes pipeline processing in subsequent stages' do
service.retry! service.execute(build)
expect(subsequent_build.reload).to be_created expect(subsequent_build.reload).to be_created
end end
end end
end end
context 'when user does not have ability to retry build' do context 'when user does not have ability to execute build' do
it 'raises an error' do it 'raises an error' do
expect { service.retry! } expect { service.execute(build) }
.to raise_error Gitlab::Access::AccessDeniedError .to raise_error Gitlab::Access::AccessDeniedError
end end
end end
......
...@@ -4,7 +4,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -4,7 +4,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
let(:service) { described_class.new(pipeline, user) } let(:service) { described_class.new(project, user) }
context 'when user has ability to modify pipeline' do context 'when user has ability to modify pipeline' do
let(:user) { create(:admin) } let(:user) { create(:admin) }
...@@ -17,7 +17,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -17,7 +17,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
end end
it 'enqueues all builds in the last stage' do it 'enqueues all builds in the last stage' do
service.execute service.execute(pipeline)
expect(build('rspec 2')).to be_pending expect(build('rspec 2')).to be_pending
expect(build('rspec 3')).to be_pending expect(build('rspec 3')).to be_pending
...@@ -34,7 +34,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -34,7 +34,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
end end
it 'retries builds failed builds and marks subsequent for processing' do it 'retries builds failed builds and marks subsequent for processing' do
service.execute service.execute(pipeline)
expect(build('rspec 1')).to be_pending expect(build('rspec 1')).to be_pending
expect(build('rspec 2')).to be_pending expect(build('rspec 2')).to be_pending
...@@ -53,7 +53,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -53,7 +53,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
end end
it 'retries builds failed builds and marks subsequent for processing' do it 'retries builds failed builds and marks subsequent for processing' do
service.execute service.execute(pipeline)
expect(build('rspec 1')).to be_pending expect(build('rspec 1')).to be_pending
expect(build('rspec 2')).to be_pending expect(build('rspec 2')).to be_pending
...@@ -63,7 +63,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -63,7 +63,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
end end
it 'creates a new job for report job in this case' do it 'creates a new job for report job in this case' do
service.execute service.execute(pipeline)
expect(statuses.where(name: 'report 1').count).to eq 2 expect(statuses.where(name: 'report 1').count).to eq 2
end end
...@@ -72,7 +72,7 @@ describe Ci::RetryPipelineService, '#execute', :services do ...@@ -72,7 +72,7 @@ describe Ci::RetryPipelineService, '#execute', :services do
context 'when user is not allowed to retry pipeline' do context 'when user is not allowed to retry pipeline' do
it 'raises an error' do it 'raises an error' do
expect { service.execute } expect { service.execute(pipeline) }
.to raise_error Gitlab::Access::AccessDeniedError .to raise_error Gitlab::Access::AccessDeniedError
end 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