= Gerrit Code Review - Prolog Submit Rules Cookbook [[SubmitRule]] == Submit Rule A _Submit Rule_ in Gerrit is logic that defines when a change is submittable. By default, a change is submittable when it gets at least one highest vote in each voting category and has no lowest vote (aka veto vote) in any category. Typically, this means that a change needs `Code-Review+2`, `Verified+1` and has neither `Code-Review-2` nor `Verified-1` to become submittable. While this rule is a good default, there are projects which need more flexibility for defining when a change is submittable. In Gerrit, it is possible to use Prolog based rules to provide project specific submit rules and replace the default submit rules. Using Prolog based rules, project owners can define a set of criteria which must be fulfilled for a change to become submittable. For a change that is not submittable, the set of needed criteria is displayed in the Gerrit UI. [NOTE] Loading and executing Prolog submit rules may be disabled by setting `rules.enable=false` in the Gerrit config file (see link:config-gerrit.html#_a_id_rules_a_section_rules[rules section]) link:https://groups.google.com/d/topic/repo-discuss/wJxTGhlHZMM/discussion[This discussion thread] explains why Prolog was chosen for the purpose of writing project specific submit rules. link:http://gerrit-documentation.googlecode.com/svn/ReleaseNotes/ReleaseNotes-2.2.2.html[Gerrit 2.2.2 ReleaseNotes] introduces Prolog support in Gerrit. [[SubmitType]] == Submit Type A _Submit Type_ is a strategy that is used on submit to integrate the change into the destination branch. Supported submit types are: * `Fast Forward Only` * `Merge If Necessary` * `Merge Always` * `Cherry Pick` * `Rebase If Necessary` _Submit Type_ is a project global setting. This means that the same submit type is used for all changes of one project. Projects which need more flexibility in choosing, or enforcing, a submit type can use Prolog based submit type which replaces the project's default submit type. Prolog based submit type computes a submit type for each change. The computed submit type is shown on the change screen for each change. When submitting changes in a batch using "Submit including ancestors" or "Submit whole topic", submit type rules may not be used to mix submit types on a single branch, and trying to submit such a batch will fail. This avoids potentially confusing behavior and spurious submit failures. It is recommended to only use submit type rules to change submit types for an entire branch, which avoids this situation. == Prolog Language This document is not a complete Prolog tutorial. link:http://en.wikipedia.org/wiki/Prolog[This Wikipedia page on Prolog] is a good starting point for learning the Prolog language. This document will only explain some elements of Prolog that are necessary to understand the provided examples. == Prolog in Gerrit Gerrit uses its own link:https://gerrit.googlesource.com/prolog-cafe/[fork] of the original link:http://kaminari.istc.kobe-u.ac.jp/PrologCafe/[prolog-cafe] project. Gerrit embeds the prolog-cafe library and can interpret Prolog programs at runtime. == Interactive Prolog Cafe Shell For interactive testing and playing with Prolog, Gerrit provides the link:pgm-prolog-shell.html[prolog-shell] program which opens an interactive Prolog interpreter shell. [NOTE] The interactive shell is just a prolog shell, it does not load a gerrit server environment and thus is not intended for xref:TestingSubmitRules[testing submit rules]. == SWI-Prolog Instead of using the link:pgm-prolog-shell.html[prolog-shell] program one can also use the link:http://www.swi-prolog.org/[SWI-Prolog] environment. It provides a better shell interface and a graphical source-level debugger. [[RulesFile]] == The rules.pl file This section explains how to create and edit project specific submit rules. How to actually write the submit rules is explained in the next section. Project specific submit rules are stored in the `rules.pl` file in the `refs/meta/config` branch of that project. Therefore, we need to fetch and checkout the `refs/meta/config` branch in order to create or edit the `rules.pl` file: ---- $ git fetch origin refs/meta/config:config $ git checkout config ... edit or create the rules.pl file $ git add rules.pl $ git commit -m "My submit rules" $ git push origin HEAD:refs/meta/config ---- [[HowToWriteSubmitRules]] == How to write submit rules Whenever Gerrit needs to evaluate submit rules for a change `C` from project `P` it will first initialize the embedded Prolog interpreter by: * consulting a set of facts about the change `C` * consulting the `rules.pl` from the project `P` Conceptually we can imagine that Gerrit adds a set of facts about the change `C` on top of the `rules.pl` file and then consults it. The set of facts about the change `C` will look like: ---- :- package gerrit. <1> commit_author(user(1000000), 'John Doe', 'john.doe@example.com'). <2> commit_committer(user(1000000), 'John Doe', 'john.doe@example.com'). <3> commit_message('Add plugin support to Gerrit'). <4> ... ---- <1> Gerrit will provide its facts in a package named `gerrit`. This means we have to use qualified names when writing our code and referencing these facts. For example: `gerrit:commit_author(ID, N, M)` <2> user ID, full name and email address of the commit author <3> user ID, full name and email address of the commit committer <4> commit message A complete set of facts which Gerrit provides about the change is listed in the link:prolog-change-facts.html[Prolog Facts for Gerrit Change]. By default, Gerrit will search for a `submit_rule/1` predicate in the `rules.pl` file, evaluate the `submit_rule(X)` and then inspect the value of `X` in order to decide whether the change is submittable or not and also to find the set of needed criteria for the change to become submittable. This means that Gerrit has an expectation on the format and value of the result of the `submit_rule` predicate which is expected to be a `submit` term of the following format: ---- submit(label(label-name, status) [, label(label-name, status)]*) ---- where `label-name` is usually `'Code-Review'` or `'Verified'` but could also be any other string (see examples below). The `status` is one of: * `ok(user(ID))`. This status is used to tell that this label/category has been met. * `need(_)` is used to tell that this label/category is needed for the change to become submittable. * `reject(user(ID))`. This status is used to tell that this label/category is blocking submission of the change. * `impossible(_)` is used when the logic knows that the change cannot be submitted as-is. This is meant for cases where the logic requires members of a specific group to apply a specific label on a change, but no users are in that group. This is usually caused by misconfiguration of permissions. * `may(_)` allows expression of approval categories that are optional, i.e. could either be set or unset without ever influencing whether the change could be submitted. [NOTE] For a change to be submittable all `label` terms contained in the returned `submit` term must have either `ok` or `may` status. [IMPORTANT] Gerrit will let the Prolog engine continue searching for solutions of the `submit_rule(X)` query until it finds the first one where all labels in the return result have either status `ok` or `may` or there are no more solutions. If a solution where all labels have status `ok` is found then all previously found solutions are ignored. Otherwise, all labels names with status `need` from all solutions will be displayed in the UI indicating the set of conditions needed for the change to become submittable. Here some examples of possible return values from the `submit_rule` predicate: ---- submit(label('Code-Review', ok(user(ID)))) <1> submit(label('Code-Review', ok(user(ID))), label('Verified', reject(user(ID)))) <2> submit(label('Author-is-John-Doe', need(_)) <3> ---- <1> label `'Code-Review'` is met. As there are no other labels in the return result, the change is submittable. <2> label `'Verified'` is rejected. Change is not submittable. <3> label `'Author-is-John-Doe'` is needed for the change to become submittable. Note that this tells nothing about how this criteria will be met. It is up to the implementer of the `submit_rule` to return `label('Author-is-John-Doe', ok(user(ID)))` when this criteria is met. Most likely, it will have to match against `gerrit:commit_author` in order to check if this criteria is met. This will become clear through the examples below. Of course, when implementing the `submit_rule` we will use the facts about the change that are already provided by Gerrit. Another aspect of the return result from the `submit_rule` predicate is that Gerrit uses it to decide which set of labels to display on the change review screen for voting. If the return result contains label `'ABC'` and if the label `'ABC'` is link:config-labels.html[defined for the project] then voting for the label `'ABC'` will be displayed. Otherwise, it is not displayed. Note that the project doesn't need a defined label for each label contained in the result of `submit_rule` predicate. For example, the decision whether `'Author-is-John-Doe'` label is met will probably not be made by explicit voting but, instead, by inspecting the facts about the change. [[SubmitFilter]] == Submit Filter Another mechanism of changing the default submit rules is to implement the `submit_filter/2` predicate. While Gerrit will search for the `submit_rule` only in the `rules.pl` file of the current project, the `submit_filter` will be searched for in the `rules.pl` of all parent projects of the current project, but not in the `rules.pl` of the current project. The search will start from the immediate parent of the current project, then in the parent project of that project and so on until, and including, the `'All-Projects'` project. The purpose of the submit filter is, as its name says, to filter the results of the `submit_rule`. Therefore, the `submit_filter` predicate has two parameters: ---- submit_filter(In, Out) :- ... ---- Gerrit will invoke `submit_filter` with the `In` parameter containing a `submit` structure produced by the `submit_rule` and will take the value of the `Out` parameter as the result. The `Out` value of a `submit_filter` will become the `In` value for the next `submit_filter` in the parent line. The value of the `Out` parameter of the top-most `submit_filter` is the final result of the submit rule that is used to decide whether a change is submittable or not. [IMPORTANT] `submit_filter` is a mechanism for Gerrit administrators to implement and enforce submit rules that would apply to all projects while `submit_rule` is a mechanism for project owners to implement project specific submit rules. However, project owners who own several projects could also make use of `submit_filter` by using a common parent project for all their projects and implementing the `submit_filter` in this common parent project. This way they can avoid implementing the same `submit_rule` in all their projects. The following "drawing" illustrates the order of the invocation and the chaining of the results of the `submit_rule` and `submit_filter` predicates. ---- All-Projects ^ submit_filter(B, S) :- ... <4> | Parent-3 ^ | Parent-2 ^ submit_filter(A, B) :- ... <3> | Parent-1 ^ submit_filter(X, A) :- ... <2> | MyProject submit_rule(X) :- ... <1> ---- <1> The `submit_rule` of `MyProject` is invoked first. <2> The result `X` is filtered through the `submit_filter` from the `Parent-1` project. <3> The result of `submit_filter` from `Parent-1` project is filtered by the `submit_filter` in the `Parent-2` project. Since `Parent-3` project doesn't have a `submit_filter` it is skipped. <4> The result of `submit_filter` from `Parent-2` project is filtered by the `submit_filter` in the `All-Projects` project. The value in `S` is the final value of the submit rule evaluation. [NOTE] If `MyProject` doesn't define its own `submit_rule` Gerrit will invoke the default implementation of submit rule that is named `gerrit:default_submit` and its result will be filtered as described above. [[HowToWriteSubmitType]] == How to write submit type Writing custom submit type logic in Prolog is similar to xref:HowToWriteSubmitRules[writing submit rules]. The only difference is that one has to implement a `submit_type` predicate (instead of the `submit_rule`) and that the return result of the `submit_type` has to be an atom that represents one of the supported submit types: * `fast_forward_only` * `merge_if_necessary` * `merge_always` * `cherry_pick` * `rebase_if_necessary` == Submit Type Filter Submit type filter works the same way as the xref:SubmitFilter[Submit Filter] where the name of the filter predicate is `submit_type_filter`. ---- submit_type_filter(In, Out). ---- Gerrit will invoke `submit_type_filter` with the `In` parameter containing a result of the `submit_type` and will take the value of the `Out` parameter as the result. [[TestingSubmitRules]] == Testing submit rules The prolog environment running the `submit_rule` is loaded with state describing the change that is being evaluated. The easiest way to load this state is to test your `submit_rule` against a real change on a running gerrit instance. The command link:cmd-test-submit-rule.html[test-submit rule] loads a specific change and executes the `submit_rule`. It optionally reads the rule from from `stdin` to facilitate easy testing. ---- $ cat rules.pl | ssh gerrit_srv gerrit test-submit rule I45e080b105a50a625cc8e1fb5b357c0bfabe6d68 -s ---- == Prolog vs Gerrit plugin for project specific submit rules Since version 2.5 Gerrit supports plugins and extension points. A plugin or an extension point could also be used as another means to provide custom submit rules. One could ask for a guideline when to use Prolog based submit rules and when to go for writing a new plugin. Writing a Prolog program is usually much faster than writing a Gerrit plugin. Prolog based submit rules can be pushed to a project by project owners while Gerrit plugins could only be installed by Gerrit administrators. In addition, Prolog based submit rules can be pushed for review by pushing to `refs/for/refs/meta/config` branch. On the other hand, Prolog based submit rules get a limited amount of facts about the change exposed to them. Gerrit plugins get full access to Gerrit internals and can potentially check more things than Prolog based rules. From version 2.6 Gerrit plugins can contribute Prolog predicates. This way, we can make use of the plugin provided predicates when writing Prolog based rules. == Examples - Submit Rule The following examples should serve as a cookbook for developing own submit rules. Some of them are too trivial to be used in production and their only purpose is to provide step by step introduction and understanding. Some of the examples will implement the `submit_rule` and some will implement the `submit_filter` just to show both possibilities. Remember that `submit_rule` is only invoked from the current project and `submit_filter` is invoked from all parent projects. This is the most important fact in deciding whether to implement `submit_rule` or `submit_filter`. === Example 1: Make every change submittable Let's start with a most trivial example where we would make every change submittable regardless of the votes it has: `rules.pl` [source,prolog] ---- submit_rule(submit(W)) :- W = label('Any-Label-Name', ok(user(1000000))). ---- In this case we make no use of facts about the change. We don't need it as we are simply making every change submittable. Note that, in this case, the Gerrit UI will not show the UI for voting for the standard `'Code-Review'` and `'Verified'` categories as labels with these names are not part of the return result. The `'Any-Label-Name'` could really be any string. The `user(1000000)` represents the user whose account ID is `1000000`. [NOTE] Instead of the account ID `1000000` we could have used any other account ID. The following examples will use `user(ID)` instead of `user(1000000)` because it is easier to read and doesn't suggest that there is anything special with the account ID `1000000`. === Example 2: Every change submittable and voting in the standard categories possible This is continuation of the previous example where, in addition, to making every change submittable we want to enable voting in the standard `'Code-Review'` and `'Verified'` categories. `rules.pl` [source,prolog] ---- submit_rule(submit(CR, V)) :- CR = label('Code-Review', ok(user(ID))), V = label('Verified', ok(user(ID))). ---- Since for every change all label statuses are `'ok'` every change will be submittable. Voting in the standard labels will be shown in the UI as the standard label names are included in the return result. === Example 3: Nothing is submittable This example shows how to make all changes non-submittable regardless of the votes they have. `rules.pl` [source,prolog] ---- submit_rule(submit(R)) :- R = label('Any-Label-Name', reject(user(ID))). ---- Since for any change we return only one label with status `reject`, no change will be submittable. The UI will, however, not indicate what is needed for a change to become submittable as we return no labels with status `need`. === Example 4: Nothing is submittable but UI shows several 'Need ...' criteria In this example no change is submittable but here we show how to present 'Need