Skip to content

Commit 3126384

Browse files
committed
make status rule handling fall back on sensible defaults if no match
1 parent d473033 commit 3126384

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

documentation/config_docs.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ Monorobot supports additional behavior for GitHub status notifications, which ar
136136

137137
The following takes place when a status notification is received.
138138

139-
1. Check whether a status notification should be *allowed* for further processing or *ignored*, according to a list of **status rules**. The bot will check the list *in order*, and use the policy defined by the first rule that the notification satisfies. If no rule matches, the default behavior is to allow the notification.
139+
1. Check whether a status notification should be *allowed* for further processing or *ignored*, according to a list of **status rules**. The bot will check the list *in order*, and use the policy defined by the first rule that the notification satisfies. If no rule matches, the default behavior of the status state is used:
140+
- `pending`: `ignore`
141+
- `failure`: `allow`
142+
- `error`: `allow`
143+
- `success`: `allow_once`
140144
1. For those payloads allowed by step 1, if it isn't a main branch build notification, route to the default channel to reduce spam in topic channels. Otherwise, check the notification commit's files according to the prefix rules.
141145

142146
Internally, the bot keeps track of the status of the last allowed payload, for a given pipeline and branch. This information is used to evaluate the status rules (see below).

lib/rule.atd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ type status_policy = [
3030
type build_status <ocaml from="Github" t="status_state"> = abstract
3131

3232
(* A status matches a status rule with the policy with the build status is in
33-
the list, and the condition is true *)
33+
the list, and the condition is true.
34+
35+
The default behavior for each status state is:
36+
- pending: ignore
37+
- failure: allow
38+
- error: allow
39+
- success: allow_once
40+
*)
3441
type status_rule = {
3542
trigger <json name="on"> : build_status list;
3643
?condition <json name="when"> : status_condition nullable;

lib/rule.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ open Base
22
open Rule_t
33

44
module Status = struct
5+
let default_rules =
6+
[
7+
{ trigger = [ Pending ]; condition = None; policy = Ignore };
8+
{ trigger = [ Failure; Error ]; condition = None; policy = Allow };
9+
{ trigger = [ Success ]; condition = None; policy = Allow_once };
10+
]
11+
512
(** `match_rules n rs` returns the policy declared by the first rule in `rs`
6-
to match status notification `n`, if one exists. A rule `r` matches `n`
7-
if `n.state` is in `r.trigger` and `n` meets `r.condition`. *)
13+
to match status notification `n` if one exists, falling back to
14+
the default rules otherwise. A rule `r` matches `n` if `n.state` is in
15+
`r.trigger` and `n` meets `r.condition`. *)
816
let match_rules (notification : Github_t.status_notification) ~rules =
917
let match_rule rule =
1018
let value_of_field = function
@@ -27,7 +35,7 @@ module Status = struct
2735
then Some rule.policy
2836
else None
2937
in
30-
List.find_map rules ~f:match_rule
38+
List.find_map (List.append rules default_rules) ~f:match_rule
3139
end
3240

3341
module Prefix = struct

test/notabot.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
}
1616
},
1717
"policy": "ignore"
18-
},
19-
{ "on": ["pending"], "policy": "ignore"},
20-
{ "on": ["failure", "error"], "policy": "allow"},
21-
{ "on": ["success"], "policy": "allow_once"}
18+
}
2219
]
2320
},
2421
"prefix_rules": {

0 commit comments

Comments
 (0)