Skip to content

Commit 48860ec

Browse files
committed
rule: use keyword "any" for global filter override
1 parent baa2b63 commit 48860ec

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

documentation/config_docs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ A **label rule** specifies whether or not a Slack channel should be notified, ba
110110
"backend/a5",
111111
"backend/a4"
112112
],
113-
"branch_filters": [],
113+
"branch_filters": "any",
114114
"channel": "backend"
115115
},
116116
{
@@ -132,13 +132,13 @@ A **prefix rule** specifies whether or not a Slack channel should be notified, b
132132
Default behavior is to apply each rule regardless of what branch is pushed, and when a rule is matched, show its `distinct` commits only.
133133
Branch filters limit rule application to selected branches, and shows _all_ commits on match.
134134
The filters can be declared globally with `filter_main_branch` (see above), or locally per rule with `branch_filters`, where the latter takes precedence.
135-
To ignore a globally declared filter for a single rule, declare one locally with an empty list, as shown in the example above.
135+
To ignore a globally declared filter for a single rule, declare one locally with value "any", as shown in the example above.
136136

137137
| value | description | default |
138138
|-|-|-|
139139
| `match` | if commit files have any prefix in this list, they should be routed to the channel | all prefixes matched |
140140
| `ignore` | if commit files have any prefix in this list, they shouldn't be routed to the channel (even if they have any `match` prefixes) | fall back on `match` field behavior |
141-
| `branch_filters` | consider commits only if pushed ref branch is in this list | fall back on `filter_main_branch` field behavior (see above) |
141+
| `branch_filters` | consider commits only if pushed ref branch is in this list; set to "any" to ignore `filter_main_branch` for this rule | fall back on `filter_main_branch` field behavior (see above) |
142142
| `channel` | channel to notify if the rule is matched | required field |
143143

144144
## Status Options

lib/atd_adapters.ml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module List_or_default_field = struct
2+
open Atdgen_runtime.Json_adapter
3+
4+
module type Param = sig
5+
(** the name of the field *)
6+
val field_name : string
7+
8+
(** string alias a user can use to denote default value *)
9+
val default_alias : string
10+
11+
(** Yojson representation of default value *)
12+
val default_value : Yojson.Safe.t
13+
end
14+
15+
module Make (P : Param) : S = struct
16+
open P
17+
18+
let assoc_replace l k v = List.map (fun x -> if fst x = k then k, v else x) l
19+
20+
let normalize x =
21+
match x with
22+
| `Assoc fields ->
23+
begin
24+
match List.assoc field_name fields with
25+
| `String s when String.equal s default_alias -> `Assoc (assoc_replace fields field_name default_value)
26+
| _ | (exception Not_found) -> x
27+
end
28+
| malformed -> malformed
29+
30+
let restore x =
31+
match x with
32+
| `Assoc fields ->
33+
begin
34+
match List.assoc field_name fields with
35+
| value when Yojson.Safe.equal value default_value -> `Assoc (assoc_replace fields field_name (`String "any"))
36+
| _ | (exception Not_found) -> x
37+
end
38+
| malformed -> malformed
39+
end
40+
end
41+
42+
module Branch_filters_adapter = List_or_default_field.Make (struct
43+
let field_name = "branch_filters"
44+
45+
let default_alias = "any"
46+
47+
let default_value = `List []
48+
end)

lib/rule.atd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type prefix_rule = {
6868
?ignore : string list nullable;
6969
?branch_filters : string list nullable;
7070
channel_name <json name="channel"> : string;
71-
}
71+
} <json adapter.ocaml="Atd_adapters.Branch_filters_adapter">
7272

7373
(* A payload matches a label rule with a channel name if absent from the ignore list
7474
and present in the allow list. Both `allow` and `ignore` are optional. If `allow`

test/monorobot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"backend/branch-filter1"
4747
],
4848
"channel": "backend1",
49-
"branch_filters": []
49+
"branch_filters": "any"
5050
},
5151
{
5252
"match": [

0 commit comments

Comments
 (0)