Preparing your design workspace

Loading the problem and your latest saved attempt…

Design Required-Approver Resolution for Pull Requests

Work out, for every pull request, which owners must approve it before it can merge, keep that answer fast across repositories of very different sizes and a high commit rate, and never let a merge through without the approvals it needs.

Problem statement

Design the mechanism that decides who must approve a pull request before it can be merged. For this exercise, assume it is part of a code-hosting platform and the merge button depends on it.

For this exercise, assume ownership is declared in rule files stored in the repository itself. A rule file may sit in any directory.

Each rule in it has a path pattern relative to that directory (ordinary glob syntax, where ** matches any number of directories), a list of owners, each a user or a team, and an optional minimum number of approvals that defaults to 1.

For one changed file, the nearest rule file above the file that has a matching rule decides, and inside one rule file the last matching rule wins. A file that no rule matches falls back to the repository's default owners.

A group's requirement is met when enough distinct people approve who are either listed or members of a listed team; the pull request's author never counts toward their own change.

For this exercise, also assume: the rules always come from the target branch, never from the pull request's own changes, so a change to a rule file needs the approval of whoever owns that rule file today; an approval applies to the commit it was given on, so a new push makes earlier approvals stale; and team membership lives in the company directory and can change at any time.

For this exercise, assume the platform hosts many repositories of very different sizes, from tiny services to one very large monorepo, and code is committed at a high rate.

Your design should show the required approvers soon after every push, keep up as repositories and commit rates grow, and act as the authority that the merge button relies on. Review comments, notifications, and choosing which particular person in a group to ask are out of scope for this exercise.

What to build

  1. After a pull request is opened or updated, work out which owner groups must approve it and how many approvals each needs, from the target branch's rules, and show each group with the files it covers.
  2. Record approvals and show, for the pull request's current commit, which requirements are met and which are still missing.
  3. Allow a merge only when every required group has enough approvals for the exact commit being merged, under the rules of the branch it merges into and the team membership at merge time.
  4. Keep each pull request's requirements correct when its commits, the target branch's rule files, or team membership change.

At scale

  • For this exercise, assume about 5,000 repositories. Most have fewer than 10,000 files and a handful of rule files; the largest is a monorepo with about 5 million files and about 20,000 rule files, and its directories go up to about 15 levels deep.
  • For this exercise, assume peaks of about 50 pushes a second to open pull requests across all repositories, and that the monorepo's main branch receives about one commit every 10 seconds at peak, about 2 percent of which change a rule file.
  • For this exercise, assume most pull requests touch fewer than 20 files, but large refactors can touch up to 50,000 files spread over about 5,000 directories.
  • For this exercise, assume the required approvers should appear within 5 seconds of a push at p99 for pull requests of up to 1,000 files and within 60 seconds for the largest, a merge check should answer within 1 second at p99, and a team membership change must be honored by merge checks within one minute.