owners-plugin based on files instead of matchers.

Modify the prolog predicates to be file based instead of Matcher based.

Matchers are the basis for collecting owners of a file.

The added predicates are:

gerrit_owners:file_owners(+File,-FormattedListOfOwners)
   gets a label (<file basename> <username1> <username2> ...
   describing for the file all its owners.

gerrit_owners:matcher_path(-File)
   lists all files having owners

gerrit_owners:matcher_owner(+File,-user(ID))
   gets in sequence all the owners of a file to allow prolog doing 
   inferences in rules.pl

Example of OWNERS files is

inherited: false
owners:
- a@mail.com
- admin
matches:
- suffix: .java
  owners:
  - b@mail.com
  - c@mail.com

Inherited flag has been tested to inherit additional user down in the
hierarchy or to specify defaults .suffix owners at the top
(which are normal cases).

Example of rules.pl is:

% default rule adding match_owner_approval if needed
submit_rule(S) :-
  gerrit:default_submit(L),
  L =.. [submit | Sr ],
  add_match_owner_approval(Sr,A),
  S =.. [submit | A ].

% add extra label for every file F
submit_rule(submit(W)) :-
    gerrit_owners:matcher_path(F),
    findall(US,code_review_user(US),Approvers),
    matcher_needed(Approvers,F,W).

% this loops over all the paths and if for any
% we have some labels generated then add a single
% Owner-Approval need to block submit button
add_match_owner_approval(In,Out) :-
    gerrit_owners:matcher_path(P),
    findall(US,code_review_user(US),Approvers),
    matcher_needed(Approvers,P,W),
    \+ W == [],
    Out = [label('Owner-Approval', need(_)) | In], !.

add_match_owner_approval(In,Out) :- Out = In.

% to get only matchers for that path
matcher_needed(Users,Path,W) :-
   findall(US,needed_review_user(Path,US),NSL),
   subtract(Users,NSL,Diff),
   % if Users - Needed is unchanged this means we are missing at
   % least one (!)
   Diff == Users,
   gerrit_owners:file_owners(Path,FormattedLabel),
   W = label(FormattedLabel, need(_)).

code_review_user(U) :-
    gerrit:commit_label(label('Code-Review', 2), user(U)).

needed_review_user(Path,User) :-
    gerrit_owners:matcher_owner(Path,user(User)).

%%%%%%%%%%%%%%%
% utility functions
%%%%%%%%%%%%%%%

%
% convert a Var back to a sequence of solutions to be feed
% in prolog (test purpose).
%
enumerate([H|T],F) :- F = H ; enumerate(T,F).

%
% check if first arg is a member of second list arg
%
memberchk(X,[X|_]) :- !.
memberchk(X,[_|T]):- memberchk(X,T).

%
% subtract second list from first list giving result
% used to understand if ApprovedList-NeededList  is changed
% in this case we understand that at least one owner of the matching
% gave the ok
%
subtract([], _, []) :- !.
subtract([A|C], B, D) :-
    memberchk(A, B), !,
    subtract(C, B, D).
subtract([A|B], C, [A|D]) :-
    subtract(B, C, D).

append([],L,L).
append([H|T],L2,[H|L3])  :-  append(T,L2,L3).

prepend([],L,L).
prepend([X|T],L,[X|Result]):- prepend(T,L,Result).
prepend(X,L,[X|L]).

Change-Id: I9688e52235e25556a0ed657c9cb2e46b3895afde
5 files changed
tree: 7e5973f361a3b29acd9fec96914d59b40e9c8bbf
  1. owners/
  2. owners-autoassign/
  3. owners-common/
  4. .gitignore
  5. CHANGELOG
  6. config.md
  7. external_plugin_deps.bzl
  8. LICENSE
  9. README.md
README.md

Gerrit OWNERS Plugin

This plugin provides some Prolog predicates that can be used to add customized validation checks based on the approval of ‘path owners’ of a particular folder in the project.

That allows creating a single big project including multiple components and users have different roles depending on the particular path where changes are being proposed. A user can be “owner” in a specific directory, and thus influencing the approvals of changes there, but cannot do the same in others paths, so assuring a kind of dynamic subproject access rights.

How it works

There are currently two main prolog public verbs:

add_owner_approval/3 (UserList, InList, OutList) appends label('Owner-Approval', need(_)) to InList building OutList if UserList has no users contained in the defined owners of this path change.

In other words, the predicate just copies InList to OutList if at least one of the elements in UserList is an owner.

add_owner_approval/2 (InList, OutList) appends label('Owner-Approval', need(_)) to InList building OutList if no owners has given a Code-Review +2 to this path change.

This predicate is similar to the first one but generates a UserList with an hardcoded policy.

Since add_owner_approval/3 is not using hard coded policies, it can be suitable for complex customizations.

Auto assigner

There is a second plugin, gerrit-owners-autoassign which depends on gerrit-owners. It will automatically assign all of the owners to review a change when it's created or updated.

How to build

Create three symbolic links of the owners-owners, owners-common and owners-autoassign from the Gerrit source code /plugins directory to the subdirectories of this project.

Overwrite the Gerrit /plugins/external_plugin_deps.bzl with the one contained in the owners-common directory.

Then build the owners and owners-autoassign plugins with the usual Gerrit plugin compile command.

Example:

   $ git clone https://gerrit.googlesource.com/plugins/owners
   $ git clone https://gerrit.googlesource.com/gerrit
   $ cd gerrit/plugins
   $ ln -s ../../owners/owners* .
   $ cp -f owners-common/external_plugin_deps.bzl .
   $ cd ..
   $ bazel build plugins/owners
   $ bazel build plugins/owners-autoassign

NOTE: the owners-common folder is producing shared artifacts for the two plugins and does not need to be built separately being a direct dependency of the build process. Its resulting .jar must not be installed in gerrit plugins directory.