Skip to content

Commit 7d37df5

Browse files
committed
use the new notification sending interface throughout the codebase
1 parent a91e58c commit 7d37df5

File tree

3 files changed

+39
-51
lines changed

3 files changed

+39
-51
lines changed

lib/action.ml

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,41 +152,29 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
152152
let cfg = Context.get_config_exn ctx in
153153
match req with
154154
| Github.Push n ->
155-
partition_push cfg n |> List.map ~f:(fun (webhook, n) -> webhook, generate_push_notification n) |> Lwt.return
156-
| Pull_request n ->
157-
partition_pr cfg n |> List.map ~f:(fun webhook -> webhook, generate_pull_request_notification n) |> Lwt.return
158-
| PR_review n ->
159-
partition_pr_review cfg n |> List.map ~f:(fun webhook -> webhook, generate_pr_review_notification n) |> Lwt.return
155+
partition_push cfg n |> List.map ~f:(fun (channel, n) -> generate_push_notification n channel) |> Lwt.return
156+
| Pull_request n -> partition_pr cfg n |> List.map ~f:(generate_pull_request_notification n) |> Lwt.return
157+
| PR_review n -> partition_pr_review cfg n |> List.map ~f:(generate_pr_review_notification n) |> Lwt.return
160158
| PR_review_comment n ->
161-
partition_pr_review_comment cfg n
162-
|> List.map ~f:(fun webhook -> webhook, generate_pr_review_comment_notification n)
163-
|> Lwt.return
164-
| Issue n ->
165-
partition_issue cfg n |> List.map ~f:(fun webhook -> webhook, generate_issue_notification n) |> Lwt.return
159+
partition_pr_review_comment cfg n |> List.map ~f:(generate_pr_review_comment_notification n) |> Lwt.return
160+
| Issue n -> partition_issue cfg n |> List.map ~f:(generate_issue_notification n) |> Lwt.return
166161
| Issue_comment n ->
167-
partition_issue_comment cfg n
168-
|> List.map ~f:(fun webhook -> webhook, generate_issue_comment_notification n)
169-
|> Lwt.return
162+
partition_issue_comment cfg n |> List.map ~f:(generate_issue_comment_notification n) |> Lwt.return
170163
| Commit_comment n ->
171-
let%lwt webhooks, api_commit = partition_commit_comment ctx n in
172-
let%lwt notif = generate_commit_comment_notification api_commit n in
173-
let notifs = List.map ~f:(fun webhook -> webhook, notif) webhooks in
164+
let%lwt channels, api_commit = partition_commit_comment ctx n in
165+
let notifs = List.map ~f:(generate_commit_comment_notification api_commit n) channels in
174166
Lwt.return notifs
175167
| Status n ->
176-
let%lwt webhooks = partition_status ctx n in
177-
let notifs = List.map ~f:(fun webhook -> webhook, generate_status_notification cfg n) webhooks in
168+
let%lwt channels = partition_status ctx n in
169+
let notifs = List.map ~f:(generate_status_notification cfg n) channels in
178170
Lwt.return notifs
179171
| _ -> Lwt.return []
180172

181173
let send_notifications (ctx : Context.t) notifications =
182-
let notify (chan, msg) =
183-
match Context.hook_of_channel ctx chan with
184-
| None -> Printf.ksprintf action_error "webhook not defined for Slack channel '%s'" chan
185-
| Some url ->
186-
( match%lwt Slack_api.send_notification ~chan ~msg ~url with
187-
| Ok () -> Lwt.return_unit
188-
| Error e -> action_error e
189-
)
174+
let notify (msg : Slack_t.post_message_req) =
175+
match%lwt Slack_api.send_notification ~ctx ~msg with
176+
| Ok () -> Lwt.return_unit
177+
| Error e -> action_error e
190178
in
191179
Lwt_list.iter_s notify notifications
192180

lib/slack.ml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ open Common
55
open Github_j
66
open Slack_j
77

