Skip to content

Commit cd3b4b9

Browse files
committed
improve readability and remove redundant code
- make exceptions adhere to naming conventions - fix typo in function name - remove trivial pattern matching - remove module prefix for locally opened modules - prefer ppx syntax over Lwt.catch
1 parent 68e9d3b commit cd3b4b9

File tree

6 files changed

+43
-45
lines changed

6 files changed

+43
-45
lines changed

lib/action.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ let partition_status (ctx : Context.t) (n : status_notification) =
224224
match List.exists n.branches ~f:(fun { name } -> String.equal name main_branch_name) with
225225
| false -> default ()
226226
| true ->
227-
( match%lwt Github.generate_query_commmit cfg ~url:n.commit.url ~sha:n.commit.sha with
227+
( match%lwt Github.generate_query_commit cfg ~url:n.commit.url ~sha:n.commit.sha with
228228
| None -> default ()
229229
| Some commit ->
230230
(*

lib/context.ml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Devkit
22

33
let log = Log.from "context"
44

5-
exception Context_Error of string
5+
exception Context_error of string
66

77
type cfg_make_args =
88
| LocalMake of string
@@ -37,16 +37,11 @@ type context_thunk = {
3737

3838
let get_secrets secrets_path = Config.load_secrets_file ~secrets_path
3939

40-
let get_remote_cfg_json_url url = Github.load_config_json url
41-
42-
let get_remote_cfg_json_req filename ?token req =
43-
get_remote_cfg_json_url @@ Github.get_remote_config_json_url filename ?token req
44-
45-
let get_local_cfg_json config_path = Lwt.return @@ Config.load_config_file ~config_path
40+
let load_config_json_local config_path = Lwt.return @@ Config.load_config_file ~config_path
4641

4742
let resolve_cfg_getter = function
48-
| Local -> get_local_cfg_json
49-
| Remote -> get_remote_cfg_json_url
43+
| Local -> load_config_json_local
44+
| Remote -> Github.load_config_json
5045

5146
let refresh_config ctx =
5247
( match ctx.data.cfg_source with
@@ -68,7 +63,7 @@ let refresh_and_get_config ctx =
6863

6964
let change_remote_url filename ctx req =
7065
match ctx.data.cfg_source with
71-
| Local -> raise @@ Context_Error "cant load remote config from a local context"
66+
| Local -> raise @@ Context_error "can't load remote config from a local context"
7267
| Remote ->
7368
ctx.data.cfg_filename <- filename;
7469
let url = Github.get_remote_config_json_url filename ?token:ctx.secrets.gh_token req in
@@ -96,11 +91,12 @@ let make_with_secrets ~state_path ~cfg_args ~secrets_path ~(secrets : Notabot_t.
9691
=
9792
let data_cfg_path, cfg_source, cfg_json, cfg_filename =
9893
match cfg_args with
99-
| LocalMake p -> p, Local, get_local_cfg_json p, p
94+
| LocalMake p -> p, Local, load_config_json_local p, p
10095
| RemoteMake (filename, r) ->
101-
match secrets.gh_token with
102-
| token ->
103-
Github.get_remote_config_json_url filename ?token r, Remote, get_remote_cfg_json_req filename ?token r, filename
96+
let token = secrets.gh_token in
97+
let url = Github.get_remote_config_json_url filename ?token r in
98+
let cfg_json = Github.load_config_json @@ Github.get_remote_config_json_url filename ?token r in
99+
url, Remote, cfg_json, filename
104100
in
105101
let%lwt cfg_json = cfg_json in
106102
let data =

lib/context.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exception Context_Error of string
1+
exception Context_error of string
22

33
type cfg_make_args =
44
| LocalMake of string

lib/github.ml

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ type t =
1818

1919
(* all other events *)
2020

21-
exception Remote_Config_Error of string
21+
exception Remote_config_error of string
22+
23+
let remote_config_error fmt = ksprintf (fun s -> raise (Remote_config_error s)) fmt
2224

2325
let to_repo = function
2426
| Push n -> Some n.repository
@@ -34,8 +36,8 @@ let to_repo = function
3436
let api_url_of_repo (repo : repository) =
3537
Option.map ~f:(fun host ->
3638
match host with
37-
| "api.github.com" -> Printf.sprintf "https://%s" host
38-
| _ -> Printf.sprintf "https://%s/api/v3" host)
39+
| "api.github.com" -> sprintf "https://%s" host
40+
| _ -> sprintf "https://%s/api/v3" host)
3941
@@ Uri.host
4042
@@ Uri.of_string repo.url
4143

@@ -51,24 +53,25 @@ let name_of_full_name_parts full_name_parts =
5153

5254
let get_remote_config_json_url filename ?token req =
5355
match to_repo req with
54-
| None -> raise @@ Remote_Config_Error "unable to resolve repository from request"
56+
| None -> remote_config_error "unable to resolve repository from request"
5557
| Some repo ->
5658
match String.split ~on:'/' repo.full_name with
5759
| full_name_parts ->
5860
match user_of_full_name_parts full_name_parts with
59-
| None -> raise @@ Remote_Config_Error "unable to resolve repository owner"
61+
| None -> remote_config_error "unable to resolve repository owner"
6062
| Some owner ->
6163
match name_of_full_name_parts full_name_parts with
62-
| None -> raise @@ Remote_Config_Error "unable to resolve repository name"
64+
| None -> remote_config_error "unable to resolve repository name"
6365
| Some repo_name ->
6466
match api_url_of_repo repo with
65-
| None -> raise @@ Remote_Config_Error "unable to resolve github api url from repository url"
67+
| None -> remote_config_error "unable to resolve github api url from repository url"
6668
| Some base_url ->
67-
match Printf.sprintf "%s/repos/%s/%s/contents/%s" base_url owner repo_name filename with
68-
| url ->
69-
match token with
70-
| None -> url
71-
| Some token -> Printf.sprintf "%s?access_token=%s" url token
69+
let url = sprintf "%s/repos/%s/%s/contents/%s" base_url owner repo_name filename in
70+
begin
71+
match token with
72+
| None -> url
73+
| Some token -> sprintf "%s?access_token=%s" url token
74+
end
7275

7376
let config_of_content_api_response response =
7477
match response.encoding with
@@ -79,12 +82,12 @@ let config_of_content_api_response response =
7982
@@ String.concat
8083
@@ String.split ~on:'\n'
8184
@@ response.content
82-
| e -> raise @@ Remote_Config_Error (Printf.sprintf "unknown encoding format '%s'." e)
85+
| e -> remote_config_error "unknown encoding format '%s'" e
8386

8487
let load_config_json url =
85-
let headers = Some [ "Accept: application/vnd.github.v3+json" ] in
86-
match%lwt Web.http_request_lwt ?headers `GET url with
87-
| `Error e -> raise @@ Remote_Config_Error (Printf.sprintf "error while querying github api %s: %s" url e)
88+
let headers = [ "Accept: application/vnd.github.v3+json" ] in
89+
match%lwt Web.http_request_lwt ~headers `GET url with
90+
| `Error e -> remote_config_error "error while querying github api %s: %s" url e
8891
| `Ok s -> config_of_content_api_response @@ Github_j.content_api_response_of_string s
8992

9093
let is_valid_signature ~secret headers_sig body =
@@ -93,7 +96,7 @@ let is_valid_signature ~secret headers_sig body =
9396
Cstruct.to_string @@ Nocrypto.Hash.SHA1.hmac ~key (Cstruct.of_string body)
9497
in
9598
let (`Hex request_hash) = Hex.of_string request_hash in
96-
String.equal headers_sig (Printf.sprintf "sha1=%s" request_hash)
99+
String.equal headers_sig (sprintf "sha1=%s" request_hash)
97100

98101
(* Parse a payload. The type of the payload is detected from the headers. *)
99102
let parse_exn ~secret headers body =
@@ -135,7 +138,7 @@ let query_api ?token ~url parse =
135138
log#error ~exn "impossible to parse github api answer to %s" url;
136139
Lwt.return_none
137140

138-
let generate_query_commmit cfg ~url ~sha =
141+
let generate_query_commit cfg ~url ~sha =
139142
(* the expected output is a payload containing content about commits *)
140143
match cfg.Config.offline with
141144
| None -> query_api ?token:cfg.Config.gh_token ~url api_commit_of_string
@@ -168,4 +171,4 @@ let generate_commit_from_commit_comment cfg n =
168171
in
169172
let commit_url = String.sub ~pos:0 ~len:url_length url ^ "/" ^ sha in
170173
(* add sha hash to get the full api link *)
171-
generate_query_commmit cfg ~url:commit_url ~sha
174+
generate_query_commit cfg ~url:commit_url ~sha

src/request_handler.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ let update_config (ctx : Lib.Context.t) = function
77
| Lib.Github.Push n ->
88
let is_config_file f = String.equal f ctx.data.cfg_filename in
99
let commit_contains_config_file (c : Lib.Github_t.commit) = List.exists is_config_file (c.added @ c.modified) in
10-
( match List.exists commit_contains_config_file n.commits with
11-
| true -> Lib.Context.refresh_config ctx
12-
| false -> Lwt.return_unit
13-
)
10+
if List.exists commit_contains_config_file n.commits then Lib.Context.refresh_config ctx else Lwt.return_unit
1411
| _ -> Lwt.return_unit
1512

1613
let process_github_notification (ctx_thunk : Lib.Context.context_thunk) headers body =
@@ -31,10 +28,10 @@ let process_github_notification (ctx_thunk : Lib.Context.context_thunk) headers
3128
Slack.send_notification url data)
3229
notifications
3330
with
34-
| Lib.Context.Context_Error s ->
31+
| Context.Context_error s ->
3532
log#error "error creating context from payload: %s" s;
3633
Lwt.return_unit
37-
| Lib.Github.Remote_Config_Error s ->
34+
| Github.Remote_config_error s ->
3835
log#error "error retrieving config from payload: %s" s;
3936
Lwt.return_unit
4037

test/test.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
open Base
22
open Lib
33

4+
let log = Devkit.Log.from "test"
5+
46
let print_notif (chan, msg) =
57
let json =
68
msg |> Slack_j.string_of_webhook_notification |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
@@ -23,10 +25,10 @@ let process ~state_dir ~cfg_path ~secrets_path file =
2325
let state_path = Caml.Filename.concat state_dir @@ Caml.Filename.basename file in
2426
let ctx_partial = Context.make ~state_path ~secrets_path ~disable_write:true in
2527
let%lwt ctx =
26-
Lwt.catch
27-
(fun () -> ctx_partial ~cfg_args:(RemoteMake (cfg_path, event)) ())
28-
(function
29-
| _ -> ctx_partial ~cfg_args:(LocalMake cfg_path) ())
28+
try%lwt ctx_partial ~cfg_args:(RemoteMake (cfg_path, event)) ()
29+
with exn ->
30+
log#info ~exn "unable to find a remote configuration %s" cfg_path;
31+
ctx_partial ~cfg_args:(LocalMake cfg_path) ()
3032
in
3133
let%lwt notifs = Action.generate_notifications ctx event in
3234
List.iter notifs ~f:print_notif;

0 commit comments

Comments
 (0)