Extract CI/CD build retry code to separate service

This commit is contained in:
Grzegorz Bizon 2017-02-10 15:43:15 +01:00
parent 4e3fc21217
commit 9202b1f4df
2 changed files with 44 additions and 26 deletions

View file

@ -63,32 +63,7 @@ module Ci
end
def retry(build, user = nil)
new_build = Ci::Build.create(
ref: build.ref,
tag: build.tag,
options: build.options,
commands: build.commands,
tag_list: build.tag_list,
project: build.project,
pipeline: build.pipeline,
name: build.name,
allow_failure: build.allow_failure,
stage: build.stage,
stage_idx: build.stage_idx,
trigger_request: build.trigger_request,
yaml_variables: build.yaml_variables,
when: build.when,
user: user,
environment: build.environment,
status_event: 'enqueue'
)
MergeRequests::AddTodoWhenBuildFailsService
.new(build.project, nil)
.close(new_build)
build.pipeline.mark_as_processable_after_stage(build.stage_idx)
new_build
Ci::RetryBuildService.new(build, user).retry!
end
end

View file

@ -0,0 +1,43 @@
module Ci
class RetryBuildService
def initialize(build, user)
@build = build
@user = user
@pipeline = build.pipeline
end
def retry!
clone_build.tap do |new_build|
new_build.enqueue!
MergeRequests::AddTodoWhenBuildFailsService
.new(@build.project, @user)
.close(new_build)
@pipeline.mark_as_processable_after_stage(@build.stage_idx)
end
end
private
def clone_build
Ci::Build.create(
ref: @build.ref,
tag: @build.tag,
options: @build.options,
commands: @build.commands,
tag_list: @build.tag_list,
project: @build.project,
pipeline: @build.pipeline,
name: @build.name,
allow_failure: @build.allow_failure,
stage: @build.stage,
stage_idx: @build.stage_idx,
trigger_request: @build.trigger_request,
yaml_variables: @build.yaml_variables,
when: @build.when,
environment: @build.environment,
user: @user)
end
end
end