Skip to content

Commit 0415c17

Browse files
committed
Always extract the installation id and use it when we know it's available.
1 parent ad0c361 commit 0415c17

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

bot-components/GitHub_subscriptions.ml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,30 @@ let github_event ~event json =
227227
Ok (UnsupportedEvent "Unsupported GitHub event.")
228228

229229
let receive_github ~secret headers body =
230-
let open Result in
231-
( match Header.get headers "X-Hub-Signature" with
232-
| Some signature ->
233-
let expected =
234-
Mirage_crypto.Hash.SHA1.hmac ~key:(Cstruct.of_string secret)
235-
(Cstruct.of_string body)
236-
|> Hex.of_cstruct |> Hex.show |> f "sha1=%s"
237-
in
238-
if Eqaf.equal signature expected then return true
239-
else Error "Webhook signed but with wrong signature."
240-
| None ->
241-
return false )
242-
>>= fun signed ->
230+
let open Result.Monad_infix in
243231
match Header.get headers "X-GitHub-Event" with
244232
| Some event -> (
245233
try
246234
let json = Yojson.Basic.from_string body in
247-
github_event ~event json |> Result.map ~f:(fun r -> (signed, r))
235+
( try
236+
let install_id =
237+
json |> member "installation" |> member "id" |> to_int
238+
in
239+
(* if there is an install id, the webhook should be signed *)
240+
match Header.get headers "X-Hub-Signature" with
241+
| Some signature ->
242+
let expected =
243+
Mirage_crypto.Hash.SHA1.hmac ~key:(Cstruct.of_string secret)
244+
(Cstruct.of_string body)
245+
|> Hex.of_cstruct |> Hex.show |> f "sha1=%s"
246+
in
247+
if Eqaf.equal signature expected then Ok (Some install_id)
248+
else Error "Webhook signed but with wrong signature."
249+
| None ->
250+
Error "Webhook comes from a GitHub App, but it is not signed."
251+
with Yojson.Json_error _ | Type_error _ -> Ok None )
252+
>>= fun install_id ->
253+
github_event ~event json |> Result.map ~f:(fun r -> (install_id, r))
248254
with
249255
| Yojson.Json_error err ->
250256
Error (f "Json error: %s" err)

bot-components/GitHub_subscriptions.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ type msg =
1717
| UnsupportedEvent of string
1818

1919
val receive_github :
20-
secret:string -> Cohttp.Header.t -> string -> (bool * msg, string) result
20+
secret:string
21+
-> Cohttp.Header.t
22+
-> string
23+
-> (int option * msg, string) result

src/bot.ml

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,17 @@ let callback _conn req body =
199199
(Request.headers req) body
200200
with
201201
| Ok
202-
( true
202+
( Some install_id
203203
, PushEvent
204204
{owner= "coq"; repo= "coq"; base_ref; head_sha; commits_msg} ) ->
205205
(fun () ->
206206
init_git_bare_repository ~bot_info
207207
>>= fun () ->
208-
action_as_github_app ~bot_info ~key ~app_id ~owner:"coq"
208+
action_as_github_app_from_install_id ~bot_info ~key ~app_id
209+
~install_id
209210
(coq_push_action ~base_ref ~commits_msg)
210-
<&> action_as_github_app ~bot_info ~key ~app_id ~owner:"coq"
211+
<&> action_as_github_app_from_install_id ~bot_info ~key ~app_id
212+
~install_id
211213
(mirror_action ~gitlab_domain:"gitlab.inria.fr" ~owner:"coq"
212214
~repo:"coq" ~base_ref ~head_sha () ) )
213215
|> Lwt.async ;
@@ -216,13 +218,15 @@ let callback _conn req body =
216218
"Processing push event on Coq repository: analyzing merge / \
217219
backporting info."
218220
()
219-
| Ok (true, PushEvent {owner; repo; base_ref; head_sha; _}) -> (
221+
| Ok (Some install_id, PushEvent {owner; repo; base_ref; head_sha; _})
222+
-> (
220223
match (owner, repo) with
221224
| "coq-community", ("docker-base" | "docker-coq") ->
222225
(fun () ->
223226
init_git_bare_repository ~bot_info
224227
>>= fun () ->
225-
action_as_github_app ~bot_info ~key ~app_id ~owner
228+
action_as_github_app_from_install_id ~bot_info ~key ~app_id
229+
~install_id
226230
(mirror_action ~gitlab_domain:"gitlab.com" ~owner ~repo
227231
~base_ref ~head_sha () ) )
228232
|> Lwt.async ;
@@ -237,7 +241,8 @@ let callback _conn req body =
237241
(fun () ->
238242
init_git_bare_repository ~bot_info
239243
>>= fun () ->
240-
action_as_github_app ~bot_info ~key ~app_id ~owner
244+
action_as_github_app_from_install_id ~bot_info ~key ~app_id
245+
~install_id
241246
(mirror_action ~gitlab_domain:"gitlab.inria.fr" ~owner ~repo
242247
~base_ref ~head_sha () ) )
243248
|> Lwt.async ;
@@ -333,7 +338,7 @@ let callback _conn req body =
333338
Server.respond_string ~status:`OK
334339
~body:(f "Unhandled new issue: %s" body)
335340
() )
336-
| Ok (signed, CommentCreated comment_info) -> (
341+
| Ok (install_id, CommentCreated comment_info) -> (
337342
let body =
338343
comment_info.body |> trim_comments
339344
|> strip_quoted_bot_name ~github_bot_name
@@ -394,7 +399,7 @@ let callback _conn req body =
394399
&& comment_info.issue.pull_request
395400
&& String.equal comment_info.issue.issue.owner "coq"
396401
&& String.equal comment_info.issue.issue.repo "coq"
397-
&& signed
402+
&& Option.is_some install_id
398403
then
399404
let full_ci =
400405
match Str.matched_group 1 body with
@@ -421,7 +426,7 @@ let callback _conn req body =
421426
&& comment_info.issue.pull_request
422427
&& String.equal comment_info.issue.issue.owner "coq"
423428
&& String.equal comment_info.issue.issue.repo "coq"
424-
&& signed
429+
&& Option.is_some install_id
425430
then (
426431
(fun () ->
427432
action_as_github_app ~bot_info ~key ~app_id
@@ -439,7 +444,7 @@ let callback _conn req body =
439444
&& comment_info.issue.pull_request
440445
&& String.equal comment_info.issue.issue.owner "coq"
441446
&& String.equal comment_info.issue.issue.repo "coq"
442-
&& signed
447+
&& Option.is_some install_id
443448
then (
444449
(fun () ->
445450
action_as_github_app ~bot_info ~key ~app_id
@@ -458,7 +463,7 @@ let callback _conn req body =
458463
&& comment_info.issue.pull_request
459464
&& String.equal comment_info.issue.issue.owner "coq"
460465
&& String.equal comment_info.issue.issue.repo "coq"
461-
&& signed
466+
&& Option.is_some install_id
462467
then (
463468
(fun () ->
464469
action_as_github_app ~bot_info ~key ~app_id
@@ -472,11 +477,11 @@ let callback _conn req body =
472477
Server.respond_string ~status:`OK
473478
~body:(f "Unhandled comment: %s" body)
474479
() ) ) )
475-
| Ok (signed, CheckRunReRequested {external_id}) -> (
476-
if not signed then
477-
Server.respond_string ~status:(Code.status_of_code 401)
478-
~body:"Request to rerun check run must be signed." ()
479-
else if String.is_empty external_id then
480+
| Ok (None, CheckRunReRequested _) ->
481+
Server.respond_string ~status:(Code.status_of_code 401)
482+
~body:"Request to rerun check run must be signed." ()
483+
| Ok (Some _, CheckRunReRequested {external_id}) -> (
484+
if String.is_empty external_id then
480485
Server.respond_string ~status:(Code.status_of_code 400)
481486
~body:"Request to rerun check run but empty external ID." ()
482487
else

0 commit comments

Comments
 (0)