@@ -86,3 +86,50 @@ let parse_exn ~secret headers body =
86
86
| "commit_comment" -> Commit_comment (commit_comment_notification_of_string body)
87
87
| "member" | "create" | "delete" | "release" -> Event (event_notification_of_string body)
88
88
| event -> failwith @@ sprintf " unsupported event : %s" event
89
+
90
+ type gh_link = Commit of repository * commit_hash
91
+
92
+ (* * `gh_link_of_string s` parses a URL string `s` to try to match a supported
93
+ GitHub link type, generating repository endpoints if necessary *)
94
+ let gh_link_of_string url_str =
95
+ let url = Uri. of_string url_str in
96
+ let path = Uri. path url in
97
+ let gh_com_html_base owner name = sprintf " https://github.com/%s/%s" owner name in
98
+ let gh_com_api_base owner name = sprintf " https://api.github.com/repos/%s/%s" owner name in
99
+ let custom_html_base ?(scheme = " https" ) base owner name = sprintf " %s://%s/%s/%s" scheme base owner name in
100
+ let custom_api_base ?(scheme = " https" ) base owner name =
101
+ sprintf " %s://%s/api/v3/repos/%s/%s" scheme base owner name
102
+ in
103
+ let re = Re.Str. regexp {|^ \(.* \)/ \(.+ \)/ \(.+ \)/ \(commit\)/ \([a- z0-9 ]+ \)/?$| } in
104
+ match Uri. host url with
105
+ | None -> None
106
+ | 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
115
+ let scheme = Uri. scheme url in
116
+ let html_base, api_base =
117
+ if String. is_suffix base ~suffix: " github.com" then gh_com_html_base owner name, gh_com_api_base owner name
118
+ else custom_html_base ?scheme base owner name, custom_api_base ?scheme base owner name
119
+ in
120
+ let repo =
121
+ {
122
+ name;
123
+ full_name = sprintf " %s/%s" owner name;
124
+ url = html_base;
125
+ commits_url = sprintf " %s/commits{/sha}" api_base;
126
+ contents_url = sprintf " %s/contents/{+path}" api_base;
127
+ }
128
+ in
129
+ begin
130
+ try
131
+ match link_type with
132
+ | "commit" -> Some (Commit (repo, item))
133
+ | _ -> None
134
+ with _ -> None
135
+ end
0 commit comments