1
1
open Base
2
2
open Common
3
3
open Devkit
4
+ open Printf
4
5
5
6
let cwd = Caml.Sys. getcwd ()
6
7
let cache_dir = Caml.Filename. concat cwd " github-api-cache"
7
8
9
+ (* * return the file with a function f applied unless the file is empty;
10
+ empty file:this is needed to simulate 404 returns from github *)
11
+ let with_cache_file url f =
12
+ match get_local_file url with
13
+ | Error e ->
14
+ let err_msg = sprintf " error while getting local file: %s\n cached for url: %s" e url in
15
+ Stdio. print_endline err_msg;
16
+ Lwt. return_error err_msg
17
+ | Ok "" -> Lwt. return_error " empty file"
18
+ | Ok file -> Lwt. return_ok (f file)
19
+
20
+ let clean_forward_slashes = String. substr_replace_all ~pattern: " /" ~with_: " _"
21
+
22
+ (* * get a member of the repo cached API call providing
23
+ the member kind (pull, issue, commit, compare, etc),
24
+ _ref (pr number, issue number, commit sha, compare basehead, etc),
25
+ and its Github_j.<kind>_of_string function.
26
+ NB: please save the cache file in the same format *)
27
+ let get_repo_member_cache ~(repo : Github_t.repository ) ~kind ~ref_ ~of_string =
28
+ let file = clean_forward_slashes (sprintf " %s_%s_%s" repo.full_name kind ref_) in
29
+ let url = Caml.Filename. concat cache_dir file in
30
+ with_cache_file url of_string
31
+
8
32
module Github : Api .Github = struct
9
33
let get_config ~(ctx : Context.t ) ~repo :_ =
10
34
let url = Caml.Filename. concat cwd ctx.config_filename in
11
- match get_local_file url with
12
- | Error e -> Lwt. return @@ fmt_error " error while getting local file: %s\n failed to get config %s" e url
13
- | Ok file -> Lwt. return @@ Ok (Config_j. config_of_string file)
14
-
15
- let get_api_commit ~ctx :_ ~repo :_ ~sha =
16
- let url = Caml.Filename. concat cache_dir sha in
17
- match get_local_file url with
18
- | Error e -> Lwt. return @@ fmt_error " error while getting local file: %s\n failed to get api commit %s" e url
19
- | Ok file -> Lwt. return @@ Ok (Github_j. api_commit_of_string file)
20
-
21
- let get_pull_request ~ctx :_ ~repo :_ ~number :_ = Lwt. return @@ Error " undefined for local setup"
22
- let get_issue ~ctx :_ ~repo :_ ~number :_ = Lwt. return @@ Error " undefined for local setup"
35
+ with_cache_file url Config_j. config_of_string
36
+
37
+ let get_api_commit ~ctx :_ ~repo ~sha =
38
+ get_repo_member_cache ~repo ~kind: " commit" ~ref_: sha ~of_string: Github_j. api_commit_of_string
39
+
40
+ let get_pull_request ~ctx :_ ~(repo : Github_t.repository ) ~number =
41
+ get_repo_member_cache ~repo ~kind: " pull" ~ref_: (Int. to_string number) ~of_string: Github_j. pull_request_of_string
42
+
43
+ let get_issue ~ctx :_ ~(repo : Github_t.repository ) ~number =
44
+ get_repo_member_cache ~repo ~kind: " issue" ~ref_: (Int. to_string number) ~of_string: Github_j. issue_of_string
45
+
46
+ let get_compare ~ctx :_ ~(repo : Github_t.repository ) ~basehead :(base , merge ) =
47
+ get_repo_member_cache ~repo ~kind: " compare" ~ref_: (sprintf " %s...%s" base merge)
48
+ ~of_string: Github_j. compare_of_string
49
+
23
50
let request_reviewers ~ctx :_ ~repo :_ ~number :_ ~reviewers :_ = Lwt. return @@ Error " undefined for local setup"
24
51
end
25
52
53
+ (* * The base implementation for local check payload debugging and mocking tests *)
26
54
module Slack_base : Api .Slack = struct
27
55
let send_notification ~ctx :_ ~msg :_ = Lwt. return @@ Error " undefined for local setup"
28
56
let send_chat_unfurl ~ctx :_ ~channel :_ ~ts :_ ~unfurls :_ () = Lwt. return @@ Error " undefined for local setup"
29
57
let send_auth_test ~ctx :_ () = Lwt. return @@ Error " undefined for local setup"
30
58
end
31
59
60
+ (* * Module for mocking test requests to slack--will output on Stdio *)
32
61
module Slack : Api .Slack = struct
33
62
include Slack_base
34
63
@@ -37,8 +66,20 @@ module Slack : Api.Slack = struct
37
66
Stdio. printf " will notify #%s\n " msg.channel;
38
67
Stdio. printf " %s\n " json;
39
68
Lwt. return @@ Ok ()
69
+
70
+ let send_chat_unfurl ~ctx :_ ~channel ~ts ~unfurls () =
71
+ let req = Slack_j. { channel; ts; unfurls } in
72
+ let data = req |> Slack_j. string_of_chat_unfurl_req |> Yojson.Basic. from_string |> Yojson.Basic. pretty_to_string in
73
+ Stdio. printf " will unfurl in #%s\n " channel;
74
+ Stdio. printf " %s\n " data;
75
+ Lwt. return @@ Ok ()
76
+
77
+ let send_auth_test ~ctx :_ () =
78
+ Lwt. return
79
+ @@ Ok ({ url = " " ; team = " " ; user = " " ; team_id = " " ; user_id = " test_slack_user" } : Slack_t.auth_test_res )
40
80
end
41
81
82
+ (* * Simple messages (only the actual text messages that users see) output to log for checking payload commands *)
42
83
module Slack_simple : Api .Slack = struct
43
84
include Slack_base
44
85
@@ -48,11 +89,22 @@ module Slack_simple : Api.Slack = struct
48
89
log#info " will notify %s%s" msg.channel
49
90
( match msg.Slack_t. text with
50
91
| None -> " "
51
- | Some s -> Printf. sprintf " with %S" s
92
+ | Some s -> sprintf " with %S" s
52
93
);
53
94
Lwt. return @@ Ok ()
95
+
96
+ let send_chat_unfurl ~ctx :_ ~channel ~ts :_ ~(unfurls : Slack_t.message_attachment Common.StringMap.t ) () =
97
+ Stdio. printf " will unfurl in #%s\n " channel;
98
+ let unfurl_text = List. map (StringMap. to_list unfurls) ~f: (fun (_ , unfurl ) -> unfurl.text) in
99
+ Stdio. printf " %s\n " (String. concat ~sep: " \n " (List. filter_opt unfurl_text));
100
+ Lwt. return @@ Ok ()
101
+
102
+ let send_auth_test ~ctx :_ () =
103
+ Lwt. return
104
+ @@ Ok ({ url = " " ; team = " " ; user = " " ; team_id = " " ; user_id = " test_slack_user" } : Slack_t.auth_test_res )
54
105
end
55
106
107
+ (* * Messages payload in json output to log for checking payload commands *)
56
108
module Slack_json : Api .Slack = struct
57
109
include Slack_base
58
110
@@ -66,4 +118,16 @@ module Slack_json : Api.Slack = struct
66
118
log#info " %s" (Uri. to_string url);
67
119
log#info " %s" json;
68
120
Lwt. return @@ Ok ()
121
+
122
+ let send_chat_unfurl ~ctx :_ ~channel ~ts :_ ~(unfurls : Slack_t.message_attachment Common.StringMap.t ) () =
123
+ log#info " will notify %s" channel;
124
+ let json = List. map (StringMap. to_list unfurls) ~f: (fun (_ , unfurl ) -> Slack_j. string_of_unfurl unfurl) in
125
+ let url = Uri. of_string " https://slack.com/api/chat.unfurl" in
126
+ log#info " %s" (Uri. to_string url);
127
+ log#info " %s" (String. concat ~sep: " ;\n " json);
128
+ Lwt. return @@ Ok ()
129
+
130
+ let send_auth_test ~ctx :_ () =
131
+ Lwt. return
132
+ @@ Ok ({ url = " " ; team = " " ; user = " " ; team_id = " " ; user_id = " test_slack_user" } : Slack_t.auth_test_res )
69
133
end
0 commit comments