Skip to content

Commit

Permalink
rename notification -> message to distinguish from gh notificaiton
Browse files Browse the repository at this point in the history
  • Loading branch information
yasunariw committed Dec 24, 2020
1 parent 9981e74 commit 6e6ae96
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lib/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
| _ -> Lwt.return []

let send_notifications (ctx : Context.t) notifications =
let notify (notification : Slack_t.notification) =
match%lwt Slack_api.send_notification ~ctx ~notification with
let notify (message : Slack_t.message) =
match%lwt Slack_api.send_notification ~ctx ~message with
| Ok () -> Lwt.return_unit
| Error e -> action_error e
in
Expand Down
2 changes: 1 addition & 1 deletion lib/api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module type Github = sig
end

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

val update_access_token_of_context : ctx:Context.t -> code:string -> (unit, string) Result.t Lwt.t
end
20 changes: 9 additions & 11 deletions lib/api_local.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ module Github : Api.Github = struct
end

module Slack : Api.Slack = struct
let send_notification ~ctx:_ ~notification =
let json =
notification |> Slack_j.string_of_notification |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string
in
Stdio.printf "will notify #%s\n" notification.channel;
let send_notification ~ctx:_ ~message =
let json = message |> Slack_j.string_of_message |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
Stdio.printf "will notify #%s\n" message.channel;
Stdio.printf "%s\n" json;
Lwt.return @@ Ok ()

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

let send_notification ~ctx:_ ~(notification : Slack_t.notification) =
log#info "will notify %s%s" notification.channel
( match notification.Slack_t.text with
let send_notification ~ctx:_ ~(message : Slack_t.message) =
log#info "will notify %s%s" message.channel
( match message.Slack_t.text with
| None -> ""
| Some s -> Printf.sprintf " with %S" s
);
Expand All @@ -51,9 +49,9 @@ end
module Slack_json : Api.Slack = struct
let log = Log.from "slack"

let send_notification ~ctx:_ ~notification =
let json = Slack_j.string_of_notification notification in
log#info "will notify %s" notification.channel;
let send_notification ~ctx:_ ~message =
let json = Slack_j.string_of_message message in
log#info "will notify %s" message.channel;
let url = Uri.of_string "https://api.slack.com/docs/messages/builder" in
let url = Uri.add_query_param url ("msg", [ json ]) in
log#info "%s" (Uri.to_string url);
Expand Down
8 changes: 4 additions & 4 deletions lib/api_remote.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ end
module Slack : Api.Slack = struct
let log = Log.from "slack"

let send_notification ~(ctx : Context.t) ~notification =
let send_notification ~(ctx : Context.t) ~message =
let secrets = Context.get_secrets_exn ctx in
match secrets.slack_access_token with
| None -> Lwt.return @@ fmt_error "failed to retrieve Slack access token"
| Some access_token ->
let url = "https://slack.com/api/chat.postMessage" in
let data = Slack_j.string_of_notification notification in
let data = Slack_j.string_of_message message in
let headers = [ Printf.sprintf "Authorization: Bearer %s" access_token ] in
let body = `Raw ("application/json", data) in
log#info "sending to %s : %s" notification.channel data;
log#info "sending to %s : %s" message.channel data;
( match%lwt http_request ~body ~headers `POST url with
| Ok s ->
let res = Slack_j.notification_response_of_string s in
let res = Slack_j.message_response_of_string s in
if res.ok then Lwt.return @@ Ok ()
else (
let msg = Option.value ~default:"an unknown error occurred" res.error in
Expand Down
2 changes: 1 addition & 1 deletion lib/config.atd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type secrets = {
slack_client_secret : string;
(* Slack uses this secret to sign requests; provide to verify incoming Slack requests *)
?slack_signing_secret : string option;
(* if specified, maintains state b/w OAuth request and callback to prevent CSRF
(* if specified, maintains state b/w OAuth request and callback to prevent CSRF
see: https://tools.ietf.org/html/rfc6749#section-4.1.1 *)
?slack_oauth_state : string option;
(* Slack bot token obtained via OAuth, enabling message posting to the workspace;
Expand Down
34 changes: 17 additions & 17 deletions lib/slack.atd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type notification_field = {
type message_field = {
?title: string nullable;
value: string;
}

type notification_attachment = {
type message_attachment = {
fallback: string nullable;
?mrkdwn_in: string list nullable;
?color: string nullable;
Expand All @@ -14,18 +14,18 @@ type notification_attachment = {
?title: string nullable;
?title_link: string nullable;
?text: string nullable;
?fields: notification_field list nullable;
?fields: message_field list nullable;
?image_url: string nullable;
?thumb_url: string nullable;
?ts: int nullable;
?footer: string nullable;
}

type notification_section_block_type = [
type message_section_block_type = [
Section <json name="section">
] <ocaml repr="classic">

type notification_divider_block_type = [
type message_divider_block_type = [
Divider <json name="divider">
] <ocaml repr="classic">

Expand All @@ -39,25 +39,25 @@ type text_object = {
text: string;
}

type notification_text_block = {
notification_type <json name="type"> : notification_section_block_type;
type message_text_block = {
message_type <json name="type"> : message_section_block_type;
text: text_object;
}

type notification_divider_block = {
notification_type <json name="type"> : notification_divider_block_type;
type message_divider_block = {
message_type <json name="type"> : message_divider_block_type;
}

type notification_block = [
Text of notification_text_block
| Divider of notification_divider_block
type message_block = [
Text of message_text_block
| Divider of message_divider_block
] <json adapter.ocaml="Atdgen_runtime.Json_adapter.Type_field">

type notification = {
type message = {
channel: string;
?text: string nullable;
?attachments: notification_attachment list nullable;
?blocks: notification_block list nullable;
?attachments: message_attachment list nullable;
?blocks: message_block list nullable;
}

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

type notification_response = {
type message_response = {
ok: bool;
?channel: string option;
?error: string option;
}
}
16 changes: 9 additions & 7 deletions src/notabot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ let check_gh_action file json config secrets state =
Action.process_github_notification ctx headers body
)

let check_slack_action file config secrets =
let check_slack_action file secrets =
let data = Stdio.In_channel.read_all file in
let ctx = Context.make ~config_filename:config ~secrets_filepath:secrets () in
match Slack_j.notification_of_string data with
let ctx = Context.make ~secrets_filepath:secrets () in
match Slack_j.message_of_string data with
| exception exn -> log#error ~exn "unable to parse notification"
| notification ->
| message ->
match Context.refresh_secrets ctx with
| Error e -> log#error "%s" e
| Ok ctx ->
Lwt_main.run
( match%lwt Api_remote.Slack.send_notification ~ctx ~notification with
| Error e -> Lwt.return @@ log#error "%s" e
( match%lwt Api_remote.Slack.send_notification ~ctx ~message with
| Error e ->
log#error "%s" e;
Lwt.return_unit
| Ok () -> Lwt.return_unit
)

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

let default_cmd =
Expand Down
1 change: 0 additions & 1 deletion test/slack_oauth_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ let secrets_with_state = { secrets_default with slack_oauth_state = Some "foo" }

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

(* name, args, headers, body *)
let tests =
[
"success, valid params", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_with_key;
Expand Down

0 comments on commit 6e6ae96

Please sign in to comment.