Return OWNERS files that were read for computing code owners of a file

With this change we extend the response of the List Code Owners REST
endpoints to return the OWNERS files which have been taken into account
for computing the code owners of the file.

Inspecting this list of OWNERS files is useful when the suggested code
owners do not match the expectation of the user as it allows the user to
see from which OWNERS files the code owners were loaded. E.g. if an
expected code owner is missing they can easily determine to which OWNERS
file this user should be added as a code owner.

Another case where this can help to avoid user confusion is when a
project contains different sets of OWNERS files (e.g. 'OWNERS' files and
'OWNERS.android' files) as it shows to the user which set of OWNERS
files is being used (depends on the code owners project configuration,
see codeOwners.fileExtension setting).

When computing the code owners for a file, the List Code Owners REST
endpoints iterate over the relevant OWNERS files, starting at the
directory of the file for which code owners are requested, e.g. if code
owners are requested for file "/foo/bar/baz/file.txt" the inspected
OWNERS files, if they exist, are '/foo/bar/baz/OWNERS',
'/foo/bar/OWNERS', '/foo/OWNERS', '/OWNERS' and finally '/OWNERS' in
refs/meta/config (this file defines the default code owners). These
files are collected as we iterate over them and are returned in the
order in which they are evaluated.

In addition, OWNERS files may import other OWNERS files. Imported OWNERS
files are returned as part of the JSON that represents the importing
OWNERS file. The imported OWNERS files can be from another project or
branch and can import further OWNERS files themselves. When importing
OWNERS files there are 2 import modes: ALL -> import all code owners
(global code owners + per-file code owners), GLOBAL_CODE_OWNER_SETS_ONLY
-> import only global code owners (but no per-file code owners). Which
import mode is being used depends on the keyword that is used to import
an OWNERS file ('include' keyword = ALL, 'file' keyword =
GLOBAL_CODE_OWNER_SETS_ONLY). To be able to differentiate between the 2
import modes the JSON for the imported OWNERS file contains a field for
the import mode.

It's possible that OWNERS files import other OWNERS files which cannot
be resolved (e.g. the imported OWNERS file no longer exists). In this
case the import is ignored. To make this transparent to users, we also
return unresolved imports as part of the JSON that is returned for the
importing OWNERS file. The JSON that represents the unresolved import
contains an error message in this case that explains why this import
couldn't be resolved.

Resolving imports is implemented in PathCodeOwners. PathCodeOwners
already returned unresolved imports and we extended PathCodeOwners to
return resolved imports as well (see new field in PathCodeOwnersResult).
Unresolved and resolved imports are represented by
ImportedCodeOwnerConfig (previously named UnresolvedImport) which
consists of the key of the importing OWNERS file, the key of the
imported OWNERS file, the code owner config reference that contains
information about the import mode and an optional error message that is
set if the imported code owner config couldn't be resolved.

CodeOwnerResolver is a wrapper around PathCodeOwners and has been
extended to provide the resolved and unresolved imports that were found
by PathCodeOwners to its caller, the List Code Owners REST endpoints, so
that they can include this information into the JSON that is returned to
the client.

The OWNERS files that were taken into account for computing the code
owners are returned as CodeOwnerConfigFileInfo entities that are
constructed by CodeOwnerConfigFileJson.

Bug: Google b/271940778
Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I951772554489095af074777792697c0e539ef453
24 files changed
tree: ad566277f773c536ab76f030d7796cf491ddea2d
  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.