Skip to content

Commit 6d3d5d9

Browse files
committed
slack: show date better
1 parent 5bdf24c commit 6d3d5d9

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lib/slack_message.ml

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ let populate_issue repository (issue : issue) =
116116
fallback = Some (sprintf "[%s] %s" repository.full_name title);
117117
}
118118

119+
(* use some date library :see_no_evil: *)
120+
let month = function
121+
| 1 -> "Jan"
122+
| 2 -> "Feb"
123+
| 3 -> "Mar"
124+
| 4 -> "Apr"
125+
| 5 -> "May"
126+
| 6 -> "Jun"
127+
| 7 -> "Jul"
128+
| 8 -> "Aug"
129+
| 9 -> "Sep"
130+
| 10 -> "Oct"
131+
| 11 -> "Nov"
132+
| 12 -> "Dec"
133+
| _ -> assert false
134+
119135
let populate_commit repository (commit : api_commit) =
120136
let ({ sha; commit; url; author; files; _ } : api_commit) = commit in
121137
let title =
@@ -135,9 +151,28 @@ let populate_commit repository (commit : api_commit) =
135151
|> String.concat ~sep:"/"
136152
in
137153
let where = if String.is_empty prefix_path then "" else sprintf " in `%s/`" prefix_path in
138-
(* TODO use "today" on same day, "Month Day" during same year
139-
even better would be to have "N units ago" and tooltip, but looks like slack doesn't provide such thing *)
140-
sprintf "modified %d files%s on %s" (List.length files) where commit.author.date
154+
let when_ =
155+
(*
156+
use "today" on same day, "Month Day" during same year
157+
even better would be to have "N units ago" and tooltip computed by slack at time of presentation
158+
but looks like slack doesn't provide such functionality
159+
*)
160+
try
161+
match String.split commit.author.date ~on:'T' with
162+
| [ date; _ ] ->
163+
let yy, mm, dd =
164+
let tm = Unix.gmtime @@ Devkit.Time.now () in
165+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday
166+
in
167+
( match List.map ~f:Int.of_string @@ String.split date ~on:'-' with
168+
| [ y; m; d ] when y = yy && m = mm && d = dd -> "today"
169+
| [ y; m; d ] when y = yy -> sprintf " on %s %d" (month m) d
170+
| _ -> date
171+
)
172+
| _ -> failwith "wut"
173+
with _ -> " on " ^ commit.author.date
174+
in
175+
sprintf "modified %d files%s %s" (List.length files) where when_
141176
in
142177
let text = sprintf "%s\n%s" title changes in
143178
let fallback = sprintf "[%s] %s - %s" (Slack.git_short_sha_hash sha) commit.message commit.author.name in

0 commit comments

Comments
 (0)