Skip to content

Commit 32306f4

Browse files
committed
Merge branch 'yasu/config-re2'
* yasu/config-re2: opam: - re test: update config regex to re2 config: wrap match condition regex as re2 github: use re2 opam: add re2
2 parents 9bf138c + 2c1c785 commit 32306f4

File tree

7 files changed

+29
-20
lines changed

7 files changed

+29
-20
lines changed

lib/common.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ module StringMap = struct
1515
let unwrap = to_list
1616
end
1717

18+
module Re2 = struct
19+
include Re2
20+
21+
let wrap s = create_exn s
22+
23+
let unwrap = Re2.to_string
24+
end
25+
1826
let fmt_error fmt = Printf.ksprintf (fun s -> Error s) fmt
1927

2028
let first_line s =

lib/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(library
22
(name lib)
33
(libraries atdgen atdgen-runtime base base64 base.caml biniou cstruct curl curl.lwt
4-
devkit devkit.core extlib hex lwt lwt.unix nocrypto omd re stdio uri
4+
devkit devkit.core extlib hex lwt lwt.unix nocrypto omd re2 stdio uri
55
yojson)
66
(preprocess
77
(pps lwt_ppx)))

lib/github.ml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ let is_main_merge_message ~msg:message ~branch (cfg : Config_t.config) =
4444
we should have already seen these commits on the feature branch but for some reason they are distinct:true
4545
*)
4646
let re =
47-
Str.regexp (sprintf {|^Merge\( remote-tracking\)? branch '\(origin/\)?%s'\( of .+\)? into \(.+\)$|} main_branch)
47+
Re2.create_exn
48+
(sprintf {|^Merge(?: remote-tracking)? branch '(?:origin/)?%s'(?: of .+)? into (.+)$|} (Re2.escape main_branch))
4849
in
4950
let title = Common.first_line message in
50-
let matched = Str.string_match re title 0 in
51-
matched && (String.equal branch main_branch || String.equal branch (Str.matched_group 4 title))
51+
begin
52+
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
55+
with Re2.Exceptions.Regex_match_failed _ -> false
56+
end
5257
| _ -> false
5358

5459
let modified_files_of_commit commit = List.concat [ commit.added; commit.removed; commit.modified ]
@@ -89,6 +94,8 @@ type gh_link =
8994
| Issue of repository * int
9095
| Commit of repository * commit_hash
9196

97+
let gh_re = Re2.create_exn {|^(.*)/(.+)/(.+)/(commit|pull|issues)/([a-z0-9]+)/?$|}
98+
9299
(** `gh_link_of_string s` parses a URL string `s` to try to match a supported
93100
GitHub link type, generating repository endpoints if necessary *)
94101
let gh_link_of_string url_str =
@@ -100,18 +107,12 @@ let gh_link_of_string url_str =
100107
let custom_api_base ?(scheme = "https") base owner name =
101108
sprintf "%s://%s/api/v3/repos/%s/%s" scheme base owner name
102109
in
103-
let re = Re.Str.regexp {|^\(.*\)/\(.+\)/\(.+\)/\(commit\|pull\|issues\)/\([a-z0-9]+\)/?$|} in
104110
match Uri.host url with
105111
| None -> None
106112
| Some host ->
107-
match Re.Str.string_match re path 0 with
108-
| false -> None
109-
| true ->
110-
let base = host ^ Re.Str.matched_group 1 path in
111-
let owner = Re.Str.matched_group 2 path in
112-
let name = Re.Str.matched_group 3 path in
113-
let link_type = Re.Str.matched_group 4 path in
114-
let item = Re.Str.matched_group 5 path in
113+
match Re2.find_submatches_exn gh_re path with
114+
| [| _; prefix; Some owner; Some name; Some link_type; Some item |] ->
115+
let base = Option.value_map prefix ~default:host ~f:(fun p -> String.concat [ host; p ]) in
115116
let scheme = Uri.scheme url in
116117
let html_base, api_base =
117118
if String.is_suffix base ~suffix:"github.com" then gh_com_html_base owner name, gh_com_api_base owner name
@@ -137,3 +138,4 @@ let gh_link_of_string url_str =
137138
| _ -> None
138139
with _ -> None
139140
end
141+
| _ | (exception Re2.Exceptions.Regex_match_failed _) -> None

lib/rule.atd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type regex = string wrap <ocaml module="Common.Re2">
2+
13
(* Text fields from the GitHub payload that can be used in a condition *)
24
type comparable_field = [
35
| Context <json name="context">
@@ -8,7 +10,7 @@ type comparable_field = [
810
(* Checks whether a payload field matches the provided regex pattern *)
911
type match_condition = {
1012
field : comparable_field;
11-
re : string; (* expects a regex pattern *)
13+
re : regex;
1214
}
1315

1416
(* Specifies additional conditions the payload must meet for the status rule to match *)

lib/rule.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ module Status = struct
2121
| Target_url -> notification.target_url
2222
in
2323
let rec match_condition = function
24-
| Match { field; re } ->
25-
value_of_field field
26-
|> Option.map ~f:(fun f -> Re.Str.string_match (Re.Str.regexp_case_fold re) f 0)
27-
|> Option.value ~default:false
24+
| Match { field; re } -> value_of_field field |> Option.map ~f:(Re2.matches re) |> Option.value ~default:false
2825
| All_of conditions -> List.for_all conditions ~f:match_condition
2926
| One_of conditions -> List.exists conditions ~f:match_condition
3027
| Not condition -> not @@ match_condition condition

monorobot.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ depends: [
2222
"lwt"
2323
"lwt_ppx"
2424
"nocrypto"
25-
"re"
25+
"re2"
2626
"stdio"
2727
"uri"
2828
"ocamlformat" {dev}

test/monorobot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"when": {
1212
"match": {
1313
"field": "description",
14-
"re": "^\\(Build #[0-9]+ canceled by .+\\|Failed (exit status 255)\\)$"
14+
"re": "^(Build #[0-9]+ canceled by .+|Failed \\(exit status 255\\))$"
1515
}
1616
},
1717
"policy": "ignore"

0 commit comments

Comments
 (0)