Commit af70ab20 authored by Furkan Ayhan's avatar Furkan Ayhan Committed by Fabio Pitino

Remove the save flag

parent 43bbf2a0
...@@ -11,23 +11,23 @@ module Ci ...@@ -11,23 +11,23 @@ module Ci
raise ArgumentError, "Pipeline must be persisted for this service to be used" unless @pipeline.persisted? raise ArgumentError, "Pipeline must be persisted for this service to be used" unless @pipeline.persisted?
end end
def execute!(job, save: true) def execute!(job, &block)
assign_pipeline_attributes(job) assign_pipeline_attributes(job)
Ci::Pipeline.transaction do Ci::Pipeline.transaction do
BulkInsertableAssociations.with_bulk_insert { job.save! } if save yield(job)
job.update_older_statuses_retried! if Feature.enabled?(:ci_fix_commit_status_retried, @pipeline.project, default_enabled: :yaml) job.update_older_statuses_retried! if Feature.enabled?(:ci_fix_commit_status_retried, @pipeline.project, default_enabled: :yaml)
end end
job ServiceResponse.success(payload: { job: job })
rescue StandardError => e
ServiceResponse.error(message: e.message, payload: { job: job })
end end
private private
def assign_pipeline_attributes(job) def assign_pipeline_attributes(job)
# these also ensures other invariants such as
# a job takes project and ref from the pipeline it belongs to
job.pipeline = @pipeline job.pipeline = @pipeline
job.project = @pipeline.project job.project = @pipeline.project
job.ref = @pipeline.ref job.ref = @pipeline.ref
......
...@@ -35,7 +35,11 @@ module Ci ...@@ -35,7 +35,11 @@ module Ci
check_access!(build) check_access!(build)
new_build = clone_build(build) new_build = clone_build(build)
::Ci::Pipelines::AddJobService.new(build.pipeline).execute!(new_build) ::Ci::Pipelines::AddJobService.new(build.pipeline).execute!(new_build) do |job|
BulkInsertableAssociations.with_bulk_insert do
job.save!
end
end
build.reset # refresh the data to get new values of `retried` and `processed`. build.reset # refresh the data to get new values of `retried` and `processed`.
new_build new_build
......
...@@ -32,10 +32,10 @@ module Projects ...@@ -32,10 +32,10 @@ module Projects
# Create status notifying the deployment of pages # Create status notifying the deployment of pages
@status = build_commit_status @status = build_commit_status
::Ci::Pipelines::AddJobService.new(@build.pipeline).execute!(@status) ::Ci::Pipelines::AddJobService.new(@build.pipeline).execute!(@status) do |job|
job.enqueue!
@status.enqueue! job.run!
@status.run! end
raise InvalidStateError, 'missing pages artifacts' unless build.artifacts? raise InvalidStateError, 'missing pages artifacts' unless build.artifacts?
raise InvalidStateError, 'build SHA is outdated for this ref' unless latest? raise InvalidStateError, 'build SHA is outdated for this ref' unless latest?
......
...@@ -99,40 +99,26 @@ module API ...@@ -99,40 +99,26 @@ module API
updatable_optional_attributes = %w[target_url description coverage] updatable_optional_attributes = %w[target_url description coverage]
status.assign_attributes(attributes_for_keys(updatable_optional_attributes)) status.assign_attributes(attributes_for_keys(updatable_optional_attributes))
if status.valid? render_validation_error!(status) unless status.valid?
::Ci::Pipelines::AddJobService.new(pipeline).execute!(status, save: false)
else
render_validation_error!(status)
end
begin response = ::Ci::Pipelines::AddJobService.new(pipeline).execute!(status) do |job|
case params[:state] apply_job_state!(job)
when 'pending' rescue ::StateMachines::InvalidTransition => e
status.enqueue! render_api_error!(e.message, 400)
when 'running' end
status.enqueue
status.run!
when 'success'
status.success!
when 'failed'
status.drop!(:api_failure)
when 'canceled'
status.cancel!
else
render_api_error!('invalid state', 400)
end
if pipeline.latest? render_validation_error!(response.payload[:job]) unless response.success?
MergeRequest.where(source_project: user_project, source_branch: ref)
.update_all(head_pipeline_id: pipeline.id)
end
present status, with: Entities::CommitStatus if pipeline.latest?
rescue StateMachines::InvalidTransition => e MergeRequest
render_api_error!(e.message, 400) .where(source_project: user_project, source_branch: ref)
.update_all(head_pipeline_id: pipeline.id)
end end
present response.payload[:job], with: Entities::CommitStatus
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
helpers do helpers do
def commit def commit
strong_memoize(:commit) do strong_memoize(:commit) do
...@@ -146,6 +132,24 @@ module API ...@@ -146,6 +132,24 @@ module API
pipelines = pipelines.for_id(params[:pipeline_id]) if params[:pipeline_id] pipelines = pipelines.for_id(params[:pipeline_id]) if params[:pipeline_id]
pipelines pipelines
end end
def apply_job_state!(job)
case params[:state]
when 'pending'
job.enqueue!
when 'running'
job.enqueue
job.run!
when 'success'
job.success!
when 'failed'
job.drop!(:api_failure)
when 'canceled'
job.cancel!
else
render_api_error!('invalid state', 400)
end
end
end end
end end
end end
......
...@@ -17,33 +17,56 @@ RSpec.describe Ci::Pipelines::AddJobService do ...@@ -17,33 +17,56 @@ RSpec.describe Ci::Pipelines::AddJobService do
end end
end end
it 'assigns pipeline attributes to the job' do describe '#execute!' do
expect do subject(:execute) do
service.execute!(job) service.execute!(job) do |job|
end.to change { job.slice(:pipeline, :project, :ref) }.to( job.save!
pipeline: pipeline, project: pipeline.project, ref: pipeline.ref end
) end
end
it 'returns the job itself' do it 'assigns pipeline attributes to the job' do
expect(service.execute!(job)).to eq(job) expect do
end execute
end.to change { job.slice(:pipeline, :project, :ref) }.to(
pipeline: pipeline, project: pipeline.project, ref: pipeline.ref
)
end
it 'returns a service response with the job as payload' do
expect(execute).to be_success
expect(execute.payload[:job]).to eq(job)
end
it 'calls update_older_statuses_retried!' do it 'calls update_older_statuses_retried!' do
expect(job).to receive(:update_older_statuses_retried!) expect(job).to receive(:update_older_statuses_retried!)
service.execute!(job) execute
end end
context 'when the FF ci_fix_commit_status_retried is disabled' do context 'when the block raises an error' do
before do subject(:execute) do
stub_feature_flags(ci_fix_commit_status_retried: false) service.execute!(job) do |job|
raise "this is an error"
end
end
it 'returns a service response with the error and the job as payload' do
expect(execute).to be_error
expect(execute.payload[:job]).to eq(job)
expect(execute.message).to eq('this is an error')
end
end end
it 'does not call update_older_statuses_retried!' do context 'when the FF ci_fix_commit_status_retried is disabled' do
expect(job).not_to receive(:update_older_statuses_retried!) before do
stub_feature_flags(ci_fix_commit_status_retried: false)
end
it 'does not call update_older_statuses_retried!' do
expect(job).not_to receive(:update_older_statuses_retried!)
service.execute!(job) execute
end
end end
end 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