debian-mirror-gitlab/app/services/ci/retry_build_service.rb

57 lines
1.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Ci
class RetryBuildService < ::BaseService
2019-02-15 15:39:39 +05:30
CLONE_ACCESSORS = %i[pipeline project ref tag options name
2018-03-17 18:26:18 +05:30
allow_failure stage stage_id stage_idx trigger_request
2017-08-17 22:00:37 +05:30
yaml_variables when environment coverage_regex
2019-10-12 21:52:04 +05:30
description tag_list protected needs].freeze
2017-08-17 22:00:37 +05:30
def execute(build)
reprocess!(build).tap do |new_build|
build.pipeline.mark_as_processable_after_stage(build.stage_idx)
new_build.enqueue!
MergeRequests::AddTodoWhenBuildFailsService
.new(project, current_user)
.close(new_build)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def reprocess!(build)
unless can?(current_user, :update_build, build)
raise Gitlab::Access::AccessDeniedError
end
attributes = CLONE_ACCESSORS.map do |attribute|
2018-03-17 18:26:18 +05:30
[attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
attributes.push([:user, current_user])
2018-03-17 18:26:18 +05:30
build.retried = true
2017-08-17 22:00:37 +05:30
Ci::Build.transaction do
# mark all other builds of that name as retried
build.pipeline.builds.latest
.where(name: build.name)
.update_all(retried: true)
2019-12-21 20:55:43 +05:30
create_build!(attributes)
2017-08-17 22:00:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
private
def create_build!(attributes)
build = project.builds.new(Hash[attributes])
build.deployment = ::Gitlab::Ci::Pipeline::Seed::Deployment.new(build).to_resource
build.save!
build
end
2017-08-17 22:00:37 +05:30
end
end