Skip to content

Commit d9bdfc2

Browse files
committed
github: use re2
1 parent 6cce70e commit d9bdfc2

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

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 re 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

0 commit comments

Comments
 (0)