Skip to content

Commit 44b006e

Browse files
committed
use synchronous I/O for local file manipulation for simplicity
1 parent d28c3a4 commit 44b006e

File tree

5 files changed

+49
-84
lines changed

5 files changed

+49
-84
lines changed

lib/api_local.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ let cache_dir = Caml.Filename.concat cwd "github-api-cache"
99
module Github : Api.Github = struct
1010
let get_config ~(ctx : Context.t) ~repo:_ =
1111
let url = Caml.Filename.concat cwd ctx.config_filename in
12-
match%lwt get_local_file url with
13-
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url
14-
| Ok res -> Lwt.return @@ Ok (Config_j.config_of_string res)
12+
try Lwt.return @@ Ok (url |> get_local_file |> Config_j.config_of_string)
13+
with Sys_error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get config %s" e url
1514

1615
let get_api_commit ~ctx:_ ~repo:_ ~sha =
1716
let url = Caml.Filename.concat cache_dir sha in
18-
match%lwt get_local_file url with
19-
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get api commit %s" e url
20-
| Ok res -> Lwt.return @@ Ok (Github_j.api_commit_of_string res)
17+
try Lwt.return @@ Ok (url |> get_local_file |> Github_j.api_commit_of_string)
18+
with Sys_error e ->
19+
Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get api commit %s" e url
2120
end
2221

2322
module Slack : Api.Slack = struct

lib/common.ml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ let http_request ?headers ?body meth path =
4242
| `Ok s -> Lwt.return @@ Ok s
4343
| `Error e -> Lwt.return @@ Error e
4444

45-
let get_local_file path =
46-
try%lwt
47-
let%lwt data = Lwt_io.with_file ~mode:Lwt_io.input path (fun ic -> Lwt_io.read ic) in
48-
Lwt.return @@ Ok data
49-
with exn -> Lwt.return @@ Error (Exn.str exn)
50-
51-
let write_to_local_file ~data path =
52-
try%lwt
53-
let%lwt () =
54-
Lwt_io.with_file ~flags:[ O_CREAT; O_WRONLY; O_TRUNC ] ~mode:Lwt_io.output path (fun oc -> Lwt_io.write oc data)
55-
in
56-
Lwt.return @@ Ok ()
57-
with exn -> Lwt.return @@ Error (Exn.str exn)
45+
let get_local_file path = Std.input_file path
46+
47+
let write_to_local_file ~data path = Devkit.Files.save_as path (fun oc -> Stdio.Out_channel.fprintf oc "%s" data)

lib/context.ml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,17 @@ let log = Log.from "context"
5353

5454
let refresh_secrets ctx =
5555
let path = ctx.secrets_filepath in
56-
match%lwt get_local_file path with
57-
| Ok res ->
58-
ctx.secrets <- Some (Config_j.secrets_of_string res);
59-
Lwt.return @@ Ok ctx
60-
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get secrets from file %s" e path
56+
try
57+
ctx.secrets <- Some (path |> get_local_file |> Config_j.secrets_of_string);
58+
Ok ctx
59+
with Sys_error e -> fmt_error "error while getting local file: %s\nfailed to get secrets from file %s" e path
6160

6261
let refresh_state ctx =
6362
match ctx.state_filepath with
64-
| None -> Lwt.return @@ Ok ctx
63+
| None -> Ok ctx
6564
| Some path ->
66-
( match%lwt get_local_file path with
67-
| Ok res ->
68-
log#info "loading saved state from file %s" path;
69-
let state = State_j.state_of_string res in
70-
Lwt.return @@ Ok { ctx with state }
71-
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
72-
)
65+
try
66+
log#info "loading saved state from file %s" path;
67+
let state = path |> get_local_file |> State_j.state_of_string in
68+
Ok { ctx with state }
69+
with Sys_error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path

