blob: d832b051facb8b9d5fffe9638ccb873e584df4d0 [file] [log] [blame]
Sasa Zivkovae50f712012-07-24 14:51:44 +02001Gerrit Code Review - Prolog Submit Rules Cookbook
2=================================================
3
4Submit Rule
5-----------
6A 'Submit Rule' in Gerrit is logic that defines when a change is submittable.
7By default, a change is submittable when it gets at least one
8highest vote in each voting category and has no lowest vote (aka veto vote) in
9any category. Typically, this means that a change needs 'Code-Review+2',
10'Verified+1' and has neither 'Code-Review-2' nor 'Verified-1' to become
11submittable.
12
13While this rule is a good default, there are projects which need more
14flexibility for defining when a change is submittable. In Gerrit, it is
15possible to use Prolog based rules to provide project specific submit rules and
16replace the default submit rules. Using Prolog based rules, project owners can
17define a set of criteria which must be fulfilled for a change to become
18submittable. For a change that is not submittable, the set of needed criteria
19is displayed in the Gerrit UI.
20
21NOTE: Loading and executing Prolog submit rules may be disabled by setting
22`rules.enabled=false` in the Gerrit config file (see
23link:config-gerrit.html#_a_id_rules_a_section_rules[rules section])
24
25link:https://groups.google.com/d/topic/repo-discuss/wJxTGhlHZMM/discussion[This
26discussion thread] explains why Prolog was chosen for the purpose of writing
27project specific submit rules.
28link:http://gerrit-documentation.googlecode.com/svn/ReleaseNotes/ReleaseNotes-2.2.2.html[Gerrit
292.2.2 ReleaseNotes] introduces Prolog support in Gerrit.
30
Sasa Zivkovb91296d2012-11-08 14:19:12 +010031Submit Type
32-----------
33A 'Submit Type' is a strategy that is used on submit to integrate the
34change into the destination branch. Supported submit types are:
35
36* `Fast Forward Only`
37* `Merge If Necessary`
38* `Merge Always`
39* `Cherry Pick`
40* `Rebase If Necessary`
41
42'Submit Type' is a project global setting. This means that the same submit type
43is used for all changes of one project.
44
45Projects which need more flexibility in choosing, or enforcing, a submit type
46can use Prolog based submit type which replaces the project's default submit
47type.
48
49Prolog based submit type computes a submit type for each change. The computed
50submit type is shown on the change screen for each change.
51
Sasa Zivkovae50f712012-07-24 14:51:44 +020052Prolog Language
53---------------
54This document is not a complete Prolog tutorial.
55link:http://en.wikipedia.org/wiki/Prolog[This Wikipedia page on Prolog] is a
56good starting point for learning the Prolog language. This document will only explain
57some elements of Prolog that are necessary to understand the provided examples.
58
59Prolog in Gerrit
60----------------
61Gerrit uses its own link:https://code.google.com/p/prolog-cafe/[fork] of the
62original link:http://kaminari.istc.kobe-u.ac.jp/PrologCafe/[prolog-cafe]
63project. Gerrit embeds the prolog-cafe library and can interpret Prolog programs at
64runtime.
65
66Interactive Prolog Cafe Shell
67-----------------------------
68For interactive testing and playing with Prolog, Gerrit provides the
69link:pgm-prolog-shell.html[prolog-shell] program which opens an interactive
70Prolog interpreter shell.
71
Johan Björk2119f052012-10-24 12:10:45 -040072NOTE: The interactive shell is just a prolog shell, it does not load
73a gerrit server environment and thus is not intended for xref:TestingSubmitRules[testing submit rules].
Sasa Zivkovae50f712012-07-24 14:51:44 +020074
75SWI-Prolog
76----------
77Instead of using the link:pgm-prolog-shell.html[prolog-shell] program one can
78also use the link:http://www.swi-prolog.org/[SWI-Prolog] environment. It
79provides a better shell interface and a graphical source-level debugger.
80
81The rules.pl file
82-----------------
83This section explains how to create and edit project specific submit rules. How
84to actually write the submit rules is explained in the next section.
85
86Project specific submit rules are stored in the `rules.pl` file in the
87`refs/meta/config` branch of that project. Therefore, we need to fetch and
88checkout the `refs/meta/config` branch in order to create or edit the `rules.pl`
89file:
90
91====
92 $ git fetch origin refs/meta/config:config
93 $ git checkout config
94 ... edit or create the rules.pl file
95 $ git add rules.pl
96 $ git commit -m "My submit rules"
97 $ git push origin HEAD:refs/meta/config
98====
99
Sasa Zivkovb91296d2012-11-08 14:19:12 +0100100[[HowToWriteSubmitRules]]
Sasa Zivkovae50f712012-07-24 14:51:44 +0200101How to write submit rules
102-------------------------
103Whenever Gerrit needs to evaluate submit rules for a change `C` from project `P` it
104will first initialize the embedded Prolog interpreter by:
105
106* consulting a set of facts about the change `C`
107* consulting the `rules.pl` from the project `P`
108
109Conceptually we can imagine that Gerrit adds a set of facts about the change
110`C` on top of the `rules.pl` file and then consults it. The set of facts about
111the change `C` will look like:
112
113====
114 :- package gerrit. <1>
115
116 commit_author(user(1000000), 'John Doe', 'john.doe@example.com'). <2>
117 commit_committer(user(1000000), 'John Doe', 'john.doe@example.com'). <3>
118 commit_message('Add plugin support to Gerrit'). <4>
119 ...
120====
121
122<1> Gerrit will provide its facts in a package named `gerrit`. This means we
123have to use qualified names when writing our code and referencing these facts.
124For example: `gerrit:commit_author(ID, N, M)`
125<2> user ID, full name and email address of the commit author
126<3> user ID, full name and email address of the commit committer
127<4> commit message
128
129A complete set of facts which Gerrit provides about the change is listed in the
130link:prolog-change-facts.html[Prolog Facts for Gerrit Change].
131
132By default, Gerrit will search for a `submit_rule/1` predicate in the `rules.pl`
133file, evaluate the `submit_rule(X)` and then inspect the value of `X` in order
134to decide whether the change is submittable or not and also to find the set of
135needed criteria for the change to become submittable. This means that Gerrit has an
136expectation on the format and value of the result of the `submit_rule` predicate
137which is expected to be a `submit` term of the following format:
138
139====
140 submit(label(label-name, status) [, label(label-name, status)]*)
141====
142
143where `label-name` is usually `'Code-Review'` or `'Verified'` but could also
144be any other string (see examples below). The `status` is one of:
145
146* `ok(user(ID))` or just `ok(_)` if user info is not important. This status is
147 used to tell that this label/category has been met.
148* `need(_)` is used to tell that this label/category is needed for change to
149 become submittable
150* `reject(user(ID))` or just `reject(_)`. This status is used to tell that label/category
151 is blocking change submission
152* `impossible(_)` is used when the logic knows that the change cannot be submitted as-is.
153 Administrative intervention is probably required. This is meant for cases
154 where the logic requires members of "FooEng" to score "Code-Review +2" on a
155 change, but nobody is in group "FooEng". It is to hint at permissions
156 misconfigurations.
157* `may(_)` allows expression of approval categories that are optional, i.e.
158 could either be set or unset without ever influencing whether the change
159 could be submitted.
160
161NOTE: For a change to be submittable all `label` terms contained in the returned
162`submit` term must have either `ok` or `may` status.
163
164IMPORTANT: Gerrit will let the Prolog engine continue searching for solutions of
165the `submit_rule(X)` query until it finds the first one where all labels in the
166return result have either status `ok` or `may` or there are no more solutions.
167If a solution where all labels have status `ok` is found then all previously
168found solutions are ignored. Otherwise, all labels names with status `need`
169from all solutions will be displayed in the UI indicating the set of conditions
170needed for the change to become submittable.
171
172Here some examples of possible return values from the `submit_rule` predicate:
173
174====
175 submit(label('Code-Review', ok(_))) <1>
176 submit(label('Code-Review', ok(_)), label('Verified', reject(_))) <2>
177 submit(label('Author-is-John-Doe', need(_)) <3>
178====
179
180<1> label `'Code-Review'` is met. As there are no other labels in the
181 return result, the change is submittable.
182<2> label `'Verified'` is rejected. Change is not submittable.
183<3> label `'Author-is-John-Doe'` is needed for the change to become submittable.
184 Note that this tells nothing about how this criteria will be met. It is up
185 to the implementor of the `submit_rule` to return `label('Author-is-John-Doe',
186 ok(_))` when this criteria is met. Most likely, it will have to match
187 against `gerrit:commit_author` in order to check if this criteria is met.
188 This will become clear through the examples below.
189
190Of course, when implementing the `submit_rule` we will use the facts about the
191change that are already provided by Gerrit.
192
193Another aspect of the return result from the `submit_rule` predicate is that
194Gerrit uses it to decide which set of labels to display on the change review
195screen for voting. If the return result contains label `'ABC'` and if the label
Dave Borowitz01c1b1f2013-02-27 13:49:04 -0800196`'ABC'` is link:config-labels.html[defined for the project] then voting for the
197label `'ABC'` will be displayed. Otherwise, it is not displayed. Note that the
198project doesn't need a defined label for each label contained in the result of
Sasa Zivkovae50f712012-07-24 14:51:44 +0200199`submit_rule` predicate. For example, the decision whether `'Author-is-John-Doe'`
200label is met will probably not be made by explicit voting but, instead, by
201inspecting the facts about the change.
202
Sasa Zivkovb91296d2012-11-08 14:19:12 +0100203[[SubmitFilter]]
Sasa Zivkov3d4f0aa2012-08-07 15:11:32 +0200204Submit Filter
205-------------
206Another mechanism of changing the default submit rules is to implement the
207`submit_filter/2` predicate. While Gerrit will search for the `submit_rule` only
208in the `rules.pl` file of the current project, the `submit_filter` will be
209searched for in the `rules.pl` of all parent projects of the current project,
210but not in the `rules.pl` of the current project. The search will start from the
211immediate parent of the current project, then in the parent project of that
212project and so on until, and including, the 'All-Projects' project.
213
214The purpose of the submit filter is, as its name says, to filter the results
215of the `submit_rule`. Therefore, the `submit_filter` predicate has two
216parameters:
217
218====
219 submit_filter(In, Out) :- ...
220====
221
222Gerrit will invoke `submit_filter` with the `In` parameter containing a `submit`
223structure produced by the `submit_rule` and will take the value of the `Out`
224parameter as the result.
225
226The `Out` value of a `submit_filter` will become the `In` value for the
227next `submit_filter` in the parent line. The value of the `Out` parameter
228of the top-most `submit_filter` is the final result of the submit rule that
229is used to decide whether a change is submittable or not.
230
231IMPORTANT: `submit_filter` is a mechanism for Gerrit administrators to implement
232and enforce submit rules that would apply to all projects while `submit_rule` is
233a mechanism for project owners to implement project specific submit rules.
234However, project owners who own several projects could also make use of
235`submit_filter` by using a common parent project for all their projects and
236implementing the `submit_filter` in this common parent project. This way they
237can avoid implementing the same `submit_rule` in all their projects.
238
239The following "drawing" illustrates the order of the invocation and the chaining
240of the results of the `submit_rule` and `submit_filter` predicates.
Sasa Zivkov3d4f0aa2012-08-07 15:11:32 +0200241====
242 All-Projects
243 ^ submit_filter(B, S) :- ... <4>
244 |
245 Parent-3
246 ^ <no submit filter here>
247 |
248 Parent-2
249 ^ submit_filter(A, B) :- ... <3>
250 |
251 Parent-1
252 ^ submit_filter(X, A) :- ... <2>
253 |
254 MyProject
255 submit_rule(X) :- ... <1>
256====
257
258<1> The `submit_rule` of `MyProject` is invoked first.
259<2> The result `X` is filtered through the `submit_filter` from the `Parent-1`
260project.
261<3> The result of `submit_filter` from `Parent-1` project is filtered by the
262`submit_filter` in the `Parent-2` project. Since `Parent-3` project doesn't have
263a `submit_filter` it is skipped.
264<4> The result of `submit_filter` from `Parent-2` project is filtered by the
265`submit_filter` in the `All-Projects` project. The value in `S` is the final
266value of the submit rule evaluation.
267
268NOTE: If `MyProject` doesn't define its own `submit_rule` Gerrit will invoke the
269default implementation of submit rule that is named `gerrit:default_submit` and
270its result will be filtered as described above.
271
Sasa Zivkovb91296d2012-11-08 14:19:12 +0100272How to write submit type
273------------------------
274Writing custom submit type logic in Prolog is the similar top
275xref:HowToWriteSubmitRules[writing submit rules]. The only difference is that
276one has to implement a `submit_type` predicate (instead of the `submit_rule`)
277and that the return result of the `submit_type` has to be an atom that
278represents one of the supported submit types:
279
280* `fast_forward_only`
281* `merge_if_necessary`
282* `merge_always`
283* `cherry_pick`
284* `rebase_if_necessary`
285
286Submit Type Filter
287------------------
288Submit type filter works the same way as the xref:SubmitFilter[Submit Filter]
289where the name of the filter predicate is `submit_type_filter`.
290
291====
292 submit_type_filter(In, Out).
293====
294
295Gerrit will invoke `submit_type_filter` with the `In` parameter containing a
296result of the `submit_type` and will take the value of the `Out` parameter as
297the result.
298
Johan Björk2119f052012-10-24 12:10:45 -0400299[[TestingSubmitRules]]
300Testing submit rules
301--------------------
302The prolog environment running the `submit_rule` is loaded with state describing the
303change that is being evaluated. The easiest way to load this state is to test your
304`submit_rule` against a real change on a running gerrit instance. The command
305link:cmd-test-submit-rule.html[test-submit-rule] loads a specific change and executes
306the `submit_rule`. It optionally reads the rule from from `stdin` to facilitate easy testing.
307
308====
309 cat rules.pl | ssh gerrit_srv gerrit test-submit-rule I45e080b105a50a625cc8e1fb5b357c0bfabe6d68 -s
310====
Sasa Zivkov3d4f0aa2012-08-07 15:11:32 +0200311
Sasa Zivkovae50f712012-07-24 14:51:44 +0200312Prolog vs Gerrit plugin for project specific submit rules
313---------------------------------------------------------
314Since version 2.5 Gerrit supports plugins and extension points. A plugin or an
315extension point could also be used as another means to provide custom submit
316rules. One could ask for a guideline when to use Prolog based submit rules and
317when to go for writing a new plugin. Writing a Prolog program is usually much
318faster than writing a Gerrit plugin. Prolog based submit rules can be pushed
319to a project by project owners while Gerrit plugins could only be installed by
320Gerrit administrators. In addition, Prolog based submit rules can be pushed
321for review by pushing to `refs/for/refs/meta/config` branch.
322
323On the other hand, Prolog based submit rules get a limited amount of facts about
324the change exposed to them. Gerrit plugins get full access to Gerrit internals
325and can potentially check more things than Prolog based rules.
326
Sasa Zivkovd0e55262013-01-16 14:26:06 +0100327From version 2.6 Gerrit plugins can contribute Prolog predicates. This way, we
328can make use of the plugin provided predicates when writing Prolog based rules.
329
Sasa Zivkovb91296d2012-11-08 14:19:12 +0100330Examples - Submit Rule
331----------------------
Sasa Zivkovae50f712012-07-24 14:51:44 +0200332The following examples should serve as a cookbook for developing own submit rules.
333Some of them are too trivial to be used in production and their only purpose is
334to provide step by step introduction and understanding.
335
Sasa Zivkov3d4f0aa2012-08-07 15:11:32 +0200336Some of the examples will implement the `submit_rule` and some will implement
337the `submit_filter` just to show both possibilities. Remember that
338`submit_rule` is only invoked from the current project and `submit_filter` is
339invoked from all parent projects. This is the most important fact in deciding
340whether to implement `submit_rule` or `submit_filter`.
341
Sasa Zivkovae50f712012-07-24 14:51:44 +0200342Example 1: Make every change submittable
343~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344Let's start with a most trivial example where we would make every change submittable
345regardless of the votes it has:
346
347.rules.pl
348[caption=""]
349====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100350 submit_rule(submit(W)) :-
351 W = label('Any-Label-Name', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200352====
353
354In this case we make no use of facts about the change. We don't need it as we are simply
355making every change submittable. Note that, in this case, the Gerrit UI will not show
356the UI for voting for the standard `'Code-Review'` and `'Verified'` categories as labels
357with these names are not part of the return result. The `'Any-Label-Name'` could really
358be any string.
359
360Example 2: Every change submittable and voting in the standard categories possible
361~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362This is continuation of the previous example where, in addition, to making
363every change submittable we want to enable voting in the standard
364`'Code-Review'` and `'Verified'` categories.
365
366.rules.pl
367[caption=""]
368====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100369 submit_rule(submit(CR, V)) :-
370 CR = label('Code-Review', ok(_)),
371 V = label('Verified', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200372====
373
374Since for every change all label statuses are `'ok'` every change will be submittable.
375Voting in the standard labels will be shown in the UI as the standard label names are
376included in the return result.
377
378Example 3: Nothing is submittable
379~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
380This example shows how to make all changes non-submittable regardless of the
381votes they have.
382
383.rules.pl
384[caption=""]
385====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100386 submit_rule(submit(R)) :-
387 R = label('Any-Label-Name', reject(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200388====
389
390Since for any change we return only one label with status `reject`, no change
391will be submittable. The UI will, however, not indicate what is needed for a
392change to become submittable as we return no labels with status `need`.
393
394Example 4: Nothing is submittable but UI shows several 'Need ...' criteria
395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396In this example no change is submittable but here we show how to present 'Need
397<label>' information to the user in the UI.
398
399.rules.pl
400[caption=""]
401====
402 % In the UI this will show: Need Any-Label-Name
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100403 submit_rule(submit(N)) :-
404 N = label('Any-Label-Name', need(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200405
406 % We could define more "need" labels by adding more rules
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100407 submit_rule(submit(N)) :-
408 N = label('Another-Label-Name', need(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200409
410 % or by providing more than one need label in the same rule
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100411 submit_rule(submit(NX, NY)) :-
412 NX = label('X-Label-Name', need(_)),
413 NY = label('Y-Label-Name', need(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200414====
415
416In the UI this will show:
417****
418* Need Any-Label-Name
419* Need Another-Label-Name
420* Need X-Label-Name
421* Need Y-Label-Name
422****
423
424From the example above we can see a few more things:
425
426* comment in Prolog starts with the `%` character
427* there could be multiple `submit_rule` predicates. Since Prolog, by default, tries to find
428 all solutions for a query, the result will be union of all solutions.
429 Therefore, we see all 4 `need` labels in the UI.
430
431Example 5: The 'Need ...' labels not shown when change is submittable
432~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433This example shows that, when there is a solution for `submit_rule(X)` where all labels
434have status `ok` then Gerrit will not show any labels with the `need` status from
435any of the previous `submit_rule(X)` solutions.
436
437.rules.pl
438[caption=""]
439====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100440 submit_rule(submit(N)) :-
441 N = label('Some-Condition', need(_)).
442
443 submit_rule(submit(OK)) :-
444 OK = label('Another-Condition', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200445====
446
447The 'Need Some-Condition' will not be show in the UI because of the result of
448the second rule.
449
450The same is valid if the two rules are swapped:
451
452.rules.pl
453[caption=""]
454====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100455 submit_rule(submit(OK)) :-
456 OK = label('Another-Condition', ok(_)).
457
458 submit_rule(submit(N)) :-
459 N = label('Some-Condition', need(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200460====
461
462The result of the first rule will stop search for any further solutions.
463
464Example 6: Make change submittable if commit author is "John Doe"
465~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
466This is the first example where we will use the Prolog facts about a change that
467are automatically exposed by Gerrit. Our goal is to make any change submittable
468when the commit author is named `'John Doe'`. In the very first
469step let's make sure Gerrit UI shows 'Need Author-is-John-Doe' in
470the UI to clearly indicate to the user what is needed for a change to become
471submittable:
472
473.rules.pl
474[caption=""]
475====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100476 submit_rule(submit(Author)) :-
477 Author = label('Author-is-John-Doe', need(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200478====
479
480This will show:
481****
482* Need Author-is-John-Doe
483****
484
485in the UI but no change will be submittable yet. Let's add another rule:
486
487.rules.pl
488[caption=""]
489====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100490 submit_rule(submit(Author)) :-
491 Author = label('Author-is-John-Doe', need(_)).
492
493 submit_rule(submit(Author)) :-
494 gerrit:commit_author(_, 'John Doe', _),
495 Author = label('Author-is-John-Doe', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200496====
497
498In the second rule we return `ok` status for the `'Author-is-John-Doe'` label
499if there is a `commit_author` fact where the full name is `'John Doe'`. If
500author of a change is `'John Doe'` then the second rule will return a solution
501where all labels have `ok` status and the change will become submittable. If
502author of a change is not `'John Doe'` then only the first rule will produce a
503solution. The UI will show 'Need Author-is-John-Doe' but, as expected, the
504change will not be submittable.
505
506Instead of checking by full name we could also check by the email address:
507
508.rules.pl
509[caption=""]
510====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100511 submit_rule(submit(Author)) :-
512 Author = label('Author-is-John-Doe', need(_)).
513
514 submit_rule(submit(Author)) :-
515 gerrit:commit_author(_, _, 'john.doe@example.com'),
516 Author = label('Author-is-John-Doe', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200517====
518
519or by user id (assuming it is 1000000):
520
521.rules.pl
522[caption=""]
523====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100524 submit_rule(submit(Author)) :-
525 Author = label('Author-is-John-Doe', need(_)).
526
527 submit_rule(submit(Author)) :-
528 gerrit:commit_author(user(1000000), _, _),
529 Author = label('Author-is-John-Doe', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200530====
531
532or by a combination of these 3 attributes:
533
534.rules.pl
535[caption=""]
536====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100537 submit_rule(submit(Author)) :-
538 Author = label('Author-is-John-Doe', need(_)).
539
540 submit_rule(submit(Author)) :-
541 gerrit:commit_author(_, 'John Doe', 'john.doe@example.com'),
542 Author = label('Author-is-John-Doe', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200543====
544
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100545Example 7: Make change submittable if commit message starts with "Fix "
546~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sasa Zivkovae50f712012-07-24 14:51:44 +0200547Besides showing how to make use of the commit message text the purpose of this
548example is also to show how to match only a part of a string symbol. Similarly
549like commit author the commit message is provided as a string symbol which is
550an atom in Prolog terms. When working with an atom we could only match against
551the whole value. To match only part of a string symbol we have, at least, two
552options:
553
554* convert the string symbol into a list of characters and then perform
555 the "classical" list matching
556* use the `regex_matches/2` or, even more convenient, the
557 `gerrit:commit_message_matches/1` predicate
558
559Let's implement both options:
560
561.rules.pl
562[caption=""]
563====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100564 submit_rule(submit(Fix)) :-
565 Fix = label('Commit-Message-starts-with-Fix', need(_)).
566
567 submit_rule(submit(Fix)) :-
568 gerrit:commit_message(M), name(M, L), starts_with(L, "Fix "),
569 Fix = label('Commit-Message-starts-with-Fix', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200570
571 starts_with(L, []).
572 starts_with([H|T1], [H|T2]) :- starts_with(T1, T2).
573====
574
575NOTE: The `name/2` embedded predicate is used to convert a string symbol into a
576list of characters. A string `abc` is converted into a list of characters `[97,
57798, 99]`. A double quoted string in Prolog is just a shortcut for creating a
578list of characters. `"abc"` is a shortcut for `[97, 98, 99]`. This is why we use
579double quotes for the `"Trivial Fix"` in the example above.
580
581The `starts_with` predicate is self explaining.
582
583Using the `gerrit:commit_message_matches` predicate is probably more efficient:
584
585.rules.pl
586[caption=""]
587====
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100588 submit_rule(submit(Fix)) :-
589 Fix = label('Commit-Message-starts-with-Fix', need(_)).
590
591 submit_rule(submit(Fix)) :-
592 gerrit:commit_message_matches('^Fix '),
593 Fix = label('Commit-Message-starts-with-Fix', ok(_)).
Sasa Zivkovae50f712012-07-24 14:51:44 +0200594====
595
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100596The previous example could also be written so that it first checks if the commit
597message starts with 'Fix '. If true then it sets OK for that category and stops
598further backtracking by using the cut `!` operator:
599.rules.pl
600[caption=""]
601====
602 submit_rule(submit(Fix)) :-
603 gerrit:commit_message_matches('^Fix '),
604 Fix = label('Commit-Message-starts-with-Fix', ok(_)),
605 !.
606
607 % Message does not start with 'Fix ' so Fix is needed to submit
608 submit_rule(submit(Fix)) :-
609 Fix = label('Commit-Message-starts-with-Fix', need(_)).
610====
611
612The default submit policy
613-------------------------
Sasa Zivkovae50f712012-07-24 14:51:44 +0200614All examples until now concentrate on one particular aspect of change data.
615However, in real-life scenarios we would rather want to reuse Gerrit's default
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100616submit policy and extend/change it for our specific purpose. This could be
617done in one of the following ways:
Sasa Zivkovae50f712012-07-24 14:51:44 +0200618
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100619* understand how the default submit policy is implemented and use that as a
620 template for implementing custom submit rules,
621* invoke the default submit rule implementation and then perform further
622 actions on its return result.
623
624Default submit rule implementation
625~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
626The default submit rule with the two default categories, `Code-Review` and
627`Verified`, can be implemented as:
628
629.rules.pl
630[caption=""]
631====
632 submit_rule(submit(V, CR)) :-
633 gerrit:max_with_block(-2, 2, 'Code-Review', CR),
634 gerrit:max_with_block(-1, 1, 'Verified', V).
635====
636
637Once this implementation is understood it can be customized to implement
638project specific submit rules. Note, that this implementation hardcodes
639the two default categories. Introducing a new category in the database would
640require introducing the same category here or a `submit_filter` in a parent
641project would have to care about including the new category in the result of
642this `submit_rule`. On the other side, this example is easy to read and
643understand.
644
645Reusing the default submit policy
646~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sasa Zivkovae50f712012-07-24 14:51:44 +0200647To get results of Gerrits default submit policy we use the
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100648`gerrit:default_submit` predicate. The `gerrit:default_submit(X)` includes all
649categories from the database. This means that if we write a submit rule like:
Sasa Zivkovae50f712012-07-24 14:51:44 +0200650
651.rules.pl
652[caption=""]
653====
654 submit_rule(X) :- gerrit:default_submit(X).
655====
Sasa Zivkovae50f712012-07-24 14:51:44 +0200656then this is equivalent to not using `rules.pl` at all. We just delegate to
657default logic. However, once we invoke the `gerrit:default_submit(X)` we can
658perform further actions on the return result `X` and apply our specific
659logic. The following pattern illustrates this technique:
660
661.rules.pl
662[caption=""]
663====
664 submit_rule(S) :- gerrit:default_submit(R), project_specific_policy(R, S).
665
666 project_specific_policy(R, S) :- ...
667====
668
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100669In the following examples both styles will be shown.
Sasa Zivkovae50f712012-07-24 14:51:44 +0200670
671Example 8: Make change submittable only if `Code-Review+2` is given by a non author
672~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
673In this example we introduce a new label `Non-Author-Code-Review` and make it
674satisfied if there is at least one `Code-Review+2` from a non author. All other
675default policies like the `Verified` category and vetoing changes still apply.
676
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100677Reusing the `gerrit:default_submit`
678^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sasa Zivkovae50f712012-07-24 14:51:44 +0200679First, we invoke `gerrit:default_submit` to compute the result for the default
680submit policy and then add the `Non-Author-Code-Review` label to it. The
681`Non-Author-Code-Review` label is added with status `ok` if such an approval
682exists or with status `need` if it doesn't exist.
683
684.rules.pl
685[caption=""]
686====
687 submit_rule(S) :-
688 gerrit:default_submit(X),
689 X =.. [submit | Ls],
690 add_non_author_approval(Ls, R),
691 S =.. [submit | R].
692
693 add_non_author_approval(S1, S2) :-
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100694 gerrit:commit_author(A),
695 gerrit:commit_label(label('Code-Review', 2), R),
Sasa Zivkovae50f712012-07-24 14:51:44 +0200696 R \= A, !,
697 S2 = [label('Non-Author-Code-Review', ok(R)) | S1].
698 add_non_author_approval(S1, [label('Non-Author-Code-Review', need(_)) | S1]).
699====
700
701This example uses the `univ` operator `=..` to "unpack" the result of the
702default_submit, which is a structure of the form `submit(label('Code-Review',
703ok(_)), label('Verified', need(_)) ...)` into a list like `[submit,
704label('Code-Review', ok(_)), label('Verified', need(_)), ...]`. Then we
705process the tail of the list (the list of labels) as a Prolog list, which is
706much easier than processing a structure. In the end we use the same `univ`
707operator to convert the resulting list of labels back into a `submit` structure
708which is expected as a return result. The `univ` operator works both ways.
709
710In `add_non_author_approval` we use the `cut` operator `!` to prevent Prolog
711from searching for more solutions once the `cut` point is reached. This is
712important because in the second `add_non_author_approval` rule we just add the
713`label('Non-Author-Code-Review', need(_))` without first checking that there
714is no non author `Code-Review+2`. The second rule will only be reached
715if the `cut` in the first rule is not reached and it only happens if a
716predicate before the `cut` fails.
717
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100718Don't use `gerrit:default_submit`
719^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
720Let's implement the same submit rule the other way, without reusing the
721`gerrit:default_submit`:
722
723.rules.pl
724[caption=""]
725====
726 submit_rule(submit(CR, V)) :-
727 base(CR, V),
728 CR = label(_, ok(Reviewer)),
729 gerrit:commit_author(Author),
730 Author \= Reviewer,
731 !.
732
733 submit_rule(submit(CR, V, N)) :-
734 base(CR, V),
735 N = label('Non-Author-Code-Review', need(_)).
736
737 base(CR, V) :-
738 gerrit:max_with_block(-2, 2, 'Code-Review', CR),
739 gerrit:max_with_block(-1, 1, 'Verified', V).
740====
741
742The latter implementation is probably easier to understand and the code looks
743cleaner. Note, however, that the latter implementation will always return the
744two standard categories only (`Code-Review` and `Verified`) even if a new
745category has beeen inserted into the database. To include the new category
746the `rules.pl` would need to be modified or a `submit_filter` in a parent
747project would have to care about including the new category in the result
748of this `submit_rule`.
749
750The former example, however, would include any newly added category as it
751invokes the `gerrit:default_submit` and then modifies its result.
752
753Which of these two behaviors is desired will always depend on how a particular
754Gerrit server is managed.
755
Sasa Zivkovae50f712012-07-24 14:51:44 +0200756Example 9: Remove the `Verified` category
757~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
758A project has no build and test. It consists of only text files and needs only
759code review. We want to remove the `Verified` category from this project so
760that `Code-Review+2` is the only criteria for a change to become submittable.
761We also want the UI to not show the `Verified` category in the table with
762votes and on the voting screen.
763
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100764This is quite simple without reusing the 'gerrit:default_submit`:
765
766.rules.pl
767[caption=""]
768====
769 submit_rule(submit(CR)) :-
770 gerrit:max_with_block(-2, 2, 'Code-Review', CR).
771====
772
773Implementing the same rule by reusing `gerrit:default_submit` is a bit more complex:
774
Sasa Zivkovae50f712012-07-24 14:51:44 +0200775.rules.pl
776[caption=""]
777====
778 submit_rule(S) :-
779 gerrit:default_submit(X),
780 X =.. [submit | Ls],
781 remove_verified_category(Ls, R),
782 S =.. [submit | R].
783
784 remove_verified_category([], []).
785 remove_verified_category([label('Verified', _) | T], R) :- remove_verified_category(T, R), !.
786 remove_verified_category([H|T], [H|R]) :- remove_verified_category(T, R).
787====
788
789Example 10: Combine examples 8 and 9
790~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
791In this example we want to both remove the verified and have the four eyes
792principle. This means we want a combination of examples 7 and 8.
793
794.rules.pl
795[caption=""]
796====
797 submit_rule(S) :-
798 gerrit:default_submit(X),
799 X =.. [submit | Ls],
800 remove_verified_category(Ls, R1),
801 add_non_author_approval(R1, R),
802 S =.. [submit | R].
803====
804
805The `remove_verified_category` and `add_non_author_approval` predicates are the
806same as defined in the previous two examples.
807
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100808Without reusing the `gerrit:default_submit` the same example may be implemented
809as:
810
811.rules.pl
812[caption=""]
813====
814 submit_rule(submit(CR)) :-
815 base(CR),
816 CR = label(_, ok(Reviewer)),
817 gerrit:commit_author(Author),
818 Author \= Reviewer,
819 !.
820
821 submit_rule(submit(CR, N)) :-
822 base(CR),
823 N = label('Non-Author-Code-Review', need(_)).
824
825 base(CR) :-
826 gerrit:max_with_block(-2, 2, 'Code-Review', CR),
827====
828
Sasa Zivkov3d4f0aa2012-08-07 15:11:32 +0200829Example 11: Remove the `Verified` category from all projects
830~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
831Example 9, implements `submit_rule` that removes the `Verified` category from
832one project. In this example we do the same but we want to remove the `Verified`
833category from all projects. This means we have to implement `submit_filter` and
834we have to do that in the `rules.pl` of the `All-Projects` project.
835
836.rules.pl
837[caption=""]
838====
839 submit_filter(In, Out) :-
840 In =.. [submit | Ls],
841 remove_verified_category(Ls, R),
842 Out =.. [submit | R].
843
844 remove_verified_category([], []).
845 remove_verified_category([label('Verified', _) | T], R) :-
846 remove_verified_category(T, R), !.
847 remove_verified_category([H|T], [H|R]) :- remove_verified_category(T, R).
848====
849
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100850Example 12: On release branches require DrNo in addition to project rules
851~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
852A new category 'DrNo' is added to the database and is required for release
853branches. To mark a branch as a release branch we use `drno('refs/heads/branch')`.
854
855.rules.pl
856[caption=""]
857====
858 drno('refs/heads/master').
859 drno('refs/heads/stable-2.3').
860 drno('refs/heads/stable-2.4').
861 drno('refs/heads/stable-2.5').
862 drno('refs/heads/stable-2.5').
863
864 submit_filter(In, Out) :-
865 gerrit:change_branch(Branch),
866 drno(Branch),
867 !,
868 In =.. [submit | I],
869 gerrit:max_with_block(-1, 1, 'DrNo', DrNo),
870 Out =.. [submit, DrNo | I].
871
872 submit_filter(In, Out) :- In = Out.
873====
874
875Example 13: 1+1=2 Code-Review
Johan Björkef028542012-10-24 12:06:33 -0400876~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
877In this example we introduce accumulative voting to determine if a change is
878submittable or not. We modify the standard Code-Review to be accumulative, and make the
879change submittable if the total score is 2 or higher.
880
881The code in this example is very similar to Example 8, with the addition of findall/3
882and gerrit:remove_label.
883The findall/3 embedded predicate is used to form a list of all objects that satisfy a
884specified Goal. In this example it is used to get a list of all the 'Code-Review' scores.
885gerrit:remove_label is a built-in helper that is implemented similarly to the
886'remove_verified_category' as seen in the previous example.
887
888.rules.pl
889[caption=""]
890====
891 sum_list([], 0).
892 sum_list([H | Rest], Sum) :- sum_list(Rest,Tmp), Sum is H + Tmp.
893
894 add_category_min_score(In, Category, Min, P) :-
895 findall(X, gerrit:commit_label(label(Category,X),R),Z),
896 sum_list(Z, Sum),
897 Sum >= Min, !,
898 P = [label(Category,ok(R)) | In].
899
900 add_category_min_score(In, Category,Min,P) :-
901 P = [label(Category,need(Min)) | In].
902
903 submit_rule(S) :-
904 gerrit:default_submit(X),
905 X =.. [submit | Ls],
906 gerrit:remove_label(Ls,label('Code-Review',_),NoCR),
907 add_category_min_score(NoCR,'Code-Review', 2, Labels),
908 S =.. [submit | Labels].
909====
910
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100911Implementing the same example without using `gerrit:default_submit`:
912
913.rules.pl
914[caption=""]
915====
916 submit_rule(submit(CR, V)) :-
917 sum(2, 'Code-Review', CR),
918 gerrit:max_with_block(-1, 1, 'Verified', V).
919
920 % Sum the votes in a category. Uses a helper function score/2
921 % to select out only the score values the given category.
922 sum(VotesNeeded, Category, label(Category, ok(_))) :-
923 findall(Score, score(Category, Score), All),
924 sum_list(All, Sum),
925 Sum >= VotesNeeded,
926 !.
927 sum(VotesNeeded, Category, label(Category, need(VotesNeeded))).
928
929 score(Category, Score) :-
930 gerrit:commit_label(label(Category, Score), User).
931
932 % Simple Prolog routine to sum a list of integers.
933 sum_list(List, Sum) :- sum_list(List, 0, Sum).
934 sum_list([X|T], Y, S) :- Z is X + Y, sum_list(T, Z, S).
935 sum_list([], S, S).
936====
937
938Example 14: Master and apprentice
Johan Björk87927a92012-10-25 15:00:36 -0400939~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
940The master and apprentice example allow you to specify a user (the `master`)
941that must approve all changes done by another user (the `apprentice`).
942
943The code first checks if the commit author is in the apprentice database.
944If the commit is done by an apprentice, it will check if there is a +2
945review by the associated `master`.
946
947.rules.pl
948[caption=""]
949====
950 % master_apprentice(Master, Apprentice).
951 % Extend this with appropriate user-id's for your master/apprentice setup.
952 master_apprentice(user(1000064), user(1000000)).
953
954 submit_rule(S) :-
955 gerrit:default_submit(In),
956 In =.. [submit | Ls],
957 add_apprentice_master(Ls, R),
958 S =.. [submit | R].
959
960 check_master_approval(S1, S2, Master) :-
961 gerrit:commit_label(label('Code-Review', 2), R),
962 R = Master, !,
963 S2 = [label('Master-Approval', ok(R)) | S1].
964 check_master_approval(S1, [label('Master-Approval', need(_)) | S1], _).
965
966 add_apprentice_master(S1, S2) :-
967 gerrit:commit_author(Id),
968 master_apprentice(Master, Id),
969 !,
970 check_master_approval(S1, S2, Master).
971
972 add_apprentice_master(S, S).
973====
974
Sasa Zivkov79b08cc2013-01-16 17:00:54 +0100975Example 15: Only allow Author to submit change
Johan Björk0f132542012-10-26 10:16:07 -0400976~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
977This example adds a new needed category `Patchset-Author` for any user that is
978not the author of the patch. This effectively blocks all users except the author
979from submitting the change. This could result in an impossible situation if the
980author does not have permissions for submitting the change.
981
982.rules.pl
983[caption=""]
984====
985 submit_rule(S) :-
986 gerrit:default_submit(In),
987 In =.. [submit | Ls],
988 only_allow_author_to_submit(Ls, R),
989 S =.. [submit | R].
990
991 only_allow_author_to_submit(S, S) :-
992 gerrit:commit_author(Id),
993 gerrit:current_user(Id),
994 !.
995
996 only_allow_author_to_submit(S1, [label('Patchset-Author', need(_)) | S1]).
997====
998
Sasa Zivkovb91296d2012-11-08 14:19:12 +0100999Examples - Submit Type
1000----------------------
1001The following examples show how to implement own submit type rules.
1002
1003Example 1: Set a `Cherry Pick` submit type for all changes
1004~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1005This example sets the `Cherry Pick` submit type for all changes. It overrides
1006whatever is set as project default submit type.
1007
1008rules.pl
1009[caption=""]
1010====
1011 submit_type(cherry_pick).
1012====
1013
1014
1015Example 2: `Fast Forward Only` for all `refs/heads/stable*` branches
1016~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1017For all `refs/heads/stable.*` branches we would like to enforce the `Fast
1018Forward Only` submit type. A reason for this decision may be a need to never
1019break the build in the stable branches. For all other branches, those not
1020matching the `refs/heads/stable.*` pattern, we would like to use the project's
1021default submit type as defined on the project settings page.
1022
1023.rules.pl
1024[caption=""]
1025====
1026 submit_type(fast_forward_only) :-
1027 gerrit:change_branch(B), regex_matches('refs/heads/stable.*', B),
1028 !.
1029 submit_type(T) :- gerrit:project_default_submit_type(T)
1030====
1031
1032The first `submit_type` predicate defines the `Fast Forward Only` submit type
1033for `refs/heads/stable.*` branches. The second `submit_type` predicate returns
1034the project's default submit type.
1035
1036Example 3: Don't require `Fast Forward Only` if only documentation was changed
1037~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1038Like in the previous example we want the `Fast Forward Only` submit type for
1039the `refs/heads/stable*` branches. However, if only documentation was changed
1040(only `*.txt` files), then we allow project's default submit type for such
1041changes.
1042
1043.rules.pl
1044[caption=""]
1045====
1046 submit_type(fast_forward_only) :-
1047 gerrit:commit_delta('(?<!\.txt)$'),
1048 gerrit:change_branch(B), regex_matches('refs/heads/stable.*', B),
1049 !.
1050 submit_type(T) :- gerrit:project_default_submit_type(T)
1051====
1052
1053The `gerrit:commit_delta('(?<!\.txt)$')` succeeds if the change contains a file
1054whose name doesn't end with `.txt` The rest of this rule is same like in the
1055previous example.
1056
1057If all file names in the change end with `.txt`, then the
1058`gerrit:commit_delta('(?<!\.txt)$')` will fail as no file name will match this
1059regular expression.
1060
Sasa Zivkovae50f712012-07-24 14:51:44 +02001061GERRIT
1062------
1063Part of link:index.html[Gerrit Code Review]