From f8d6f7322ea43ff7844d006885db9f0c36ad7cc9 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Tue, 2 Jul 2019 09:01:36 +0700 Subject: Fix race condition on merge train ref generation Today, Pipelines for merge train run on `refs/merge`, however, this causes a race condition that it can be overwritten by CheckMergeabilityService. This patch fixes the problem by generating `refs/train` for those pipelines. --- app/models/merge_request.rb | 13 +++++++++++++ app/models/repository.rb | 4 ++++ 2 files changed, 17 insertions(+) (limited to 'app/models') diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index e96e26cc773..53977748c30 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -1127,6 +1127,19 @@ class MergeRequest < ApplicationRecord "refs/#{Repository::REF_MERGE_REQUEST}/#{iid}/merge" end + def train_ref_path + "refs/#{Repository::REF_MERGE_REQUEST}/#{iid}/train" + end + + def cleanup_refs(only: :all) + target_refs = [] + target_refs << ref_path if %i[all head].include?(only) + target_refs << merge_ref_path if %i[all merge].include?(only) + target_refs << train_ref_path if %i[all train].include?(only) + + project.repository.delete_refs(*target_refs) + end + def self.merge_request_ref?(ref) ref.start_with?("refs/#{Repository::REF_MERGE_REQUEST}/") end diff --git a/app/models/repository.rb b/app/models/repository.rb index a408db7ebbe..a25d5abfa64 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -845,6 +845,10 @@ class Repository raw.merge_to_ref(user, source_sha, branch, target_ref, message, first_parent_ref) end + def delete_refs(*ref_names) + raw.delete_refs(*ref_names) + end + def ff_merge(user, source, target_branch, merge_request: nil) their_commit_id = commit(source)&.id raise 'Invalid merge source' if their_commit_id.nil? -- cgit v1.2.1