Skip to content

Commit 59458ee

Browse files
committed
add logic for building message body of unfurled commit links
The new file `slack_message.ml` should only contain generation logic for messages that describe the state of some info on GH (e.g., a description of a PR), and *not* an action taken on GH (e.g, "XYZ opened a PR". This decoupling is desired because we eventually want to reuse code between link unfurl generation and event notification generation, and link unfurl only concerns the former type of info described above. Currently, it's difficult to do as there is tight coupling in `slack.ml`. Currently we support commit link unfurls. We can later easily expand the selection of unfurlable links by adding onto `slack_message.ml`.
1 parent ce68f5f commit 59458ee

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

lib/colors.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(* https://styleguide.github.com/primer/utilities/colors/#background-colors *)
2+
3+
let gray = "#f6f8fa"
4+
5+
let blue = "#0366d6"
6+
7+
let yellow = "#ffd33d"
8+
9+
let red = "#d73a49"
10+
11+
let green = "#28a745"
12+
13+
let purple = "#6f42c1"

lib/github.atd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ type commit = {
2424
type github_user = {
2525
login: string;
2626
id: int;
27+
url: string;
28+
html_url: string;
29+
avatar_url: string;
2730
}
2831

2932
type repository = {
@@ -218,12 +221,19 @@ type file = {
218221
blob_url <ocaml name="url"> : string;
219222
}
220223

224+
type api_commit_stats = {
225+
total: int;
226+
additions: int;
227+
deletions: int;
228+
}
229+
221230
type api_commit = {
222231
sha: commit_hash;
223232
commit: inner_commit;
224233
html_url <ocaml name="url"> : string;
225234
author: github_user;
226235
files: file list;
236+
stats: api_commit_stats;
227237
}
228238

229239
type commit_comment_notification = {

lib/slack.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
open Printf
22
open Base
3-
open Devkit
43
open Common
54
open Github_j
65
open Slack_j
@@ -33,6 +32,8 @@ let show_labels = function
3332
| (labels : label list) ->
3433
Some (sprintf "Labels: %s" @@ String.concat ~sep:", " (List.map ~f:(fun x -> x.name) labels))
3534

35+
let pluralize name num suffix = if num = 1 then sprintf "%s" name else String.concat [ name; suffix ]
36+
3637
let generate_pull_request_notification notification channel =
3738
let { action; number; sender; pull_request; repository } = notification in
3839
let ({ body; title; html_url; labels; _ } : pull_request) = pull_request in
@@ -302,8 +303,7 @@ let generate_status_notification (cfg : Config_t.config) (notification : status_
302303
[ main ]
303304
| _ -> notification_branches
304305
in
305-
let pluralize s = if Int.equal (List.length branches) 1 then s else sprintf "%ses" s in
306-
[ sprintf "*%s*: %s" (pluralize "Branch") (String.concat ~sep:", " branches) ]
306+
[ sprintf "*%s*: %s" (pluralize "Branch" (List.length branches) "es") (String.concat ~sep:", " branches) ]
307307
in
308308
let summary =
309309
match target_url with

lib/slack_message.ml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
open Base
2+
open Printf
3+
open Github_t
4+
open Slack_t
5+
6+
let git_short_sha_hash hash = String.sub ~pos:0 ~len:8 hash
7+
8+
let empty_attachment =
9+
{
10+
mrkdwn_in = None;
11+
fallback = None;
12+
color = None;
13+
pretext = None;
14+
author_name = None;
15+
author_link = None;
16+
author_icon = None;
17+
title = None;
18+
title_link = None;
19+
text = None;
20+
fields = None;
21+
image_url = None;
22+
thumb_url = None;
23+
ts = None;
24+
footer = None;
25+
}
26+
27+
let base_attachment (repository : repository) =
28+
{ empty_attachment with footer = Some (sprintf "<%s|%s>" repository.url repository.full_name) }
29+
30+
let pp_file (file : file) = sprintf "<%s|%s>" file.url (Mrkdwn.escape_mrkdwn file.filename)
31+
32+
let populate_commit repository (commit : api_commit) =
33+
let ({ sha; commit; url; author; files; stats } : api_commit) = commit in
34+
let get_files () = List.map files ~f:pp_file in
35+
let title = sprintf "`<%s|%s>` *%s - %s*" url (git_short_sha_hash sha) commit.message commit.author.name in
36+
let num_change = List.length files in
37+
let changes =
38+
sprintf "%d %s with %d %s and %d %s:" num_change
39+
(Slack.pluralize "changed file" num_change "s")
40+
stats.additions
41+
(Slack.pluralize "addition" stats.additions "s")
42+
stats.deletions
43+
(Slack.pluralize "deletion" stats.deletions "s")
44+
in
45+
let files = get_files () |> String.concat ~sep:"\n" in
46+
let text = sprintf "%s\n%s\n%s" title changes files in
47+
let fallback = sprintf "[%s] %s - %s" (git_short_sha_hash sha) commit.message commit.author.name in
48+
{
49+
(base_attachment repository) with
50+
author_name = Some author.login;
51+
author_link = Some author.html_url;
52+
author_icon = Some author.avatar_url;
53+
color = Some Colors.gray;
54+
mrkdwn_in = Some [ "text" ];
55+
text = Some text;
56+
fallback = Some fallback;
57+
}

0 commit comments

Comments
 (0)