Skip to content

Commit 041fc71

Browse files
committed
don't wrap file io
1 parent ab6945d commit 041fc71

File tree

6 files changed

+56
-64
lines changed

6 files changed

+56
-64
lines changed

lib/api_local.ml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ let slack_cache_dir = Filename.concat cwd "slack-api-cache"
1010

1111
(** return the file with a function f applied unless the file is empty;
1212
empty file:this is needed to simulate 404 returns from github *)
13-
let with_cache_file url f =
14-
match get_local_file url with
15-
| Error e ->
16-
let err_msg = sprintf "error while getting local file: %s\ncached for url: %s" e url in
17-
Printf.printf "%s\n" err_msg;
18-
Lwt.return_error err_msg
19-
| Ok "" -> Lwt.return_error "empty file"
20-
| Ok file -> Lwt.return_ok (f file)
13+
let with_cache_file cache_filepath f =
14+
match Std.input_file cache_filepath with
15+
| "" -> Lwt.return_error "empty file"
16+
| file -> Lwt.return_ok (f file)
17+
| exception exn -> Exn.fail ~exn "failed to get local cache file : %s" cache_filepath
2118

2219
let rec clean_forward_slashes str =
2320
let cont, ns = ExtLib.String.replace ~str ~sub:"/" ~by:"_" in

lib/common.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ end
3737

3838
open Devkit
3939

40-
let fmt_error fmt = Printf.ksprintf (fun s -> Error s) fmt
40+
let fmt_error ?exn fmt =
41+
Printf.ksprintf
42+
(fun s ->
43+
match exn with
44+
| Some exn -> Error (s ^ " : exn " ^ Exn.str exn)
45+
| None -> Error s
46+
)
47+
fmt
4148

4249
let first_line s =
4350
match String.split_on_char '\n' s with
@@ -55,11 +62,5 @@ let http_request ?headers ?body meth path =
5562
| `Ok s -> Lwt.return @@ Ok s
5663
| `Error e -> Lwt.return @@ Error e
5764

58-
let get_local_file path = try Ok (Std.input_file path) with exn -> fmt_error "%s" (Exn.to_string exn)
59-
60-
let write_to_local_file ~data path =
61-
try Ok (Devkit.Files.save_as path (fun oc -> Printf.fprintf oc "%s" data))
62-
with exn -> fmt_error "%s" (Exn.to_string exn)
63-
6465
let sign_string_sha256 ~key ~basestring =
6566
Cstruct.of_string basestring |> Nocrypto.Hash.SHA256.hmac ~key:(Cstruct.of_string key) |> Hex.of_cstruct |> Hex.show

lib/context.ml

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,28 @@ let is_pipeline_allowed ctx repo_url ~pipeline =
7575

7676
let refresh_secrets ctx =
7777
let path = ctx.secrets_filepath in
78-
match get_local_file path with
79-
| Error e -> fmt_error "error while getting local file: %s\nfailed to get secrets from file %s" e path
80-
| Ok file ->
81-
let secrets = Config_j.secrets_of_string file in
82-
begin
83-
match secrets.slack_access_token, secrets.slack_hooks with
84-
| None, [] -> fmt_error "either slack_access_token or slack_hooks must be defined in file '%s'" path
85-
| _ ->
86-
match secrets.repos with
87-
| [] -> fmt_error "at least one repository url must be specified in the 'repos' list in file %S" path
88-
| _ :: _ ->
89-
ctx.secrets <- Some secrets;
90-
Ok ctx
91-
end
78+
match Config_j.secrets_of_string (Std.input_file path) with
79+
| exception exn -> fmt_error ~exn "failed to read secrets from file %s" path
80+
| secrets ->
81+
match secrets.slack_access_token, secrets.slack_hooks with
82+
| None, [] -> fmt_error "either slack_access_token or slack_hooks must be defined in file %s" path
83+
| _ ->
84+
match secrets.repos with
85+
| [] -> fmt_error "at least one repository url must be specified in the 'repos' list in file %s" path
86+
| _ :: _ ->
87+
ctx.secrets <- Some secrets;
88+
Ok ctx
9289

