-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify process to generate a new install key and store install ids.
- Loading branch information
Showing
6 changed files
with
98 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,80 @@ | ||
open Base | ||
open Bot_components | ||
open Helpers | ||
open Lwt | ||
|
||
let installation_tokens : (string, string * float) Base.Hashtbl.t = | ||
let installation_ids : (string, int) Base.Hashtbl.t = | ||
Hashtbl.create (module String) | ||
|
||
let action_with_new_installation_token ~bot_info ~key ~app_id ~owner ~repo | ||
action = | ||
let installation_tokens : (int, string * float) Base.Hashtbl.t = | ||
Hashtbl.create (module Int) | ||
|
||
let action_with_new_installation_token ~bot_info ~key ~app_id ~install_id action | ||
= | ||
(* Installation tokens expire after one hour, we stop using them after 40 minutes *) | ||
GitHub_app.get_installation_token ~bot_info ~key ~app_id ~owner ~repo | ||
GitHub_app.get_installation_token ~bot_info ~key ~app_id ~install_id | ||
>>= function | ||
| Ok (install_token, expiration_date) -> | ||
let _ = | ||
Hashtbl.add installation_tokens ~key:owner | ||
Hashtbl.add installation_tokens ~key:install_id | ||
~data:(install_token, expiration_date) | ||
in | ||
let bot_info : Bot_info.t = | ||
{bot_info with github_install_token= Some install_token} | ||
in | ||
action ~bot_info | ||
| Error _ -> | ||
(* If we cannot retrieve an installation token for the repository | ||
repo owned by owner, we execute the action with the github access token. *) | ||
action ~bot_info | ||
| Error err -> | ||
failwith | ||
(f | ||
"We did not manage to get an installation token for installation \ | ||
%d: %s" | ||
install_id err ) | ||
|
||
let action_as_github_app ~bot_info ~key ~app_id ~owner ~repo action = | ||
let action_as_github_app_from_install_id ~bot_info ~key ~app_id ~install_id | ||
action = | ||
(* Executes an action with an installation token if the repository has | ||
the GitHub app installed. | ||
Generates a new installation token if the existing one has expired. *) | ||
match Hashtbl.find installation_tokens owner with | ||
match Hashtbl.find installation_tokens install_id with | ||
| Some (install_token, expiration_date) -> | ||
if Float.(expiration_date < Unix.time ()) then ( | ||
Hashtbl.remove installation_tokens owner ; | ||
action_with_new_installation_token ~bot_info ~key ~app_id ~owner ~repo | ||
Hashtbl.remove installation_tokens install_id ; | ||
action_with_new_installation_token ~bot_info ~key ~app_id ~install_id | ||
action ) | ||
else | ||
let bot_info : Bot_info.t = | ||
{bot_info with github_install_token= Some install_token} | ||
in | ||
action ~bot_info | ||
| None -> | ||
action_with_new_installation_token ~bot_info ~key ~app_id ~install_id | ||
action | ||
|
||
let action_as_github_app ~bot_info ~key ~app_id ~owner action = | ||
(* Executes an action with an installation token if the repository has | ||
the GitHub app installed. | ||
Generates a new installation token if the existing one has expired. *) | ||
match Hashtbl.find installation_ids owner with | ||
| Some install_id -> | ||
action_as_github_app_from_install_id ~bot_info ~key ~app_id ~install_id | ||
action | ||
| None -> ( | ||
GitHub_app.get_installations ~bot_info ~key ~app_id | ||
>>= function | ||
| Ok installs -> | ||
if List.exists installs ~f:(String.equal owner) then | ||
action_with_new_installation_token ~bot_info ~key ~app_id ~owner | ||
~repo action | ||
else action ~bot_info | ||
| Error _ -> | ||
action ~bot_info ) | ||
| Ok installs -> ( | ||
match | ||
installs | ||
|> List.find_map ~f:(fun (owner', install_id) -> | ||
if String.equal owner owner' then Some install_id else None ) | ||
with | ||
| Some install_id -> | ||
let _ = Hashtbl.add installation_ids ~key:owner ~data:install_id in | ||
action_as_github_app_from_install_id ~bot_info ~key ~app_id | ||
~install_id action | ||
| None -> | ||
(* If the owner does not have the GitHub app installed, | ||
we execute the action with the github access token. *) | ||
action ~bot_info ) | ||
| Error err -> | ||
failwith | ||
(f "We did not manage to get the list of installations: %s" err) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
open Bot_components | ||
|
||
val installation_tokens : (string, string * float) Base.Hashtbl.t | ||
val installation_ids : (string, int) Base.Hashtbl.t | ||
|
||
val installation_tokens : (int, string * float) Base.Hashtbl.t | ||
|
||
val action_as_github_app_from_install_id : | ||
bot_info:Bot_info.t | ||
-> key:Mirage_crypto_pk.Rsa.priv | ||
-> app_id:int | ||
-> install_id:int | ||
-> (bot_info:Bot_info.t -> 'a Lwt.t) | ||
-> 'a Lwt.t | ||
|
||
val action_as_github_app : | ||
bot_info:Bot_info.t | ||
-> key:Mirage_crypto_pk.Rsa.priv | ||
-> app_id:int | ||
-> owner:string | ||
-> repo:string | ||
-> (bot_info:Bot_info.t -> 'a Lwt.t) | ||
-> 'a Lwt.t |