Commit 19ef4083 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add a separate CI/CD pipeline retry service class

parent 1be454e7
......@@ -234,7 +234,9 @@ module Ci
end
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
def latest?
......
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
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
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