9390
let refresh_state ctx =
9491
match ctx.state_filepath with
9592
| None -> Ok ctx
9693
| Some path ->
9794
if Sys.file_exists path then begin
9895
log#info "loading saved state from file %s" path;
99-
match get_local_file path with
100-
| Error e -> fmt_error "error while getting local file: %s\nfailed to get state from file %s" e path
101-
| Ok file ->
102-
(* todo: extract state related parts to state.ml *)
103-
let state = { State.state = State_j.state_of_string file } in
104-
Ok { ctx with state }
96+
(* todo: extract state related parts to state.ml *)
97+
match State_j.state_of_string (Std.input_file path) with
98+
| exception exn -> fmt_error ~exn "failed to read state from file %s" path
99+
| state -> Ok { ctx with state = { State.state } }
105100
end
106101
else Ok ctx
107102

lib/state.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let get_bot_user_id { state; _ } = state.State_t.bot_user_id
3636

3737
let save { state; _ } path =
3838
let data = State_j.string_of_state state |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
39-
match write_to_local_file ~data path with
40-
| Ok () -> Ok ()
41-
| Error e -> fmt_error "error while writing to local file %s: %s\nfailed to save state" path e
39+
try
40+
Files.save_as path (fun oc -> output_string oc data);
41+
Ok ()
42+
with exn -> fmt_error ~exn "failed to save state to file %s" path

src/monorobot.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ let check_gh_action file json config secrets state =
3636
| None ->
3737
log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file
3838
| Some kind ->
39-
match Common.get_local_file file with
40-
| Error e -> log#error "%s" e
41-
| Ok body ->
39+
match Std.input_file file with
40+
| exception exn -> log#error ~exn "failed to read file %s" file
41+
| body ->
4242
let headers = [ "x-github-event", kind ] in
4343
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets ?state_filepath:state () in
4444
( match Context.refresh_secrets ctx with

test/test.ml

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,37 @@ let process_gh_payload ~(secrets : Config_t.secrets) ~config (kind, path, state_
4141
let ctx = Context.make () in
4242
ctx.secrets <- Some secrets;
4343
let (_ : State_t.repo_state) = State.find_or_add_repo ctx.state repo.url in
44-
match state_path with
45-
| None ->
46-
Context.set_repo_config ctx repo.url config;
47-
Lwt.return ctx
48-
| Some state_path ->
49-
match Common.get_local_file state_path with
50-
| Error e ->
51-
log#error "failed to read %s: %s" state_path e;
52-
Lwt.return ctx
53-
| Ok file ->
54-
let repo_state = State_j.repo_state_of_string file in
55-
State.set_repo_state ctx.state repo.url repo_state;
56-
Context.set_repo_config ctx repo.url config;
57-
Lwt.return ctx
44+
let () =
45+
match state_path with
46+
| None -> Context.set_repo_config ctx repo.url config
47+
| Some state_path ->
48+
match State_j.repo_state_of_string (Std.input_file state_path) with
49+
| repo_state ->
50+
State.set_repo_state ctx.state repo.url repo_state;
51+
Context.set_repo_config ctx repo.url config
52+
| exception exn -> log#error ~exn "failed to load state from file %s" state_path
53+
in
54+
ctx
5855
in
5956
Printf.printf "===== file %s =====\n" path;
6057
let headers = [ "x-github-event", kind ] in
61-
match Common.get_local_file path with
62-
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
63-
| Ok event ->
64-
let%lwt ctx = make_test_context event in
65-
let%lwt _ctx = Action_local.process_github_notification ctx headers event in
58+
match Std.input_file path with
59+
| event ->
60+
let ctx = make_test_context event in
61+
Action_local.process_github_notification ctx headers event
62+
| exception exn ->
63+
log#error ~exn "failed to read file %s" path;
6664
Lwt.return_unit
6765

6866
let process_slack_event ~(secrets : Config_t.secrets) path =
6967
let ctx = Context.make () in
7068
ctx.secrets <- Some secrets;
7169
State.set_bot_user_id ctx.state "bot_user";
7270
Printf.printf "===== file %s =====\n" path;
73-
match Common.get_local_file path with
74-
| Error e -> Lwt.return @@ log#error "failed to read %s: %s" path e
75-
| Ok body ->
76-
match Slack_j.event_notification_of_string body with
71+
match Slack_j.event_notification_of_string (Std.input_file path) with
72+
| exception exn ->
73+
log#error ~exn "failed to read event notification from file %s" path;
74+
Lwt.return_unit
7775
| Url_verification _ -> Lwt.return ()
7876
| Event_callback notification ->
7977
match notification.event with

0 commit comments

Comments
 (0)