2017-02-13 14:14:40 +00:00
|
|
|
module Ci
|
|
|
|
class RetryPipelineService
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
|
|
|
def initialize(pipeline, user)
|
|
|
|
@pipeline = pipeline
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
unless can?(@user, :update_pipeline, @pipeline)
|
|
|
|
raise Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
|
2017-02-13 15:38:08 +00:00
|
|
|
##
|
|
|
|
# Reprocess builds in subsequent stages if any
|
|
|
|
#
|
|
|
|
# TODO, refactor.
|
|
|
|
#
|
|
|
|
@pipeline.builds
|
|
|
|
.where('stage_idx > ?', resume_stage.index)
|
|
|
|
.failed_or_canceled.find_each do |build|
|
|
|
|
Ci::RetryBuildService.new(build, @user).reprocess!
|
2017-02-13 14:14:40 +00:00
|
|
|
end
|
2017-02-13 15:38:08 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Retry builds in the first unsuccessful stage
|
|
|
|
#
|
|
|
|
resume_stage.builds.failed_or_canceled.find_each do |build|
|
|
|
|
Ci::Build.retry(build, @user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def resume_stage
|
|
|
|
@resume_stage ||= @pipeline.stages.find do |stage|
|
|
|
|
stage.failed? || stage.canceled?
|
2017-02-13 14:14:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|