Class: Gitlab::Checks::MatchingMergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/checks/matching_merge_request.rb

Constant Summary collapse

TOTAL_METRIC =
:gitlab_merge_request_match_total
STALE_METRIC =
:gitlab_merge_request_match_stale_secondary

Instance Method Summary collapse

Constructor Details

#initialize(newrev, branch_name, project) ⇒ MatchingMergeRequest

Returns a new instance of MatchingMergeRequest.


9
10
11
12
13
# File 'lib/gitlab/checks/matching_merge_request.rb', line 9

def initialize(newrev, branch_name, project)
  @newrev = newrev
  @branch_name = branch_name
  @project = project
end

Instance Method Details

#match?Boolean

Returns:

  • (Boolean)

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/checks/matching_merge_request.rb', line 15

def match?
  # When a user merges a merge request, the following sequence happens:
  #
  # 1. Sidekiq: MergeService runs and updates the merge request in a locked state.
  # 2. Gitaly: The UserMergeBranch RPC runs.
  # 3. Gitaly (gitaly-ruby): This RPC calls the pre-receive hook.
  # 4. Rails: This hook makes an API request to /api/v4/internal/allowed.
  # 5. Rails: This API check does a SQL query for locked merge
  #    requests with a matching SHA.
  #
  # Since steps 1 and 5 will happen on different database
  # sessions, replication lag could erroneously cause step 5 to
  # report no matching merge requests. To avoid this, we check
  # the write location to ensure the replica can make this query.
  track_session_metrics do
    ::ApplicationRecord.sticking.select_valid_host(:project, @project.id)
  end

  # rubocop: disable CodeReuse/ActiveRecord
  @project.merge_requests
    .with_state(:locked)
    .where(in_progress_merge_commit_sha: @newrev, target_branch: @branch_name)
    .exists?
  # rubocop: enable CodeReuse/ActiveRecord
end