Skip to content

Commit 6e6ae96

Browse files
committed
rename notification -> message to distinguish from gh notificaiton
1 parent 9981e74 commit 6e6ae96

8 files changed

+43
-44
lines changed

lib/action.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
171171
| _ -> Lwt.return []
172172

173173
let send_notifications (ctx : Context.t) notifications =
174-
let notify (notification : Slack_t.notification) =
175-
match%lwt Slack_api.send_notification ~ctx ~notification with
174+
let notify (message : Slack_t.message) =
175+
match%lwt Slack_api.send_notification ~ctx ~message with
176176
| Ok () -> Lwt.return_unit
177177
| Error e -> action_error e
178178
in

lib/api.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module type Github = sig
99
end
1010

1111
module type Slack = sig
12-
val send_notification : ctx:Context.t -> notification:notification -> (unit, string) Result.t Lwt.t
12+
val send_notification : ctx:Context.t -> message:message -> (unit, string) Result.t Lwt.t
1313

1414
val update_access_token_of_context : ctx:Context.t -> code:string -> (unit, string) Result.t Lwt.t
1515
end

lib/api_local.ml

+9-11
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ module Github : Api.Github = struct
2121
end
2222

2323
module Slack : Api.Slack = struct
24-
let send_notification ~ctx:_ ~notification =
25-
let json =
26-
notification |> Slack_j.string_of_notification |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
27-
in
28-
Stdio.printf "will notify #%s\n" notification.channel;
24+
let send_notification ~ctx:_ ~message =
25+
let json = message |> Slack_j.string_of_message |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
26+
Stdio.printf "will notify #%s\n" message.channel;
2927
Stdio.printf "%s\n" json;
3028
Lwt.return @@ Ok ()
3129

@@ -37,9 +35,9 @@ end
3735
module Slack_simple : Api.Slack = struct
3836
let log = Log.from "slack"
3937

