Skip to content

Commit e502d3a

Browse files
committed
slack & slack_msg: reuse push commit notification truncation for compare
1 parent 479ac73 commit e502d3a

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/slack.ml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ let generate_issue_comment_notification notification channel =
216216

217217
let git_short_sha_hash hash = String.sub ~pos:0 ~len:8 hash
218218

219+
(** pretty print github commit *)
220+
let pp_commit ({ url; id; message; author; _ } : commit) =
221+
let title = first_line message in
222+
sprintf "`<%s|%s>` %s - %s" url (git_short_sha_hash id) title author.name
223+
224+
(** pretty print list with previews of each item per line--will always show at most 7 and drop the rest*)
225+
let pp_list_with_previews ~pp_item list =
226+
let num_items = List.length list in
227+
(* truncation point depends on line length, but 7+3 lines seems okay for most cases *)
228+
let num_shown = 7 in
229+
let dropped = num_items - num_shown in
230+
if dropped > 3 then begin
231+
let h, list' = List.split_n list (num_shown / 2) in
232+
let t = List.drop list' dropped in
233+
let num_after_h = num_items - List.length h in
234+
List.concat [ List.map ~f:pp_item h; [ sprintf "+%d more...\n" num_after_h ]; List.map ~f:pp_item t ]
235+
end
236+
else List.map ~f:pp_item list
237+
219238
let generate_push_notification notification channel =
220239
let { sender; created; deleted; forced; compare; commits; repository; _ } = notification in
221240
let commits_branch = Github.commits_branch_of_ref notification.ref in
@@ -241,21 +260,7 @@ let generate_push_notification notification channel =
241260
(if created then "to new branch " else "")
242261
sender.login
243262
in
244-
let commits =
245-
let pp_commit { url; id; message; author; _ } =
246-
let title = first_line message in
247-
sprintf "`<%s|%s>` %s - %s" url (git_short_sha_hash id) title author.name
248-
in
249-
(* truncation point depends on line length, but 7+3 lines seems okay for most cases *)
250-
let num_shown = 7 in
251-
let dropped = num_commits - num_shown in
252-
if dropped > 3 then begin
253-
let h, commits' = List.split_n commits (num_shown / 2) in
254-
let t = List.drop commits' dropped in
255-
List.concat [ List.map ~f:pp_commit h; [ sprintf "+%d more..." dropped ]; List.map ~f:pp_commit t ]
256-
end
257-
else List.map commits ~f:pp_commit
258-
in
263+
let commits = pp_list_with_previews ~pp_item:pp_commit commits in
259264
{
260265
channel;
261266
text = Some title;

lib/slack_message.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ let populate_compare repository (compare : compare) =
228228
| false ->
229229
let commits_unfurl = List.map compare.commits ~f:(populate_commit ~include_changes:false repository) in
230230
let commits_unfurl_text =
231-
List.map commits_unfurl ~f:(fun commit_unfurl -> Option.value commit_unfurl.text ~default:"")
231+
Slack.pp_list_with_previews
232+
~pp_item:(fun (commit_unfurl : unfurl) -> Option.value commit_unfurl.text ~default:"")
233+
commits_unfurl
232234
in
235+
233236
let commits_unfurl_fallback =
234237
List.map commits_unfurl ~f:(fun commit_unfurl -> Option.value commit_unfurl.fallback ~default:"")
235238
in

0 commit comments

Comments
 (0)