Skip to content

Commit 98404d1

Browse files
committed
tests, api, slack_messages: standardize github cache file for testing, and cleaned up populate compare unfurling
1 parent 4d4359a commit 98404d1

13 files changed

+132
-79
lines changed

lib/action.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
337337
| Ok commit -> Lwt.return_some @@ (link, Slack_message.populate_commit repo commit)
338338
)
339339
| Compare (repo, (base, merge)) ->
340-
let basehead = Printf.sprintf "%s...%s" base merge in
341-
( match%lwt Github_api.get_compare ~ctx ~repo ~basehead with
340+
( match%lwt Github_api.get_compare ~ctx ~repo ~basehead:(base, merge) with
342341
| Error _ -> Lwt.return_none
343342
| Ok compare -> Lwt.return_some @@ (link, Slack_message.populate_compare repo compare)
344343
)

lib/api.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module type Github = sig
88
val get_api_commit : ctx:Context.t -> repo:repository -> sha:string -> (api_commit, string) Result.t Lwt.t
99
val get_pull_request : ctx:Context.t -> repo:repository -> number:int -> (pull_request, string) Result.t Lwt.t
1010
val get_issue : ctx:Context.t -> repo:repository -> number:int -> (issue, string) Result.t Lwt.t
11-
val get_compare : ctx:Context.t -> repo:repository -> basehead:string -> (compare, string) Result.t Lwt.t
11+
val get_compare : ctx:Context.t -> repo:repository -> basehead:Github.basehead -> (compare, string) Result.t Lwt.t
1212
val get_release_tag : ctx:Context.t -> repo:repository -> release_tag:string -> (release_tag, string) Result.t Lwt.t
1313

1414
val request_reviewers

lib/api_local.ml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,50 @@ let cache_dir = Caml.Filename.concat cwd "github-api-cache"
88

99
(** return the file with a function f applied unless the file is empty;
1010
empty file:this is needed to simulate 404 returns from github *)
11-
let get_cache_file_f url f =
11+
let with_cache_file url f =
1212
match get_local_file url with
1313
| Error e ->
1414
let err_msg = sprintf "error while getting local file: %s\ncached for url: %s" e url in
1515
Stdio.print_endline err_msg;
16-
Lwt.return @@ Error err_msg
17-
| Ok "" -> Lwt.return @@ Error "empty file"
18-
| Ok file -> Lwt.return @@ Ok (f file)
16+
Lwt.return_error err_msg
17+
| Ok "" -> Lwt.return_error "empty file"
18+
| Ok file -> Lwt.return_ok (f file)
1919

2020
let clean_forward_slashes = String.substr_replace_all ~pattern:"/" ~with_:"_"
2121

22+
(** get a member of the repo cached API call providing
23+
the member kind (pull, issue, commit, compare, etc),
24+
_ref (pr number, issue number, commit sha, compare basehead, etc),
25+
and its Github_j.<kind>_of_string function.
26+
NB: please save the cache file in the same format *)
27+
let get_repo_member_cache ~(repo : Github_t.repository) ~kind ~_ref ~of_string =
28+
let file = clean_forward_slashes (sprintf "%s_%s_%s" repo.full_name kind _ref) in
29+
let url = Caml.Filename.concat cache_dir file in
30+
with_cache_file url of_string
31+
2232
module Github : Api.Github = struct
2333
let get_config ~(ctx : Context.t) ~repo:_ =
2434
let url = Caml.Filename.concat cwd ctx.config_filename in
25-
get_cache_file_f url Config_j.config_of_string
35+
with_cache_file url Config_j.config_of_string
2636

2737
let get_branch ~ctx:_ ~(repo : Github_t.repository) ~name =
28-
let repo_branch = clean_forward_slashes (sprintf "%s_branch_%s" repo.full_name name) in
29-
let url = Caml.Filename.concat cache_dir repo_branch in
30-
get_cache_file_f url Github_j.branch_of_string
38+
get_repo_member_cache ~repo ~kind:"branch" ~_ref:name ~of_string:Github_j.branch_of_string
3139

32-
let get_api_commit ~ctx:_ ~repo:_ ~sha =
33-
let url = Caml.Filename.concat cache_dir sha in
34-
get_cache_file_f url Github_j.api_commit_of_string
40+
let get_api_commit ~ctx:_ ~repo ~sha =
41+
get_repo_member_cache ~repo ~kind:"commit" ~_ref:sha ~of_string:Github_j.api_commit_of_string
3542

3643
let get_pull_request ~ctx:_ ~(repo : Github_t.repository) ~number =
37-
let pr = clean_forward_slashes (sprintf "%s_pull_%d" repo.full_name number) in
38-
let url = Caml.Filename.concat cache_dir pr in
39-
get_cache_file_f url Github_j.pull_request_of_string
44+
get_repo_member_cache ~repo ~kind:"pull" ~_ref:(Int.to_string number) ~of_string:Github_j.pull_request_of_string
4045

4146
let get_issue ~ctx:_ ~(repo : Github_t.repository) ~number =
42-
let issue = clean_forward_slashes (sprintf "%s_issue_%d" repo.full_name number) in
43-
let url = Caml.Filename.concat cache_dir issue in
44-
get_cache_file_f url Github_j.issue_of_string
47+
get_repo_member_cache ~repo ~kind:"issue" ~_ref:(Int.to_string number) ~of_string:Github_j.issue_of_string
4548

46-
let get_compare ~ctx:_ ~(repo : Github_t.repository) ~basehead =
47-
let compare = clean_forward_slashes (sprintf "%s_compare_%s" repo.full_name basehead) in
48-
let url = Caml.Filename.concat cache_dir compare in
49-
get_cache_file_f url Github_j.compare_of_string
49+
let get_compare ~ctx:_ ~(repo : Github_t.repository) ~basehead:(base, merge) =
50+
get_repo_member_cache ~repo ~kind:"compare" ~_ref:(sprintf "%s...%s" base merge)
51+
~of_string:Github_j.compare_of_string
5052

5153
let get_release_tag ~ctx:_ ~(repo : Github_t.repository) ~release_tag =
52-
let release_tag = clean_forward_slashes (sprintf "%s_release_tag_%s" repo.full_name release_tag) in
53-
let url = Caml.Filename.concat cache_dir release_tag in
54-
get_cache_file_f url Github_j.release_tag_of_string
54+
get_repo_member_cache ~repo ~kind:"release_tag" ~_ref:release_tag ~of_string:Github_j.release_tag_of_string
5555

5656
let request_reviewers ~ctx:_ ~repo:_ ~number:_ ~reviewers:_ = Lwt.return @@ Error "undefined for local setup"
5757
end

lib/api_remote.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module Github : Api.Github = struct
1919
let branches_url ~(repo : Github_t.repository) ~name =
2020
String.substr_replace_first ~pattern:"{/branch}" ~with_:(sprintf "/%s" name) repo.branches_url
2121

22-
let compare_url ~(repo : Github_t.repository) ~basehead =
23-
String.substr_replace_first ~pattern:"{/basehead}" ~with_:(sprintf "/%s" basehead) repo.compare_url
22+
let compare_url ~(repo : Github_t.repository) ~basehead:(base, merge) =
23+
String.substr_replace_first ~pattern:"{/basehead}" ~with_:(sprintf "/%s...%s" base merge) repo.compare_url
2424

2525
let releases_tags_url ~(repo : Github_t.repository) ~release_tag =
2626
String.substr_replace_first ~pattern:"{/release_tag}" ~with_:(sprintf "/tags/%s" release_tag) repo.releases_url
@@ -88,7 +88,6 @@ module Github : Api.Github = struct
8888
let%lwt res =
8989
compare_url ~repo ~basehead |> get_resource ~secrets:(Context.get_secrets_exn ctx) ~repo_url:repo.url
9090
in
91-
9291
Lwt.return @@ Result.map res ~f:Github_j.compare_of_string
9392

9493
let get_release_tag ~(ctx : Context.t) ~(repo : Github_t.repository) ~release_tag =

lib/github.atd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ type api_commit = {
288288
sha: commit_hash;
289289
commit: inner_commit;
290290
html_url <ocaml name="url"> : string;
291-
?author: github_user option;
291+
?author: github_user option; (* will be none if author's email is not associated with any github account *)
292292
~files <ocaml default="[]">: file list;
293293
~stats <ocaml default="{total=0; additions=0; deletions=0;}">: api_commit_stats;
294294
}

lib/github.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ let gh_link_of_string url_str =
151151
match Re2.find_submatches_exn compare_basehead_re basehead with
152152
| [| _; Some base; _; Some merge |] -> Some (Compare (repo, (base, merge)))
153153
| _ -> None
154-
with e ->
155-
let msg = Exn.to_string e in
156-
Stdio.printf "there was an error: %s\n" msg;
157-
None
154+
with Re2.Exceptions.Regex_match_failed _ -> None
158155
in
159156
begin
160157
try

lib/slack_message.ml

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ let condense_file_changes files =
145145
in
146146
if String.is_empty prefix_path then "" else sprintf " in `%s/`" prefix_path
147147

148-
let populate_commit ?(for_compare = false) repository (commit : api_commit) =
148+
let populate_commit ?(include_changes = true) repository (commit : api_commit) =
149149
let ({ sha; commit; url; author; files; _ } : api_commit) = commit in
150150
let title =
151151
match author with
@@ -158,7 +158,7 @@ let populate_commit ?(for_compare = false) repository (commit : api_commit) =
158158
(escape_mrkdwn @@ first_line commit.message)
159159
(escape_mrkdwn commit.author.name)
160160
in
161-
let changes =
161+
let changes () =
162162
let where = condense_file_changes files in
163163
let when_ =
164164
(*
@@ -184,9 +184,9 @@ let populate_commit ?(for_compare = false) repository (commit : api_commit) =
184184
sprintf "modified %d files%s %s" (List.length files) where when_
185185
in
186186
let text =
187-
match for_compare with
188-
| true -> sprintf "%s\n" title
189-
| false -> sprintf "%s\n%s" title changes
187+
match include_changes with
188+
| false -> sprintf "%s\n" title
189+
| true -> sprintf "%s\n%s" title (changes ())
190190
in
191191
let fallback = sprintf "[%s] %s - %s" (Slack.git_short_sha_hash sha) commit.message commit.author.name in
192192
{
@@ -212,55 +212,34 @@ let populate_commit ?(for_compare = false) repository (commit : api_commit) =
212212
}
213213

214214
let populate_compare repository (compare : compare) =
215-
if compare.total_commits = 0 then (
216-
let no_commit_msg = "There are no commit difference in this compare!" in
215+
let base =
217216
{
218217
(base_attachment repository) with
219218
footer = Some (simple_footer repository);
220219
author_icon = None;
221220
color = Some Colors.gray;
222221
mrkdwn_in = Some [ "text" ];
223-
text = Some no_commit_msg;
224-
fallback = Some no_commit_msg;
222+
text = None;
223+
fallback = None;
225224
}
226-
)
227-
else (
228-
let commits_unfurl = List.map compare.commits ~f:(populate_commit ~for_compare:true repository) in
225+
in
226+
match compare.total_commits = 0 with
227+
| true ->
228+
let no_commit_msg = "There are no commit difference in this compare!" in
229+
{ base with text = Some no_commit_msg; fallback = Some no_commit_msg }
230+
| false ->
231+
let commits_unfurl = List.map compare.commits ~f:(populate_commit ~include_changes:false repository) in
229232
let commits_unfurl_text =
230-
List.map commits_unfurl ~f:(fun commit_unfurl ->
231-
match commit_unfurl.text with
232-
| Some text -> text
233-
| None -> ""
234-
)
233+
List.map commits_unfurl ~f:(fun commit_unfurl -> Option.value commit_unfurl.text ~default:"")
235234
in
236-
237235
let commits_unfurl_fallback =
238-
List.map commits_unfurl ~f:(fun commit_unfurl ->
239-
match commit_unfurl.fallback with
240-
| Some fallback -> fallback
241-
| None -> ""
242-
)
236+
List.map commits_unfurl ~f:(fun commit_unfurl -> Option.value commit_unfurl.fallback ~default:"")
243237
in
244238
let file_stats =
245239
match condense_file_changes compare.files with
246240
| "" -> ""
247241
| text -> sprintf "\n\n%s" text
248242
in
249-
let repo_text = sprintf "<%s|[%s]>" (escape_mrkdwn repository.url) (escape_mrkdwn repository.name) in
250-
let total_commits_text =
251-
if compare.total_commits > 1 then
252-
sprintf "<%s|%d _commits_>" (escape_mrkdwn compare.html_url) compare.total_commits
253-
else sprintf "<%s|%d _commit_>" (escape_mrkdwn compare.html_url) compare.total_commits
254-
in
255-
let text = sprintf "%s %s:\n%s%s" repo_text total_commits_text (String.concat commits_unfurl_text) file_stats in
243+
let text = sprintf "%s%s" (String.concat commits_unfurl_text) file_stats in
256244
let fallback = String.concat commits_unfurl_fallback in
257-
{
258-
(base_attachment repository) with
259-
footer = Some (simple_footer repository);
260-
author_icon = None;
261-
color = Some Colors.gray;
262-
mrkdwn_in = Some [ "text" ];
263-
text = Some text;
264-
fallback = Some fallback;
265-
}
266-
)
245+
{ base with text = Some text; fallback = Some fallback }
File renamed without changes.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"sha": "0d95302addd66c1816bce1b1d495ed1c93ccd478",
3+
"node_id": "00000000000000000000",
4+
"commit": {
5+
"author": {
6+
"name": "Louis",
7+
"email": "[email protected]",
8+
"date": "2020-06-02T03:14:51Z"
9+
},
10+
"committer": {
11+
"name": "GitHub Enterprise",
12+
"email": "[email protected]",
13+
"date": "2020-06-02T03:14:51Z"
14+
},
15+
"message": "Update README.md",
16+
"tree": {
17+
"sha": "ee5c539cad37c77348ce7a55756acc542b41cfc7",
18+
"url": "https://github.com/api/v3/repos/ahrefs/monorepo/git/trees/ee5c539cad37c77348ce7a55756acc542b41cfc7"
19+
},
20+
"url": "https://github.com/api/v3/repos/ahrefs/monorepo/git/commits/0d95302addd66c1816bce1b1d495ed1c93ccd478",
21+
"comment_count": 0,
22+
"verification": {
23+
"verified": false,
24+
"reason": "unsigned",
25+
"signature": null,
26+
"payload": null
27+
}
28+
},
29+
"url": "https://github.com/api/v3/repos/ahrefs/monorepo/commits/0d95302addd66c1816bce1b1d495ed1c93ccd478",
30+
"html_url": "https://github.com/ahrefs/monorepo/commit/0d95302addd66c1816bce1b1d495ed1c93ccd478",
31+
"comments_url": "https://github.com/api/v3/repos/ahrefs/monorepo/commits/0d95302addd66c1816bce1b1d495ed1c93ccd478/comments",
32+
"author": {
33+
"login": "Khady",
34+
"id": 0,
35+
"node_id": "00000000000000000000",
36+
"avatar_url": "https://github.com/avatars/u/0",
37+
"gravatar_id": "",
38+
"url": "https://github.com/api/v3/users/Khady",
39+
"html_url": "https://github.com/Khady",
40+
"followers_url": "https://github.com/api/v3/users/Khady/followers",
41+
"following_url": "https://github.com/api/v3/users/Khady/following{/other_user}",
42+
"gists_url": "https://github.com/api/v3/users/Khady/gists{/gist_id}",
43+
"starred_url": "https://github.com/api/v3/users/Khady/starred{/owner}{/repo}",
44+
"subscriptions_url": "https://github.com/api/v3/users/Khady/subscriptions",
45+
"organizations_url": "https://github.com/api/v3/users/Khady/orgs",
46+
"repos_url": "https://github.com/api/v3/users/Khady/repos",
47+
"events_url": "https://github.com/api/v3/users/Khady/events{/privacy}",
48+
"received_events_url": "https://github.com/api/v3/users/Khady/received_events",
49+
"type": "User",
50+
"site_admin": false
51+
},
52+
"committer": null,
53+
"parents": [
54+
{
55+
"sha": "04cb72d6dc8d92131282a7eff57f6caf632f0a39",
56+
"url": "https://github.com/api/v3/repos/ahrefs/monorepo/commits/04cb72d6dc8d92131282a7eff57f6caf632f0a39",
57+
"html_url": "https://github.com/ahrefs/monorepo/commit/04cb72d6dc8d92131282a7eff57f6caf632f0a39"
58+
}
59+
],
60+
"stats": {
61+
"total": 2,
62+
"additions": 1,
63+
"deletions": 1
64+
},
65+
"files": [
66+
{
67+
"sha": "e7d353f786a2f29d0cf7ed6fdad08ec446c1b9b1",
68+
"filename": "README.md",
69+
"status": "modified",
70+
"additions": 1,
71+
"deletions": 1,
72+
"changes": 2,
73+
"blob_url": "https://github.com/ahrefs/monorepo/blob/0d95302addd66c1816bce1b1d495ed1c93ccd478/README.md",
74+
"raw_url": "https://github.com/ahrefs/monorepo/raw/0d95302addd66c1816bce1b1d495ed1c93ccd478/README.md",
75+
"contents_url": "https://github.com/api/v3/repos/ahrefs/monorepo/contents/README.md?ref=0d95302addd66c1816bce1b1d495ed1c93ccd478",
76+
"patch": ""
77+
}
78+
]
79+
}
File renamed without changes.

0 commit comments

Comments
 (0)