diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-27 18:09:21 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-27 18:09:21 +0000 |
commit | e0fa0638a422c3e20d4423c9bb69d79afc9c7d3d (patch) | |
tree | 9abb3c0706576bbda895fe9539a55556930606e2 /app/services/ci | |
parent | f8d15ca65390475e356b06dedc51e10ccd179f86 (diff) | |
download | gitlab-ce-e0fa0638a422c3e20d4423c9bb69d79afc9c7d3d.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/ci')
-rw-r--r-- | app/services/ci/update_ci_ref_status_service.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/app/services/ci/update_ci_ref_status_service.rb b/app/services/ci/update_ci_ref_status_service.rb new file mode 100644 index 00000000000..e5e5b94b629 --- /dev/null +++ b/app/services/ci/update_ci_ref_status_service.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +module Ci + class UpdateCiRefStatusService + include Gitlab::OptimisticLocking + + attr_reader :pipeline + + def initialize(pipeline) + @pipeline = pipeline + end + + def call + save.tap { |success| after_save if success } + end + + private + + def save + might_insert = ref.new_record? + + begin + retry_optimistic_lock(ref) do + next false if ref.persisted? && + (ref.last_updated_by_pipeline_id || 0) >= pipeline.id + + ref.update(status: next_status(ref.status, pipeline.status), + last_updated_by_pipeline: pipeline) + end + rescue ActiveRecord::RecordNotUnique + if might_insert + @ref = pipeline.reset.ref_status + might_insert = false + retry + else + raise + end + end + end + + def next_status(ref_status, pipeline_status) + if ref_status == 'failed' && pipeline_status == 'success' + 'fixed' + else + pipeline_status + end + end + + def after_save + enqueue_pipeline_notification + end + + def enqueue_pipeline_notification + PipelineNotificationWorker.perform_async(pipeline.id, ref_status: ref.status) + end + + def ref + @ref ||= pipeline.ref_status || build_ref + end + + def build_ref + Ci::Ref.new(ref: pipeline.ref, project: pipeline.project, tag: pipeline.tag) + end + end +end |