8-
let log = Log.from "slack"
9-
10-
type channel_hook = string
11-
128
let empty_attachments =
139
{
1410
mrkdwn_in = None;
@@ -37,7 +33,7 @@ let show_labels = function
3733
| (labels : label list) ->
3834
Some (sprintf "Labels: %s" @@ String.concat ~sep:", " (List.map ~f:(fun x -> x.name) labels))
3935

40-
let generate_pull_request_notification notification =
36+
let generate_pull_request_notification notification channel =
4137
let { action; number; sender; pull_request; repository } = notification in
4238
let ({ body; title; html_url; labels; _ } : pull_request) = pull_request in
4339
let action, body =
@@ -57,6 +53,7 @@ let generate_pull_request_notification notification =
5753
action sender.login)
5854
in
5955
{
56+
channel;
6057
text = None;
6158
attachments =
6259
Some
@@ -73,7 +70,7 @@ let generate_pull_request_notification notification =
7370
blocks = None;
7471
}
7572

76-
let generate_pr_review_notification notification =
73+
let generate_pr_review_notification notification channel =
7774
let { action; sender; pull_request; review; repository } = notification in
7875
let ({ number; title; html_url; _ } : pull_request) = pull_request in
7976
let action_str =
@@ -96,6 +93,7 @@ let generate_pr_review_notification notification =
9693
action_str number html_url title)
9794
in
9895
{
96+
channel;
9997
text = None;
10098
attachments =
10199
Some
@@ -112,7 +110,7 @@ let generate_pr_review_notification notification =
112110
blocks = None;
113111
}
114112

115-
let generate_pr_review_comment_notification notification =
113+
let generate_pr_review_comment_notification notification channel =
116114
let { action; pull_request; sender; comment; repository } = notification in
117115
let ({ number; title; html_url; _ } : pull_request) = pull_request in
118116
let action_str =
@@ -134,6 +132,7 @@ let generate_pr_review_comment_notification notification =
134132
| Some a -> Some (sprintf "New comment by %s in <%s|%s>" sender.login comment.html_url a)
135133
in
136134
{
135+
channel;
137136
text = None;
138137
attachments =
139138
Some
@@ -151,7 +150,7 @@ let generate_pr_review_comment_notification notification =
151150
blocks = None;
152151
}
153152

154-
let generate_issue_notification notification =
153+
let generate_issue_notification notification channel =
155154
let ({ action; sender; issue; repository } : issue_notification) = notification in
156155
let { number; body; title; html_url; labels; _ } = issue in
157156
let action, body =
@@ -171,6 +170,7 @@ let generate_issue_notification notification =
171170
sender.login)
172171
in
173172
{
173+
channel;
174174
text = None;
175175
attachments =
176176
Some
@@ -187,7 +187,7 @@ let generate_issue_notification notification =
187187
blocks = None;
188188
}
189189

190-
let generate_issue_comment_notification notification =
190+
let generate_issue_comment_notification notification channel =
191191
let { action; issue; sender; comment; repository } = notification in
192192
let { number; title; _ } = issue in
193193
let action_str =
@@ -205,6 +205,7 @@ let generate_issue_comment_notification notification =
205205
action_str number issue.html_url title)
206206
in
207207
{
208+
channel;
208209
text = None;
209210
attachments =
210211
Some
@@ -223,7 +224,7 @@ let generate_issue_comment_notification notification =
223224

224225
let git_short_sha_hash hash = String.sub ~pos:0 ~len:8 hash
225226

226-
let generate_push_notification notification =
227+
let generate_push_notification notification channel =
227228
let { sender; created; deleted; forced; compare; commits; repository; _ } = notification in
228229
let commits_branch = Github.commits_branch_of_ref notification.ref in
229230
let tree_url = String.concat ~sep:"/" [ repository.url; "tree"; Uri.pct_encode commits_branch ] in
@@ -247,6 +248,7 @@ let generate_push_notification notification =
247248
sprintf "`<%s|%s>` %s - %s" url (git_short_sha_hash id) title author.name)
248249
in
249250
{
251+
channel;
250252
text = Some title;
251253
attachments =
252254
Some
@@ -262,7 +264,7 @@ let generate_push_notification notification =
262264
blocks = None;
263265
}
264266

