Simplify path code owners computation

Global code owners (aka folder code owners) should be ignored if there
is any matching per-file code owner set (in the code owner config that
is being resolved or in any imported code owner config) that ignores
global and parent code owners (i.e. that has "set noparent" set).

To achieve this we did 2 things:
1. For the code owner config that is being evaluated we skipped global
   imports that only import global code owners (importMode =
   GLOBAL_CODE_OWNER_SETS_ONLY) if the code owner config contained any
   matching per-file code owner set that ignores global and parent code
   owners (as those global code owners would be dropped by 2.).
2. We droped global code owners sets that we had already collected when
   we found any matching per-file code owner set that ignores global and
   parent code owners.

Since we use a builder to collect the result it's not straight-forward
to drop global code owner sets that have already been added. For this we
need to build the PathCodeOwnerResult and then create a new builder that
copies all the fields except global code owner sets. This adds quite
some boilerplate code.

While we have good test coverage for PathCodeOwners I cannot shake off
the feeling that there is also some edge case where dropping global code
owners is wrongly dropping too much, but I can’t pinpoint this.

In any case the existing logic is confusing and hard to read, which is
why we want to simplify this.

With this change the logic is easier to grasp now:
1. Differentiate between global and per-file code owner sets in
   PathCodeOwnersResult.
2. Collect all relevant global code owner sets in PathCodeOwnersResult
   regardless of whether they are possibly ignored.
3. Have an ignoreGlobalCodeOwners flag in PathCodeOwnersResult that is
   set when there is any matching per-file code owner set that ignores
   global and parent code owners.
4. When looking at code owner sets in PathCodeOwnerResult we simply
   ignore global code owner set when ignoreGlobalCodeOwners has been set
   to true.

This way we no longer need to recreate the PathCodeOwnersResult.Builder
to drop already collected global code owner sets and the code gets
easier to read.

In addition, since the ignored global code owners are now present in
PathCodeOwnerResult we may inform about them when code ownership is
being checked via the CheckCodeOwner REST endpoint (possible follow-up
change).

Bug: Google b/345161989
Change-Id: I6f7723c0ad49a216c5118ffd047216a9648c438a
Signed-off-by: Edwin Kempin <ekempin@google.com>
3 files changed
tree: f73e1eacf59afdf0b3336eb1b0b36236f46a8e28
  1. java/
  2. javatests/
  3. proto/
  4. resources/
  5. web/
  6. .gitignore
  7. .gitreview
  8. BUILD
  9. LICENSE
  10. README.md
README.md

Gerrit Code Review code-owners plugin

This plugin provides support for defining code owners for files in a repository.

If the code-owners plugin is enabled, changes can only be submitted if all touched files are covered by approvals from code owners.

For a detailed description of the plugin functionality please refer to the plugin documentation.

IMPORTANT: Before installing/enabling the plugin follow the instructions from the setup guide.

NOTE: The plugin documentation only renders correctly when the plugin is installed in Gerrit and the documentation is accessed via https:///plugins/code-owners/Documentation/index.html. If you want to read the documentation before installing the plugin, you can find it properly rendered here.

JavaScript Plugin

From the root of the gerrit repository.

bazel test //plugins/code-owners/web:karma_test

For testing the plugin with the Gerrit FE Dev Helper the command below builds

    bazel build //plugins/code-owners/web:code-owners
    ln -s bazel-bin/plugins/code-owners/web/code-owners.js polygerrit-ui/app/plugins/

and let the Dev Helper redirect from .+/plugins/code-owners/static/code-owners.js to http://localhost:8081/plugins/code-owners.js.