From 19ef4083f373dd5fdc6b4c59325a4cb14179e699 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 13 Feb 2017 15:14:40 +0100 Subject: [PATCH] Add a separate CI/CD pipeline retry service class --- app/models/ci/pipeline.rb | 4 +- app/services/ci/retry_pipeline_service.rb | 22 +++++++++ .../ci/retry_pipeline_service_spec.rb | 48 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 app/services/ci/retry_pipeline_service.rb create mode 100644 spec/services/ci/retry_pipeline_service_spec.rb diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 8371c2d8c72..72b09c12e01 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -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? diff --git a/app/services/ci/retry_pipeline_service.rb b/app/services/ci/retry_pipeline_service.rb new file mode 100644 index 00000000000..f873acc9964 --- /dev/null +++ b/app/services/ci/retry_pipeline_service.rb @@ -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 diff --git a/spec/services/ci/retry_pipeline_service_spec.rb b/spec/services/ci/retry_pipeline_service_spec.rb new file mode 100644 index 00000000000..6fc1d884920 --- /dev/null +++ b/spec/services/ci/retry_pipeline_service_spec.rb @@ -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