Add a separate CI/CD pipeline retry service class

This commit is contained in:
Grzegorz Bizon 2017-02-13 15:14:40 +01:00
parent 1be454e712
commit 19ef4083f3
3 changed files with 73 additions and 1 deletions

View file

@ -234,7 +234,9 @@ module Ci
end end
def mark_as_processable_after_stage(stage_idx) def mark_as_processable_after_stage(stage_idx)
builds.skipped.where('stage_idx > ?', stage_idx).find_each(&:process) builds.skipped
.where('stage_idx > ?', stage_idx)
.find_each(&:process)
end end
def latest? def latest?

View file

@ -0,0 +1,22 @@
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
@pipeline.stages.each do |stage|
stage.builds.failed_or_canceled.find_each do |build|
Ci::Build.retry(build, @user)
end
end
end
end
end

View file

@ -0,0 +1,48 @@
require 'spec_helper'
describe Ci::RetryPipelineService, '#execute', :services do
let(:user) { create(:user) }
let(:project) { create(:empty_project) }
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:service) { described_class.new(pipeline, user) }
context 'when user has ability to modify pipeline' do
let(:user) { create(:admin) }
context 'when there are failed builds in the last stage' do
before do
create_build(name: 'rspec 1', status: :success, stage_num: 0)
create_build(name: 'rspec 2', status: :failed, stage_num: 1)
create_build(name: 'rspec 3', status: :canceled, stage_num: 1)
end
it 'enqueues all builds in the last stage' do
service.execute
expect(build('rspec 2')).to be_pending
expect(build('rspec 3')).to be_pending
end
end
end
context 'when user is not allowed to retry pipeline' do
it 'raises an error' do
expect { service.execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
def build(name)
pipeline.statuses.find_by(name: name)
end
def create_build(name:, status:, stage_num:)
create(:ci_build, name: name,
status: status,
stage: "stage_#{stage_num}",
stage_idx: stage_num,
pipeline: pipeline) do |build|
pipeline.update_status
end
end
end