@@ -25,59 +25,48 @@ let http_server_action addr port config secrets state =
25
25
)
26
26
)
27
27
28
- (* let check_slack_action webhook file =
28
+ (* * In check mode, instead of actually sending the message to slack, we
29
+ simply print it in the console *)
30
+ 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
56
+
57
+ let check_slack_action url file =
29
58
let data = Stdio.In_channel. read_all file in
59
+ let chan = Printf. sprintf " webhook %s" url in
30
60
match Slack_j. webhook_notification_of_string data with
31
61
| exception exn -> log#error ~exn " unable to parse notification"
32
- | _ -> Lwt_main.run (Slack.send_notification webhook data)
33
-
34
- let check_common file print config secrets state_path =
35
- let ctx_thunk =
36
- Context.make_thunk ~state_path ~cfg_path_or_remote_filename:config ~secrets_path:secrets ~cfg_action_after_refresh
37
- ()
38
- in
39
- let filename = Caml.Filename.basename file in
40
- match Github.event_of_filename filename with
41
- | None ->
42
- log#error "aborting because payload %s is not named properly, named should be KIND.NAME_OF_PAYLOAD.json" file;
43
- Lwt.return_unit
44
- | Some kind ->
45
- let headers = [ "x-github-event", kind ] in
46
- (* read the event from a file and try to parse it *)
47
- ( match Github.parse_exn ~secret:None headers (Stdio.In_channel.read_all file) with
48
- | exception exn ->
49
- log#error ~exn "unable to parse payload";
50
- Lwt.return_unit
51
- | event ->
52
- let%lwt ctx = Context.resolve_ctx_in_thunk ctx_thunk event in
53
- let%lwt notifs = Action.generate_notifications ctx event in
54
- List.iter ~f:print notifs;
55
- Lwt.return_unit
56
- )
57
-
58
- let print_simplified_message (chan, msg) =
59
- (* In check mode, instead of actually sending the message to slack, we
60
- simply print it in the console *)
61
- log#info "will notify %s%s" chan
62
- ( match msg.Slack_t.text with
63
- | None -> ""
64
- | Some s -> Printf.sprintf " with %S" s
65
- )
66
-
67
- let print_json_message (chan, msg) =
68
- let json = Slack_j.string_of_webhook_notification msg in
69
- log#info "will notify %s" chan;
70
- let url = Uri.of_string "https://api.slack.com/docs/messages/builder" in
71
- let url = Uri.add_query_param url ("msg", [ json ]) in
72
- log#info "%s" (Uri.to_string url);
73
- log#info "%s" json
74
-
75
- let check_gh_action file json config secrets state =
76
- Lwt_main.run
77
- ( match json with
78
- | false -> check_common file print_simplified_message config secrets state
79
- | true -> check_common file print_json_message config secrets state
80
- ) *)
62
+ | msg ->
63
+ Lwt_main. run
64
+ ( match % lwt Api_remote.Slack. send_notification ~chan ~msg ~url with
65
+ | Error e ->
66
+ log#error " %s" e;
67
+ Lwt. return_unit
68
+ | Ok () -> Lwt. return_unit
69
+ )
81
70
82
71
(* flags *)
83
72
@@ -91,7 +80,7 @@ let port =
91
80
92
81
let config =
93
82
let doc = " remote configuration file name" in
94
- Arg. (value & opt file " monorobot.json" & info [ " config" ] ~docv: " CONFIG" ~doc )
83
+ Arg. (value & opt string " monorobot.json" & info [ " config" ] ~docv: " CONFIG" ~doc )
95
84
96
85
let secrets =
97
86
let doc = " configuration file containing secrets" in
@@ -125,7 +114,7 @@ let run =
125
114
let term = Term. (const http_server_action $ addr $ port $ config $ secrets $ state) in
126
115
term, info
127
116
128
- (* let check_gh =
117
+ let check_gh =
129
118
let doc = " read a Github notification from a file and display the actions that will be taken; used for testing" in
130
119
let info = Term. info " check_gh" ~doc in
131
120
let term = Term. (const check_gh_action $ gh_payload $ json $ config $ secrets $ state) in
@@ -135,13 +124,12 @@ let check_slack =
135
124
let doc = " read a Slack notification from a file and send it to a webhook; used for testing" in
136
125
let info = Term. info " check_slack" ~doc in
137
126
let term = Term. (const check_slack_action $ slack_webhook_url $ slack_payload) in
138
- term, info *)
127
+ term, info
139
128
140
129
let default_cmd =
141
130
let doc = " the notification bot" in
142
131
Term. (ret (const (`Help (`Pager , None )))), Term. info " monorobot" ~doc
143
132
144
- (* let cmds = [ run; check_gh; check_slack ] *)
145
- let cmds = [ run ]
133
+ let cmds = [ run; check_gh; check_slack ]
146
134
147
135
let () = Term. (exit @@ eval_choice default_cmd cmds)
0 commit comments