2019-05-21 17:14:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class MergeabilityCheckService < ::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-07-23 20:51:23 -04:00
|
|
|
include Gitlab::ExclusiveLeaseHelpers
|
2019-05-21 17:14:22 -04:00
|
|
|
|
2019-06-21 10:30:09 -04:00
|
|
|
delegate :project, to: :@merge_request
|
2019-05-21 17:14:22 -04:00
|
|
|
delegate :repository, to: :project
|
|
|
|
|
|
|
|
def initialize(merge_request)
|
|
|
|
@merge_request = merge_request
|
|
|
|
end
|
|
|
|
|
2020-01-21 10:08:26 -05:00
|
|
|
def async_execute
|
|
|
|
return service_error if service_error
|
|
|
|
return unless merge_request.mark_as_checking
|
|
|
|
|
|
|
|
MergeRequestMergeabilityCheckWorker.perform_async(merge_request.id)
|
|
|
|
end
|
|
|
|
|
2019-05-21 17:14:22 -04:00
|
|
|
# Updates the MR merge_status. Whenever it switches to a can_be_merged state,
|
|
|
|
# the merge-ref is refreshed.
|
|
|
|
#
|
|
|
|
# recheck - When given, it'll enforce a merge-ref refresh if the current merge_status is
|
2019-06-20 13:38:28 -04:00
|
|
|
# can_be_merged or cannot_be_merged and merge-ref is outdated.
|
2019-05-21 17:14:22 -04:00
|
|
|
# Given MergeRequests::RefreshService is called async, it might happen that the target
|
|
|
|
# branch gets updated, but the MergeRequest#merge_status lags behind. So in scenarios
|
|
|
|
# where we need the current state of the merge ref in repository, the `recheck`
|
|
|
|
# argument is required.
|
|
|
|
#
|
2019-07-23 20:51:23 -04:00
|
|
|
# retry_lease - Concurrent calls wait for at least 10 seconds until the
|
|
|
|
# lease is granted (other process finishes running). Returns an error
|
|
|
|
# ServiceResponse if the lease is not granted during this time.
|
|
|
|
#
|
2019-05-21 17:14:22 -04:00
|
|
|
# Returns a ServiceResponse indicating merge_status is/became can_be_merged
|
|
|
|
# and the merge-ref is synced. Success in case of being/becoming mergeable,
|
|
|
|
# error otherwise.
|
2019-07-23 20:51:23 -04:00
|
|
|
def execute(recheck: false, retry_lease: true)
|
2020-01-21 10:08:26 -05:00
|
|
|
return service_error if service_error
|
2019-07-23 20:51:23 -04:00
|
|
|
return check_mergeability(recheck) unless merge_ref_auto_sync_lock_enabled?
|
|
|
|
|
|
|
|
in_write_lock(retry_lease: retry_lease) do |retried|
|
|
|
|
# When multiple calls are waiting for the same lock (retry_lease),
|
|
|
|
# it's possible that when granted, the MR status was already updated for
|
|
|
|
# that object, therefore we reset if there was a lease retry.
|
|
|
|
merge_request.reset if retried
|
|
|
|
|
|
|
|
check_mergeability(recheck)
|
|
|
|
end
|
|
|
|
rescue FailedToObtainLockError => error
|
|
|
|
ServiceResponse.error(message: error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :merge_request
|
2019-05-21 17:14:22 -04:00
|
|
|
|
2019-07-23 20:51:23 -04:00
|
|
|
def check_mergeability(recheck)
|
2019-05-21 17:14:22 -04:00
|
|
|
recheck! if recheck
|
|
|
|
update_merge_status
|
|
|
|
|
|
|
|
unless merge_request.can_be_merged?
|
|
|
|
return ServiceResponse.error(message: 'Merge request is not mergeable')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless merge_ref_auto_sync_enabled?
|
|
|
|
return ServiceResponse.error(message: 'Merge ref is outdated due to disabled feature')
|
|
|
|
end
|
|
|
|
|
2019-06-21 10:30:09 -04:00
|
|
|
unless payload.fetch(:merge_ref_head)
|
|
|
|
return ServiceResponse.error(message: 'Merge ref cannot be updated')
|
|
|
|
end
|
|
|
|
|
2019-05-21 17:14:22 -04:00
|
|
|
ServiceResponse.success(payload: payload)
|
|
|
|
end
|
|
|
|
|
2019-07-23 20:51:23 -04:00
|
|
|
# It's possible for this service to send concurrent requests to Gitaly in order
|
|
|
|
# to "git update-ref" the same ref. Therefore we handle a light exclusive
|
|
|
|
# lease here.
|
|
|
|
#
|
|
|
|
def in_write_lock(retry_lease:, &block)
|
|
|
|
lease_key = "mergeability_check:#{merge_request.id}"
|
2019-05-21 17:14:22 -04:00
|
|
|
|
2019-07-23 20:51:23 -04:00
|
|
|
lease_opts = {
|
|
|
|
ttl: 1.minute,
|
|
|
|
retries: retry_lease ? 10 : 0,
|
|
|
|
sleep_sec: retry_lease ? 1.second : 0
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:09:35 -04:00
|
|
|
in_lock(lease_key, **lease_opts, &block)
|
2019-07-23 20:51:23 -04:00
|
|
|
end
|
2019-05-21 17:14:22 -04:00
|
|
|
|
|
|
|
def payload
|
|
|
|
strong_memoize(:payload) do
|
|
|
|
{
|
|
|
|
merge_ref_head: merge_ref_head_payload
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_ref_head_payload
|
|
|
|
commit = merge_request.merge_ref_head
|
|
|
|
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
target_id, source_id = commit.parent_ids
|
|
|
|
|
|
|
|
{
|
|
|
|
commit_id: commit.id,
|
|
|
|
source_id: source_id,
|
|
|
|
target_id: target_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_merge_status
|
|
|
|
return unless merge_request.recheck_merge_status?
|
2020-10-16 08:09:33 -04:00
|
|
|
return merge_request.mark_as_unmergeable if merge_request.broken?
|
2019-05-21 17:14:22 -04:00
|
|
|
|
2020-10-16 08:09:33 -04:00
|
|
|
merge_to_ref_success = merge_to_ref
|
|
|
|
|
|
|
|
update_diff_discussion_positions! if merge_to_ref_success
|
|
|
|
|
|
|
|
if merge_to_ref_success && can_git_merge?
|
2019-05-21 17:14:22 -04:00
|
|
|
merge_request.mark_as_mergeable
|
|
|
|
else
|
|
|
|
merge_request.mark_as_unmergeable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-15 08:09:18 -04:00
|
|
|
def update_diff_discussion_positions!
|
|
|
|
Discussions::CaptureDiffNotePositionsService.new(merge_request).execute
|
|
|
|
end
|
|
|
|
|
2019-05-21 17:14:22 -04:00
|
|
|
def recheck!
|
2019-06-20 13:38:28 -04:00
|
|
|
if !merge_request.recheck_merge_status? && outdated_merge_ref?
|
|
|
|
merge_request.mark_as_unchecked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks if the existing merge-ref is synced with the target branch.
|
|
|
|
#
|
|
|
|
# Returns true if the merge-ref does not exists or is out of sync.
|
|
|
|
def outdated_merge_ref?
|
|
|
|
return false unless merge_ref_auto_sync_enabled?
|
2019-06-21 10:30:09 -04:00
|
|
|
return false unless merge_request.open?
|
2019-06-20 13:38:28 -04:00
|
|
|
|
|
|
|
return true unless ref_head = merge_request.merge_ref_head
|
|
|
|
return true unless target_sha = merge_request.target_branch_sha
|
|
|
|
return true unless source_sha = merge_request.source_branch_sha
|
|
|
|
|
|
|
|
ref_head.parent_ids != [target_sha, source_sha]
|
2019-05-21 17:14:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_git_merge?
|
2020-10-16 08:09:33 -04:00
|
|
|
repository.can_be_merged?(merge_request.diff_head_sha, merge_request.target_branch)
|
2019-05-21 17:14:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_to_ref
|
|
|
|
return true unless merge_ref_auto_sync_enabled?
|
|
|
|
|
2020-10-16 08:09:33 -04:00
|
|
|
params = { allow_conflicts: Feature.enabled?(:display_merge_conflicts_in_diff, project) }
|
|
|
|
result = MergeRequests::MergeToRefService.new(project, merge_request.author, params).execute(merge_request)
|
2019-05-21 17:14:22 -04:00
|
|
|
result[:status] == :success
|
|
|
|
end
|
2019-06-21 10:30:09 -04:00
|
|
|
|
|
|
|
def merge_ref_auto_sync_enabled?
|
|
|
|
Feature.enabled?(:merge_ref_auto_sync, project, default_enabled: true)
|
|
|
|
end
|
2019-07-23 20:51:23 -04:00
|
|
|
|
|
|
|
def merge_ref_auto_sync_lock_enabled?
|
|
|
|
Feature.enabled?(:merge_ref_auto_sync_lock, project, default_enabled: true)
|
|
|
|
end
|
2020-01-21 10:08:26 -05:00
|
|
|
|
|
|
|
def service_error
|
|
|
|
strong_memoize(:service_error) do
|
|
|
|
if !merge_request
|
|
|
|
ServiceResponse.error(message: 'Invalid argument')
|
|
|
|
elsif Gitlab::Database.read_only?
|
|
|
|
ServiceResponse.error(message: 'Unsupported operation')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-21 17:14:22 -04:00
|
|
|
end
|
|
|
|
end
|