Skip to content

Commit 4b77278

Browse files
committed
rule: add branch_filters prefix rule
1 parent df93ce4 commit 4b77278

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

lib/action.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
1515
let partition_push (cfg : Config_t.config) n =
1616
let default = Option.to_list cfg.prefix_rules.default_channel in
1717
let rules = cfg.prefix_rules.rules in
18+
let branch = Github.commits_branch_of_ref n.ref in
1819
n.commits
19-
|> List.filter ~f:(fun c -> c.distinct)
2020
|> List.filter ~f:(fun c ->
21-
let branch = Github.commits_branch_of_ref n.ref in
2221
let skip = Github.is_main_merge_message ~msg:c.message ~branch cfg in
2322
if skip then log#info "main branch merge, ignoring %s: %s" c.id (first_line c.message);
2423
not skip)
2524
|> List.concat_map ~f:(fun commit ->
25+
let rules = List.filter ~f:(Rule.Prefix.filter_by_branch ~branch ~distinct:commit.distinct) rules in
2626
let matched_channel_names =
2727
Github.modified_files_of_commit commit
2828
|> List.filter_map ~f:(Rule.Prefix.match_rules ~rules)
2929
|> List.dedup_and_sort ~compare:String.compare
3030
in
31-
let channel_names = if List.is_empty matched_channel_names then default else matched_channel_names in
31+
let channel_names =
32+
if List.is_empty matched_channel_names && commit.distinct then default else matched_channel_names
33+
in
3234
List.map channel_names ~f:(fun n -> n, commit))
3335
|> Map.of_alist_multi (module String)
3436
|> Map.map ~f:(fun commits -> { n with commits })

lib/rule.atd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type status_rule = {
6666
type prefix_rule = {
6767
?allow <json name="match"> : string list nullable;
6868
?ignore : string list nullable;
69+
~branch_filters <ocaml default="[]"> : string list;
6970
channel_name <json name="channel"> : string;
7071
}
7172

lib/rule.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ 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
47+
4248
(** `match_rules f rs` returns the channel name of a rule in `rs` that matches
4349
file name `f` with the longest prefix, if one exists. A rule `r` matches
4450
`f` with prefix length `l`, if `f` has no prefix in `r.ignore` and `l` is

0 commit comments

Comments
 (0)