Reorder code-owner gate to skip can_push_to_branch? on large pushes

What does this MR do and why?

On a push that touches many refs, EE::Gitlab::Checks::DiffCheck#validate_code_owners? runs once per ref and evaluated the expensive user_access.can_push_to_branch? before the cheap code-owner-approval gate:

return false if updated_from_web? || user_access.can_push_to_branch?(branch_name)
project.branch_requires_code_owner_approval?(branch_name)

can_push_to_branch? is request_cached by (method, ref), so each distinct ref is a miss and re-runs UserAccess#protected? (the per-ref protected-branch Redis cache) plus members / project_group_links authorization queries. On a 20,000-ref push that is ~80k Redis calls and ~40k SQL queries, and the whole check can exceed the 50s Gitlab::GitAccess::INTERNAL_TIMEOUT.

This reorders the guards cheapest-first:

return false if updated_from_web?
return false unless project.branch_requires_code_owner_approval?(branch_name)

!user_access.can_push_to_branch?(branch_name)
  • updated_from_web? is protocol == 'web', pure in-memory.
  • branch_requires_code_owner_approval? returns false immediately when code-owner approval is not available/configured, which is the common case.
  • can_push_to_branch? now runs only for branches that actually require code-owner approval, a bounded set.

The boolean result is identical (NOT web AND requires_code_owner AND NOT can_push) and all three checks are side-effect-free, so no feature flag is needed.

Measurement

Benchmark of ChangesAccess#validate!, non-protected branch creations, before this change (with the branch-cache fix from !267313 (merged) already applied):

N refs Redis SQL
100 403 (4.0/ref) 203 (2.0/ref)
500 1,706 (3.4/ref) 1,003 (2.0/ref)

Those residual per-ref Redis and SQL calls come from this can_push_to_branch? call. For projects without code-owner-approval branches they drop to ~0 after this change.

How to set up and validate locally

Push many refs to a project with no code-owner-approval-required branches; confirm validate_code_owners? no longer calls can_push_to_branch? per ref (covered by the added spec).

Merge request reports

Loading
Loading