From 783d27e2d3605d1499c49e70b09b1c4d47e80899 Mon Sep 17 00:00:00 2001 From: Emile Trotignon Date: Wed, 26 Feb 2025 15:04:52 +0000 Subject: [PATCH] remove webhook mode --- lib/action.ml | 17 ++++++++--------- lib/api.ml | 2 +- lib/api_local.ml | 8 +++++--- lib/api_remote.ml | 24 ++++++++---------------- src/monorobot.ml | 5 ++--- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/lib/action.ml b/lib/action.ml index 787e5020..b9e2def2 100644 --- a/lib/action.ml +++ b/lib/action.ml @@ -440,16 +440,15 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) (Buildkite_api : let send_notifications (ctx : Context.t) notifications = let notify (msg, handler) = match%lwt Slack_api.send_notification ~ctx ~msg with - | Ok (Some res) -> - (match handler with - | None -> Lwt.return_unit - | Some handler -> - (match%lwt handler res with - | Result.Error e -> handler_error e - | Ok () -> Lwt.return_unit - | exception exn -> handler_error (Printexc.to_string exn))) - | Ok None -> Lwt.return_unit | Error e -> action_error e + | Ok res -> + match handler with + | None -> Lwt.return_unit + | Some handler -> + (match%lwt handler res with + | Result.Error e -> handler_error e + | Ok () -> Lwt.return_unit + | exception exn -> handler_error (Printexc.to_string exn)) in Lwt_list.iter_s notify notifications diff --git a/lib/api.ml b/lib/api.ml index eb43ef57..378cf5fc 100644 --- a/lib/api.ml +++ b/lib/api.ml @@ -23,7 +23,7 @@ module type Slack = sig lookup_user_res slack_response Lwt.t val list_users : ?cursor:string -> ?limit:int -> ctx:Context.t -> unit -> list_users_res slack_response Lwt.t - val send_notification : ctx:Context.t -> msg:post_message_req -> post_message_res option slack_response Lwt.t + val send_notification : ctx:Context.t -> msg:post_message_req -> post_message_res slack_response Lwt.t val send_chat_unfurl : ctx:Context.t -> diff --git a/lib/api_local.ml b/lib/api_local.ml index 7071a1b8..987eddb8 100644 --- a/lib/api_local.ml +++ b/lib/api_local.ml @@ -107,7 +107,9 @@ module Slack_simple : Api.Slack = struct (match msg.Slack_t.text with | None -> "" | Some s -> sprintf " with %S" s); - Lwt.return @@ Ok None + let channel = Slack_channel.Ident.inject (Slack_channel.Any.project msg.channel) in + let res = ({ Slack_t.channel; ts = Slack_timestamp.inject "mock_ts" } : Slack_t.post_message_res) in + Lwt.return_ok res let send_file ~ctx:_ ~(file : Slack.file_req) = let json = string_of_file_req file in @@ -147,7 +149,7 @@ module Slack_json : Api.Slack = struct log#info "%s" json; let channel = Slack_channel.Ident.inject (Slack_channel.Any.project msg.channel) in let res = ({ Slack_t.channel; ts = Slack_timestamp.inject "mock_ts" } : Slack_t.post_message_res) in - Lwt.return_ok (Some res) + Lwt.return_ok res let send_file ~ctx:_ ~(file : Slack.file_req) = let json = string_of_file_req file in @@ -190,7 +192,7 @@ module Slack : Api.Slack = struct Printf.printf "%s\n" json; let channel = Slack_channel.Ident.inject (Slack_channel.Any.project msg.channel) in let res = ({ Slack_t.channel; ts = Slack_timestamp.inject "mock_ts" } : Slack_t.post_message_res) in - Lwt.return @@ Ok (Some res) + Lwt.return @@ Ok res let send_file ~ctx:_ ~(file : Slack.file_req) = let json = string_of_file_req file in diff --git a/lib/api_remote.ml b/lib/api_remote.ml index 24b8893e..3acb0c38 100644 --- a/lib/api_remote.ml +++ b/lib/api_remote.ml @@ -171,13 +171,13 @@ module Slack : Api.Slack = struct log#info "sending to %s" (Slack_channel.Any.project msg.channel); let build_error e = sprintf "%s\nfailed to send Slack notification" e in let secrets = Context.get_secrets_exn ctx in - let headers, url, webhook_mode = + let headers, url = match Context.hook_of_channel ctx msg.channel with - | Some url -> [], Some url, true + | Some url -> [], Some url | None -> match secrets.slack_access_token with - | Some access_token -> [ bearer_token_header access_token ], Some "https://slack.com/api/chat.postMessage", false - | None -> [], None, false + | Some access_token -> [ bearer_token_header access_token ], Some "https://slack.com/api/chat.postMessage" + | None -> [], None in match url with | None -> @@ -189,18 +189,10 @@ module Slack : Api.Slack = struct let data = Slack_j.string_of_post_message_req msg in let body = `Raw ("application/json", data) in log#info "data: %s" data; - if webhook_mode then begin - let* _res = - http_request ~body ~headers `POST url |> Lwt_result.map_error (fun e -> build_error (query_error_msg url e)) - in - Lwt.return @@ Ok None - end - else begin - let* res = - slack_api_request ~body ~headers `POST url Slack_j.read_post_message_res |> Lwt_result.map_error build_error - in - Lwt.return @@ Ok (Some res) - end + let* res = + slack_api_request ~body ~headers `POST url Slack_j.read_post_message_res |> Lwt_result.map_error build_error + in + Lwt.return @@ Ok res let send_chat_unfurl ~(ctx : Context.t) ~channel ~ts ~unfurls () = let req = Slack_j.{ channel; ts; unfurls } in diff --git a/src/monorobot.ml b/src/monorobot.ml index 209da5e2..73b14f3d 100644 --- a/src/monorobot.ml +++ b/src/monorobot.ml @@ -74,10 +74,9 @@ let check_slack_action file secrets = | Error e -> log#error "%s" e; Lwt.return_unit - | Ok (Some res : Slack_t.post_message_res option) -> + | Ok (res : Slack_t.post_message_res) -> log#info "%s" (Slack_j.string_of_post_message_res res); - Lwt.return_unit - | Ok None -> Lwt.return_unit) + Lwt.return_unit) (* flags *)