Skip to content

Commit 065e04b

Browse files
committed
github: if main branch push, ignore merge unless main merge
If the main branch is pushed, ignore all merge commits except those that merge into the main branch. Previously, it disallowed main -> feature merges (desirable), but allowed feature -> feature merges (undesirable). https://ahrefs.slack.com/archives/CKZANG2TE/p1635736108002200
1 parent 32306f4 commit 065e04b

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

lib/action.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
2020
let filter_by_branch = Rule.Prefix.filter_by_branch ~branch ~main_branch in
2121
n.commits
2222
|> List.filter ~f:(fun c ->
23-
let skip = Github.is_main_merge_message ~msg:c.message ~branch cfg in
23+
let skip = Github.is_merge_commit_to_ignore ~cfg ~branch c in
2424
if skip then log#info "main branch merge, ignoring %s: %s" c.id (first_line c.message);
2525
not skip
2626
)

lib/github.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ let event_of_filename filename =
3636
| [ kind; _; "json" ] -> Some kind
3737
| _ -> None
3838

39-
let is_main_merge_message ~msg:message ~branch (cfg : Config_t.config) =
39+
let merge_commit_re = Re2.create_exn {|^Merge(?: remote-tracking)? branch '(?:origin/)?.+'(?: of .+)? into (.+)$|}
40+
41+
let is_merge_commit_to_ignore ~(cfg : Config_t.config) ~branch commit =
4042
match cfg.main_branch_name with
41-
| Some main_branch ->
43+
| Some main_branch when String.equal branch main_branch ->
4244
(*
43-
handle "Merge <main branch> into <feature branch>" commits when they are merged into main branch
45+
handle "Merge <any branch> into <feature branch>" commits when they are merged into main branch
4446
we should have already seen these commits on the feature branch but for some reason they are distinct:true
4547
*)
46-
let re =
47-
Re2.create_exn
48-
(sprintf {|^Merge(?: remote-tracking)? branch '(?:origin/)?%s'(?: of .+)? into (.+)$|} (Re2.escape main_branch))
49-
in
50-
let title = Common.first_line message in
48+
let title = Common.first_line commit.message in
5149
begin
5250
try
53-
let other_branch = Re2.find_first_exn ~sub:(`Index 1) re title in
54-
String.equal branch main_branch || String.equal branch other_branch
51+
let receiving_branch = Re2.find_first_exn ~sub:(`Index 1) merge_commit_re title in
52+
not @@ String.equal branch receiving_branch
5553
with Re2.Exceptions.Regex_match_failed _ -> false
5654
end
57-
| _ -> false
55+
| Some _ | None -> false
5856

5957
let modified_files_of_commit commit = List.concat [ commit.added; commit.removed; commit.modified ]
6058

0 commit comments

Comments
 (0)