src/notabot.ml

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,31 @@ let log = Log.from "monorobot"
1111
let http_server_action addr port config secrets state =
1212
log#info "monorobot starting";
1313
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
14-
Lwt_main.run
15-
( match%lwt Context.refresh_secrets ctx with
16-
| Error e ->
17-
log#error "%s" e;
18-
Lwt.return_unit
19-
| Ok ctx ->
20-
( match%lwt Context.refresh_state ctx with
21-
| Error e ->
22-
log#error "%s" e;
23-
Lwt.return_unit
24-
| Ok ctx -> Request_handler.run ~ctx ~addr ~port
25-
)
26-
)
14+
match Context.refresh_secrets ctx with
15+
| Error e -> log#error "%s" e
16+
| Ok ctx ->
17+
match Context.refresh_state ctx with
18+
| Error e -> log#error "%s" e
19+
| Ok ctx -> Lwt_main.run (Request_handler.run ~ctx ~addr ~port)
2720

2821
(** In check mode, instead of actually sending the message to slack, we
2922
simply print it in the console *)
3023
let check_gh_action file json config secrets state =
31-
Lwt_main.run
32-
begin
33-
match Github.event_of_filename file with
34-
| None ->
35-
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file;
36-
Lwt.return_unit
37-
| Some kind ->
38-
let headers = [ "x-github-event", kind ] in
39-
( match%lwt Common.get_local_file file with
40-
| Error e ->
41-
log#error "%s" e;
42-
Lwt.return_unit
43-
| Ok body ->
44-
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
45-
let%lwt () =
46-
if json then
47-
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
48-
Action.process_github_notification ctx headers body
49-
else
50-
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
51-
Action.process_github_notification ctx headers body
52-
in
53-
Lwt.return_unit
54-
)
55-
end
24+
match Github.event_of_filename file with
25+
| None ->
26+
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file
27+
| Some kind ->
28+
let headers = [ "x-github-event", kind ] in
29+
let body = Common.get_local_file file in
30+
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
31+
Lwt_main.run
32+
( if json then
33+
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_json) in
34+
Action.process_github_notification ctx headers body
35+
else
36+
let module Action = Action.Action (Api_remote.Github) (Api_local.Slack_simple) in
37+
Action.process_github_notification ctx headers body
38+
)
5639

5740
let check_slack_action url file =
5841
let data = Stdio.In_channel.read_all file in

test/test.ml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,20 @@ let process ~(ctx : Context.t) (kind, path, state_path) =
2424
match state_path with
2525
| None -> Lwt.return { ctx with state = State.empty }
2626
| Some state_path ->
27-
( match%lwt Common.get_local_file state_path with
28-
| Error e ->
29-
log#error "failed to read %s: %s" state_path e;
30-
Lwt.return ctx
31-
| Ok file ->
32-
let state = State_j.state_of_string file in
33-
Lwt.return { ctx with state }
34-
)
27+
try
28+
let state = state_path |> Common.get_local_file |> State_j.state_of_string in
29+
Lwt.return { ctx with state }
30+
with Sys_error e ->
31+
log#error "failed to read %s: %s" state_path e;
32+
Lwt.return ctx
3533
in
3634
Stdio.printf "===== file %s =====\n" path;
3735
let headers = [ "x-github-event", kind ] in
38-
match%lwt Common.get_local_file path with
39-
| Error e ->
40-
log#error "failed to read %s: %s" path e;
41-
Lwt.return_unit
42-
| Ok event ->
36+
try
37+
let event = Common.get_local_file path in
4338
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
4439
Lwt.return_unit
40+
with Sys_error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4541

4642
let () =
4743
let payloads = get_mock_payloads () in
@@ -56,7 +52,7 @@ let () =
5652
(* can remove this wrapper once status_rules doesn't depend on Config.t *)
5753
let config = Config.make config in
5854
let ctx = { ctx with config = Some config } in
59-
( match%lwt Context.refresh_secrets ctx with
55+
( match Context.refresh_secrets ctx with
6056
| Ok ctx -> Lwt_list.iter_s (process ~ctx) payloads
6157
| Error e ->
6258
log#error "failed to read secrets:";

0 commit comments

Comments
 (0)