Skip to content

Commit 09c8c12

Browse files
committed
add api routes to retrieve GH issues and PRs
1 parent 6f0aa64 commit 09c8c12

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/api.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module type Github = sig
66
val get_config : ctx:Context.t -> repo:repository -> (Config_t.config, string) Result.t Lwt.t
77

88
val get_api_commit : ctx:Context.t -> repo:repository -> sha:string -> (api_commit, string) Result.t Lwt.t
9+
10+
val get_pull_request : ctx:Context.t -> repo:repository -> number:int -> (pull_request, string) Result.t Lwt.t
11+
12+
val get_issue : ctx:Context.t -> repo:repository -> number:int -> (issue, string) Result.t Lwt.t
913
end
1014

1115
module type Slack = sig

lib/api_local.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module Github : Api.Github = struct
1818
match get_local_file url with
1919
| Error e -> Lwt.return @@ fmt_error "error while getting local file: %s\nfailed to get api commit %s" e url
2020
| Ok file -> Lwt.return @@ Ok (Github_j.api_commit_of_string file)
21+
22+
let get_pull_request ~ctx:_ ~repo:_ ~number:_ = Lwt.return @@ Error "undefined for local setup"
23+
24+
let get_issue ~ctx:_ ~repo:_ ~number:_ = Lwt.return @@ Error "undefined for local setup"
2125
end
2226

2327
module Slack_base : Api.Slack = struct

lib/api_remote.ml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ module Github : Api.Github = struct
1010
let contents_url ~(repo : Github_t.repository) ~path =
1111
String.substr_replace_first ~pattern:"{+path}" ~with_:path repo.contents_url
1212

13+
let pulls_url ~(repo : Github_t.repository) ~number =
14+
String.substr_replace_first ~pattern:"{/number}" ~with_:(sprintf "/%d" number) repo.pulls_url
15+
16+
let issues_url ~(repo : Github_t.repository) ~number =
17+
String.substr_replace_first ~pattern:"{/number}" ~with_:(sprintf "/%d" number) repo.issues_url
18+
1319
let build_headers ?token () =
1420
let headers = [ "Accept: application/vnd.github.v3+json" ] in
1521
Option.value_map token ~default:headers ~f:(fun v -> sprintf "Authorization: token %s" v :: headers)
@@ -38,13 +44,24 @@ module Github : Api.Github = struct
3844
@@ fmt_error "unexpected encoding '%s' in Github response\nfailed to get config from file %s" encoding url
3945
)
4046

41-
let get_api_commit ~(ctx : Context.t) ~repo ~sha =
47+
let get_resource (ctx : Context.t) url =
4248
let secrets = Context.get_secrets_exn ctx in
43-
let url = commits_url ~repo ~sha in
4449
let headers = build_headers ?token:secrets.gh_token () in
4550
match%lwt http_request ~headers `GET url with
46-
| Ok res -> Lwt.return @@ Ok (Github_j.api_commit_of_string res)
47-
| Error e -> Lwt.return @@ fmt_error "error while querying remote: %s\nfailed to get api commit from file %s" e url
51+
| Ok res -> Lwt.return @@ Ok res
52+
| Error e -> Lwt.return @@ fmt_error "error while querying remote: %s\nfailed to get resource from %s" e url
53+
54+
let get_api_commit ~(ctx : Context.t) ~repo ~sha =
55+
let%lwt res = commits_url ~repo ~sha |> get_resource ctx in
56+
Lwt.return @@ Result.map res ~f:Github_j.api_commit_of_string
57+
58+
let get_pull_request ~(ctx : Context.t) ~repo ~number =
59+
let%lwt res = pulls_url ~repo ~number |> get_resource ctx in
60+
Lwt.return @@ Result.map res ~f:Github_j.pull_request_of_string
61+
62+
let get_issue ~(ctx : Context.t) ~repo ~number =
63+
let%lwt res = issues_url ~repo ~number |> get_resource ctx in
64+
Lwt.return @@ Result.map res ~f:Github_j.issue_of_string
4865
end
4966

5067
module Slack : Api.Slack = struct

0 commit comments

Comments
 (0)