40-
let send_notification ~ctx:_ ~(notification : Slack_t.notification) =
41-
log#info "will notify %s%s" notification.channel
42-
( match notification.Slack_t.text with
38+
let send_notification ~ctx:_ ~(message : Slack_t.message) =
39+
log#info "will notify %s%s" message.channel
40+
( match message.Slack_t.text with
4341
| None -> ""
4442
| Some s -> Printf.sprintf " with %S" s
4543
);
@@ -51,9 +49,9 @@ end
5149
module Slack_json : Api.Slack = struct
5250
let log = Log.from "slack"
5351

54-
let send_notification ~ctx:_ ~notification =
55-
let json = Slack_j.string_of_notification notification in
56-
log#info "will notify %s" notification.channel;
52+
let send_notification ~ctx:_ ~message =
53+
let json = Slack_j.string_of_message message in
54+
log#info "will notify %s" message.channel;
5755
let url = Uri.of_string "https://api.slack.com/docs/messages/builder" in
5856
let url = Uri.add_query_param url ("msg", [ json ]) in
5957
log#info "%s" (Uri.to_string url);

lib/api_remote.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ end
5050
module Slack : Api.Slack = struct
5151
let log = Log.from "slack"
5252

53-
let send_notification ~(ctx : Context.t) ~notification =
53+
let send_notification ~(ctx : Context.t) ~message =
5454
let secrets = Context.get_secrets_exn ctx in
5555
match secrets.slack_access_token with
5656
| None -> Lwt.return @@ fmt_error "failed to retrieve Slack access token"
5757
| Some access_token ->
5858
let url = "https://slack.com/api/chat.postMessage" in
59-
let data = Slack_j.string_of_notification notification in
59+
let data = Slack_j.string_of_message message in
6060
let headers = [ Printf.sprintf "Authorization: Bearer %s" access_token ] in
6161
let body = `Raw ("application/json", data) in
62-
log#info "sending to %s : %s" notification.channel data;
62+
log#info "sending to %s : %s" message.channel data;
6363
( match%lwt http_request ~body ~headers `POST url with
6464
| Ok s ->
65-
let res = Slack_j.notification_response_of_string s in
65+
let res = Slack_j.message_response_of_string s in
6666
if res.ok then Lwt.return @@ Ok ()
6767
else (
6868
let msg = Option.value ~default:"an unknown error occurred" res.error in

lib/config.atd

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type secrets = {
4343
slack_client_secret : string;
4444
(* Slack uses this secret to sign requests; provide to verify incoming Slack requests *)
4545
?slack_signing_secret : string option;
46-
(* if specified, maintains state b/w OAuth request and callback to prevent CSRF
46+
(* if specified, maintains state b/w OAuth request and callback to prevent CSRF
4747
see: https://tools.ietf.org/html/rfc6749#section-4.1.1 *)
4848
?slack_oauth_state : string option;
4949
(* Slack bot token obtained via OAuth, enabling message posting to the workspace;

lib/slack.atd

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
type notification_field = {
1+
type message_field = {
22
?title: string nullable;
33
value: string;
44
}
55

6-
type notification_attachment = {
6+
type message_attachment = {
77
fallback: string nullable;
88
?mrkdwn_in: string list nullable;
99
?color: string nullable;
@@ -14,18 +14,18 @@ type notification_attachment = {
1414
?title: string nullable;
1515
?title_link: string nullable;
1616
?text: string nullable;
17-
?fields: notification_field list nullable;
17+
?fields: message_field list nullable;
1818
?image_url: string nullable;
1919
?thumb_url: string nullable;
2020
?ts: int nullable;
2121
?footer: string nullable;
2222
}
2323

24-
type notification_section_block_type = [
24+
type message_section_block_type = [
2525
Section <json name="section">
2626
] <ocaml repr="classic">
2727

28-
type notification_divider_block_type = [
28+
type message_divider_block_type = [
2929
Divider <json name="divider">
3030
] <ocaml repr="classic">
3131

@@ -39,25 +39,25 @@ type text_object = {
3939
text: string;
4040
}
4141

42-
type notification_text_block = {
43-
notification_type <json name="type"> : notification_section_block_type;
42+
type message_text_block = {
43+
message_type <json name="type"> : message_section_block_type;
4444
text: text_object;
4545
}
4646

47-
type notification_divider_block = {
48-
notification_type <json name="type"> : notification_divider_block_type;
47+
type message_divider_block = {
48+
message_type <json name="type"> : message_divider_block_type;
4949
}
5050

51-
type notification_block = [
52-
Text of notification_text_block
53-
| Divider of notification_divider_block
51+
type message_block = [
52+
Text of message_text_block
53+
| Divider of message_divider_block
5454
] <json adapter.ocaml="Atdgen_runtime.Json_adapter.Type_field">
5555

56-
type notification = {
56+
type message = {
5757
channel: string;
5858
?text: string nullable;
59-
?attachments: notification_attachment list nullable;
60-
?blocks: notification_block list nullable;
59+
?attachments: message_attachment list nullable;
60+
?blocks: message_block list nullable;
6161
}
6262

6363
(* expected payload when exchanging oauth code for access token *)
@@ -67,8 +67,8 @@ type oauth_access_response = {
6767
?error: string option;
6868
}
6969

70-
type notification_response = {
70+
type message_response = {
7171
ok: bool;
7272
?channel: string option;
7373
?error: string option;
74-
}
74+
}

src/notabot.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ let check_gh_action file json config secrets state =
3939
Action.process_github_notification ctx headers body
4040
)
4141

42-
let check_slack_action file config secrets =
42+
let check_slack_action file secrets =
4343
let data = Stdio.In_channel.read_all file in
44-
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets () in
45-
match Slack_j.notification_of_string data with
44+
let ctx = Context.make ~secrets_filepath:secrets () in
45+
match Slack_j.message_of_string data with
4646
| exception exn -> log#error ~exn "unable to parse notification"
47-
| notification ->
47+
| message ->
4848
match Context.refresh_secrets ctx with
4949
| Error e -> log#error "%s" e
5050
| Ok ctx ->
5151
Lwt_main.run
52-
( match%lwt Api_remote.Slack.send_notification ~ctx ~notification with
53-
| Error e -> Lwt.return @@ log#error "%s" e
52+
( match%lwt Api_remote.Slack.send_notification ~ctx ~message with
53+
| Error e ->
54+
log#error "%s" e;
55+
Lwt.return_unit
5456
| Ok () -> Lwt.return_unit
5557
)
5658

@@ -105,7 +107,7 @@ let check_gh =
105107
let check_slack =
106108
let doc = "read a Slack notification from a file and send it to a channel; used for testing" in
107109
let info = Term.info "check_slack" ~doc in
108-
let term = Term.(const check_slack_action $ slack_payload $ config $ secrets) in
110+
let term = Term.(const check_slack_action $ slack_payload $ secrets) in
109111
term, info
110112

111113
let default_cmd =

test/slack_oauth_test.ml

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ let secrets_with_state = { secrets_default with slack_oauth_state = Some "foo" }
3333

3434
let secrets_with_token = { secrets_default with slack_access_token = Some "123" }
3535

36-
(* name, args, headers, body *)
3736
let tests =
3837
[
3938
"success, valid params", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_with_key;

0 commit comments

Comments
 (0)