Sync code owner rules for open merge requests only

What does this MR do and why?

Creating a protected branch synchronously syncs code owner approval rules for every open and closed merge request targeting that branch. Each sync reads CODEOWNERS from Gitaly and writes approval rule rows to the primary database, so the cost of a single protected branch creation grows with the number of merge requests that have ever targeted the branch, including ones that were closed long ago.

Behind the protected_branch_code_owner_sync_open_only feature flag, EE::ProtectedBranches::CreateService narrows the candidate set from open_and_closed to opened. Merged merge requests were already excluded.

Closed merge requests do not need this sync:

  • A closed merge request cannot be merged, so its code owner rules are never read for a mergeability decision while it stays closed.
  • Reopening one clears the merge ref and marks the merge request unchecked, so the next mergeability check runs MergeRequests::ReloadMergeHeadDiffService, which resyncs code owner approval rules.
Where the resync on reopen happens

MergeRequests::ReopenService#execute calls merge_request.mark_as_unchecked and merge_request.update_merge_ref_sha(nil). The next mergeability check therefore reloads the merge head diff, and EE::MergeRequests::ReloadMergeHeadDiffService#execute calls MergeRequests::SyncCodeOwnerApprovalRules on success for projects licensed for code owners.

This reduces the work done per protected branch creation; it does not bound it. A branch with many open merge requests still fans out proportionally.

Feature flag

Name protected_branch_code_owner_sync_open_only
Type beta, default disabled
Actor root namespace

With the flag disabled, behaviour is unchanged.

Queries

Exact branches

Before:

SELECT merge_requests.* FROM merge_requests WHERE merge_requests.target_project_id = 278964 AND merge_requests.state_id IN (1, 2) AND merge_requests.target_branch = 'master'

https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79886/commands/187838

After:

SELECT merge_requests.* FROM merge_requests WHERE merge_requests.target_project_id = 278964 AND merge_requests.state_id = 1 AND merge_requests.target_branch = 'master'

https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79886/commands/187897

Wildcard branches

Before:

SELECT merge_requests.* FROM merge_requests WHERE merge_requests.target_project_id = 278964 AND merge_requests.state_id IN (1, 2) AND (target_branch LIKE '%-stable-ee')

https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79886/commands/187894

After:

SELECT merge_requests.* FROM merge_requests WHERE merge_requests.target_project_id = 278964 AND merge_requests.state_id = 1 AND (target_branch LIKE '%-stable-ee')

https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/79886/commands/187895

References

How to set up and validate locally

The project needs a license with the code owners feature (Premium or higher).

  1. In a project, commit a CODEOWNERS file on the default branch, for example:

    * @root
  2. Create two merge requests targeting the default branch, each changing a file. Close one of them.

  3. In the Rails console, make sure the flag is off and the branch is unprotected:

    Feature.disable(:protected_branch_code_owner_sync_open_only)
    project = Project.find_by_full_path('<namespace>/<project>')
    project.protected_branches.find_by(name: project.default_branch)&.destroy
    project.merge_requests.each { |mr| mr.approval_rules.code_owner.delete_all }
  4. Protect the default branch under Settings > Repository > Protected branches, with Require approval from code owners enabled.

  5. Confirm both merge requests got code owner rules:

    project.merge_requests.map { |mr| [mr.iid, mr.state, mr.approval_rules.code_owner.count] }
  6. Repeat from step 3 with the flag enabled:

    Feature.enable(:protected_branch_code_owner_sync_open_only)
  7. Confirm only the open merge request got code owner rules. The closed one has a count of 0.

  8. Reopen the closed merge request and visit its page. Once the mergeability check runs, its code owner rules are back.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

No changelog entry: the change is behind a disabled feature flag and makes no user-facing behaviour change on its own.

Edited by Jerry Seto

Merge request reports

Loading
Loading