@@ -8,50 +8,50 @@ let cache_dir = Caml.Filename.concat cwd "github-api-cache"
8
8
9
9
(* * return the file with a function f applied unless the file is empty;
10
10
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 =
12
12
match get_local_file url with
13
13
| Error e ->
14
14
let err_msg = sprintf " error while getting local file: %s\n cached for url: %s" e url in
15
15
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)
19
19
20
20
let clean_forward_slashes = String. substr_replace_all ~pattern: " /" ~with_: " _"
21
21
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
+
22
32
module Github : Api .Github = struct
23
33
let get_config ~(ctx : Context.t ) ~repo :_ =
24
34
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
26
36
27
37
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
31
39
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
35
42
36
43
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
40
45
41
46
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
45
48
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
50
52
51
53
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
55
55
56
56
let request_reviewers ~ctx :_ ~repo :_ ~number :_ ~reviewers :_ = Lwt. return @@ Error " undefined for local setup"
57
57
end
0 commit comments