File tree Expand file tree Collapse file tree 4 files changed +51
-15
lines changed Expand file tree Collapse file tree 4 files changed +51
-15
lines changed Original file line number Diff line number Diff line change @@ -61,11 +61,5 @@ let write_to_local_file ~data path =
61
61
try Ok (Devkit.Files. save_as path (fun oc -> Printf. fprintf oc " %s" data))
62
62
with exn -> fmt_error " %s" (Exn. to_string exn )
63
63
64
- let longest_common_prefix xs =
65
- match xs with
66
- | [] -> " "
67
- | [ x ] -> x
68
- | x :: _ -> List. sort (Fun. flip String. compare) xs |> List. hd |> Stre. common_prefix x |> String. sub x 0
69
-
70
64
let sign_string_sha256 ~key ~basestring =
71
65
Cstruct. of_string basestring |> Nocrypto.Hash.SHA256. hmac ~key: (Cstruct. of_string key) |> Hex. of_cstruct |> Hex. show
Original file line number Diff line number Diff line change @@ -133,16 +133,16 @@ let month = function
133
133
let condense_file_changes files =
134
134
match files with
135
135
| [ f ] -> sprintf " _modified `%s` (+%d-%d)_" (escape_mrkdwn f.filename) f.additions f.deletions
136
- | _ ->
137
- let rec drop_last = function
138
- | [ _ ] | [] -> [] (* Should raise when empty instead? *)
139
- | v :: vs -> v :: drop_last vs
136
+ | [] -> " _no files modified_"
137
+ | first_file :: fl ->
138
+ let rec longest_prefix_of_two_lists l1 l2 =
139
+ match l1, l2 with
140
+ | e1 :: l1' , e2 :: l2' when e1 = e2 -> e1 :: longest_prefix_of_two_lists l1' l2'
141
+ | _ -> []
140
142
in
141
143
let prefix_path =
142
- List. map (fun f -> f.filename) files
143
- |> Common. longest_common_prefix
144
- |> String. split_on_char '/'
145
- |> drop_last
144
+ List. map (fun f -> String. split_on_char '/' f.filename) fl
145
+ |> List. fold_left longest_prefix_of_two_lists (String. split_on_char '/' first_file.filename)
146
146
|> String. concat " /"
147
147
in
148
148
sprintf " modified %d files%s" (List. length files) (if prefix_path = " " then " " else sprintf " in `%s/`" prefix_path)
Original file line number Diff line number Diff line change 1
1
(executables
2
- (names test github_link_test)
2
+ (names test github_link_test longest_prefix_test )
3
3
(libraries lib devkit devkit.core extlib lwt.unix
4
4
yojson)
5
5
(preprocess
27
27
(alias runtest)
28
28
(action
29
29
(run ./github_link_test.exe)))
30
+
31
+ (rule
32
+ (alias runtest)
33
+ (action
34
+ (run ./longest_prefix_test.exe)))
Original file line number Diff line number Diff line change
1
+ open Lib
2
+ open Github_t
3
+ open Slack_message
4
+
5
+ let make_test_file filename =
6
+ {
7
+ sha = " something something" ;
8
+ filename;
9
+ status = " something something" ;
10
+ additions = 5 ;
11
+ deletions = 5 ;
12
+ changes = 10 ;
13
+ url = " test_link" ;
14
+ }
15
+
16
+ let single_file = [ make_test_file " testdir1/testdir2/changed_file.txt" ]
17
+
18
+ let multiple_files_same_dir =
19
+ List. map make_test_file
20
+ [
21
+ " testdir1/testdir2/changed_file2.txt" ;
22
+ " testdir1/testdir2/changed_file3.txt" ;
23
+ " testdir1/testdir2/changed_file1.txt" ;
24
+ ]
25
+
26
+ let multiple_files_common_root_dir =
27
+ List. map make_test_file [ " testdir1/changed_file2.txt" ; " backend/changed_file3.txt" ; " changed_file1.txt" ]
28
+
29
+ let multiple_files_common_dir =
30
+ List. map make_test_file
31
+ [ " backend/something/changed_file2.txt" ; " backend/something/changed_file3.txt" ; " backend/some/changed_file1.txt" ]
32
+
33
+ let () =
34
+ assert (condense_file_changes single_file = " _modified `testdir1/testdir2/changed_file.txt` (+5-5)_" );
35
+ assert (condense_file_changes multiple_files_same_dir = " modified 3 files in `testdir1/testdir2/`" );
36
+ assert (condense_file_changes multiple_files_common_root_dir = " modified 3 files" );
37
+ assert (condense_file_changes multiple_files_common_dir = " modified 3 files in `backend/`" )
You can’t perform that action at this time.
0 commit comments