Skip to content

Commit 963df20

Browse files
committed
rule: add global default_branch_filters
If prefix rule has local `branch_filters` defined, use that. Otherwise, use `default_branch_filters` (if defined).
1 parent 4b77278 commit 963df20

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

lib/action.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
1616
let default = Option.to_list cfg.prefix_rules.default_channel in
1717
let rules = cfg.prefix_rules.rules in
1818
let branch = Github.commits_branch_of_ref n.ref in
19+
let default_branch_filters = cfg.prefix_rules.default_branch_filters in
20+
let filter_by_branch = Rule.Prefix.filter_by_branch ~branch ~default_branch_filters in
1921
n.commits
2022
|> List.filter ~f:(fun c ->
2123
let skip = Github.is_main_merge_message ~msg:c.message ~branch cfg in
2224
if skip then log#info "main branch merge, ignoring %s: %s" c.id (first_line c.message);
2325
not skip)
2426
|> List.concat_map ~f:(fun commit ->
25-
let rules = List.filter ~f:(Rule.Prefix.filter_by_branch ~branch ~distinct:commit.distinct) rules in
27+
let rules = List.filter ~f:(filter_by_branch ~distinct:commit.distinct) rules in
2628
let matched_channel_names =
2729
Github.modified_files_of_commit commit
2830
|> List.filter_map ~f:(Rule.Prefix.match_rules ~rules)

lib/config.atd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type status_rules = {
1212
files they are related to. *)
1313
type prefix_rules = {
1414
?default_channel: string nullable; (* if none of the rules is matching *)
15+
~default_branch_filters <ocaml default="[]">: string list;
1516
rules: prefix_rule list;
1617
}
1718

lib/rule.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ module Status = struct
3939
end
4040

4141
module Prefix = struct
42-
(** Filters prefix rules based on config and current commit. Returns true if
43-
`branch_filters` includes the commit branch, or if it's empty but the
44-
commit is `distinct`. *)
45-
let filter_by_branch ~branch ~distinct rule =
46-
(List.is_empty rule.branch_filters && distinct) || List.mem rule.branch_filters branch ~equal:String.equal
42+
(** Filters prefix rules based on branch filtering config and current commit.
43+
Prioritizes local filters over default ones. Only allows distinct commits
44+
if no filter is matched. *)
45+
let filter_by_branch ~branch ~default_branch_filters ~distinct rule =
46+
match rule.branch_filters with
47+
| _ :: _ as filters -> List.mem filters branch ~equal:String.equal
48+
| [] ->
49+
match default_branch_filters with
50+
| _ :: _ as filters -> List.mem filters branch ~equal:String.equal
51+
| [] -> distinct
4752

4853
(** `match_rules f rs` returns the channel name of a rule in `rs` that matches
4954
file name `f` with the longest prefix, if one exists. A rule `r` matches

0 commit comments

Comments
 (0)