265-
let generate_status_notification (cfg : Config_t.config) (notification : status_notification) =
267+
let generate_status_notification (cfg : Config_t.config) (notification : status_notification) channel =
266268
let { commit; state; description; target_url; context; repository; _ } = notification in
267269
let ({ commit : inner_commit; sha; author; html_url; _ } : status_commit) = commit in
268270
let ({ message; _ } : inner_commit) = commit in
@@ -324,9 +326,9 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
324326
fields = Some [ { title = None; value = String.concat ~sep:"\n" @@ List.concat [ commit_info; branches_info ] } ];
325327
}
326328
in
327-
{ text = None; attachments = Some [ attachment ]; blocks = None }
329+
{ channel; text = None; attachments = Some [ attachment ]; blocks = None }
328330

329-
let generate_commit_comment_notification api_commit notification =
331+
let generate_commit_comment_notification api_commit notification channel =
330332
let { commit; _ } = api_commit in
331333
let { sender; comment; repository; _ } = notification in
332334
let commit_id =
@@ -355,5 +357,4 @@ let generate_commit_comment_notification api_commit notification =
355357
text = Some (mrkdwn_of_markdown comment.body);
356358
}
357359
in
358-
let notifs = { text = None; attachments = Some [ attachment ]; blocks = None } in
359-
Lwt.return notifs
360+
{ channel; text = None; attachments = Some [ attachment ]; blocks = None }

src/monorobot.ml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ let check_gh_action file json config secrets state =
4343
)
4444
)
4545

46-
let check_slack_action url file =
46+
let check_slack_action file secrets =
4747
let data = Stdio.In_channel.read_all file in
48-
let chan = Printf.sprintf "webhook %s" url in
48+
let ctx = Context.make ~secrets_filepath:secrets () in
4949
match Slack_j.post_message_req_of_string data with
5050
| exception exn -> log#error ~exn "unable to parse notification"
5151
| msg ->
52+
match Context.refresh_secrets ctx with
53+
| Error e -> log#error "%s" e
54+
| Ok ctx ->
5255
Lwt_main.run
53-
( match%lwt Api_remote.Slack.send_notification ~chan ~msg ~url with
56+
( match%lwt Api_remote.Slack.send_notification ~ctx ~msg with
5457
| Error e ->
5558
log#error "%s" e;
5659
Lwt.return_unit
@@ -86,13 +89,9 @@ let gh_payload =
8689
let doc = "path to a JSON file containing a github webhook payload" in
8790
Arg.(required & pos 0 (some file) None & info [] ~docv:"GH_PAYLOAD" ~doc)
8891

89-
let slack_webhook_url =
90-
let doc = "slack webhook url" in
91-
Arg.(required & pos 0 (some string) None & info [] ~docv:"SLACK_WEBHOOK" ~doc)
92-
9392
let slack_payload =
9493
let doc = "path to a JSON file containing a slack notification payload" in
95-
Arg.(required & pos 1 (some file) None & info [] ~docv:"SLACK_PAYLOAD" ~doc)
94+
Arg.(required & pos 0 (some file) None & info [] ~docv:"SLACK_PAYLOAD" ~doc)
9695

9796
let json =
9897
let doc = "if set, will format output as json" in
@@ -113,9 +112,9 @@ let check_gh =
113112
term, info
114113

115114
let check_slack =
116-
let doc = "read a Slack notification from a file and send it to a webhook; used for testing" in
115+
let doc = "read a Slack notification from a file and send it to a channel; used for testing" in
117116
let info = Term.info "check_slack" ~doc in
118-
let term = Term.(const check_slack_action $ slack_webhook_url $ slack_payload) in
117+
let term = Term.(const check_slack_action $ slack_payload $ secrets) in
119118
term, info
120119

121120
let default_cmd =

0 commit comments

Comments
 (0)