Skip to content

Commit 982ab69

Browse files
committed
preserve results interface after switching to synchronous file I/O
1 parent 44b006e commit 982ab69

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

lib/api_local.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ 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-
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
12+
match 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 file -> Lwt.return @@ Ok (Config_j.config_of_string file)
1415

1516
let get_api_commit ~ctx:_ ~repo:_ ~sha =
1617
let url = Caml.Filename.concat cache_dir sha in
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
18+
match 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 file -> Lwt.return @@ Ok (Github_j.api_commit_of_string file)
2021
end
2122

2223
module Slack : Api.Slack = struct

lib/common.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ 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 = Std.input_file path
45+
let get_local_file path = try Ok (Std.input_file path) with exn -> fmt_error "%s" (Exn.to_string exn)
4646

47-
let write_to_local_file ~data path = Devkit.Files.save_as path (fun oc -> Stdio.Out_channel.fprintf oc "%s" data)
47+
let write_to_local_file ~data path =
48+
try Ok (Devkit.Files.save_as path (fun oc -> Stdio.Out_channel.fprintf oc "%s" data))
49+
with exn -> fmt_error "%s" (Exn.to_string exn)

lib/context.ml

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

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

6162
let refresh_state ctx =
6263
match ctx.state_filepath with
6364
| None -> Ok ctx
6465
| Some path ->
65-
try
6666
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
67+
( match get_local_file path with
68+
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
69+
| Ok file ->
70+
let state = State_j.state_of_string file in
71+
Ok { ctx with state }
72+
)

src/notabot.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ let check_gh_action file json config secrets state =
2525
| None ->
2626
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file
2727
| Some kind ->
28+
match Common.get_local_file file with
29+
| Error e -> log#error "%s" e
30+
| Ok body ->
2831
let headers = [ "x-github-event", kind ] in
29-
let body = Common.get_local_file file in
3032
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
3133
Lwt_main.run
3234
( if json then

test/test.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ 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-
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 ->
27+
match Common.get_local_file state_path with
28+
| Error e ->
3129
log#error "failed to read %s: %s" state_path e;
3230
Lwt.return ctx
31+
| Ok file ->
32+
let state = State_j.state_of_string file in
33+
Lwt.return { ctx with state }
3334
in
3435
Stdio.printf "===== file %s =====\n" path;
3536
let headers = [ "x-github-event", kind ] in
36-
try
37-
let event = Common.get_local_file path in
37+
match Common.get_local_file path with
38+
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
39+
| Ok event ->
3840
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
3941
Lwt.return_unit
40-
with Sys_error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
4142

4243
let () =
4344
let payloads = get_mock_payloads () in

0 commit comments

Comments
 (0)