2016-08-11 09:22:35 -04:00
|
|
|
module Ci
|
|
|
|
class ProcessPipelineService < BaseService
|
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def execute(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
2017-04-16 07:14:39 -04:00
|
|
|
update_retried
|
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
new_builds =
|
|
|
|
stage_indexes_of_created_builds.map do |index|
|
|
|
|
process_stage(index)
|
|
|
|
end
|
2016-08-29 10:47:31 -04:00
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
@pipeline.update_status
|
2016-10-07 08:52:30 -04:00
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
new_builds.flatten.any?
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_stage(index)
|
|
|
|
current_status = status_for_prior_stages(index)
|
|
|
|
|
2017-03-03 06:51:23 -05:00
|
|
|
return if HasStatus::BLOCKED_STATUS == current_status
|
|
|
|
|
2016-10-20 03:33:44 -04:00
|
|
|
if HasStatus::COMPLETED_STATUSES.include?(current_status)
|
|
|
|
created_builds_in_stage(index).select do |build|
|
2016-10-27 07:34:09 -04:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build) do |subject|
|
|
|
|
process_build(subject, current_status)
|
2016-10-20 03:33:44 -04:00
|
|
|
end
|
2016-09-12 06:49:58 -04:00
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_build(build, current_status)
|
|
|
|
if valid_statuses_for_when(build.when).include?(current_status)
|
2017-03-03 09:13:50 -05:00
|
|
|
build.action? ? build.actionize : build.enqueue
|
2016-08-11 09:22:35 -04:00
|
|
|
true
|
|
|
|
else
|
2016-10-26 17:03:58 -04:00
|
|
|
build.skip
|
2016-08-11 09:22:35 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_statuses_for_when(value)
|
|
|
|
case value
|
|
|
|
when 'on_success'
|
2016-12-05 08:48:37 -05:00
|
|
|
%w[success skipped]
|
2016-08-11 09:22:35 -04:00
|
|
|
when 'on_failure'
|
|
|
|
%w[failed]
|
|
|
|
when 'always'
|
2016-12-05 08:48:37 -05:00
|
|
|
%w[success failed skipped]
|
2017-03-03 09:13:50 -05:00
|
|
|
when 'manual'
|
2017-05-08 06:18:32 -04:00
|
|
|
%w[success skipped]
|
2016-08-11 09:22:35 -04:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_for_prior_stages(index)
|
|
|
|
pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
|
|
|
|
end
|
|
|
|
|
|
|
|
def stage_indexes_of_created_builds
|
|
|
|
created_builds.order(:stage_idx).pluck('distinct stage_idx')
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_builds_in_stage(index)
|
|
|
|
created_builds.where(stage_idx: index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_builds
|
|
|
|
pipeline.builds.created
|
|
|
|
end
|
2017-04-16 07:14:39 -04:00
|
|
|
|
|
|
|
# This method is for compatibility and data consistency and should be removed with 9.3 version of GitLab
|
|
|
|
# This replicates what is db/post_migrate/20170416103934_upate_retried_for_ci_build.rb
|
|
|
|
# and ensures that functionality will not be broken before migration is run
|
|
|
|
# this updates only when there are data that needs to be updated, there are two groups with no retried flag
|
|
|
|
def update_retried
|
|
|
|
# find the latest builds for each name
|
|
|
|
latest_statuses = pipeline.statuses.latest
|
|
|
|
.group(:name)
|
|
|
|
.having('count(*) > 1')
|
|
|
|
.pluck('max(id)', 'name')
|
|
|
|
|
|
|
|
# mark builds that are retried
|
|
|
|
pipeline.statuses.latest
|
|
|
|
.where(name: latest_statuses.map(&:second))
|
|
|
|
.where.not(id: latest_statuses.map(&:first))
|
|
|
|
.update_all(retried: true) if latest_statuses.any?
|
|
|
|
end
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
end
|