Skip to content

Commit 086d383

Browse files
committed
action and gh api: refactored handling api returns and cleaned up unused types
1 parent fb01b81 commit 086d383

File tree

8 files changed

+16
-55
lines changed

8 files changed

+16
-55
lines changed

lib/action.ml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,30 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
317317
Lwt.return_none
318318
in
319319
let process link =
320+
let with_gh_result_populate_slack (type a) ~(api_result : (a, string) Result.t)
321+
~(populate : repository -> a -> Slack_t.message_attachment) ~repo
322+
=
323+
match api_result with
324+
| Error _ -> Lwt.return_none
325+
| Ok item -> Lwt.return_some @@ (link, populate repo item)
326+
in
320327
match Github.gh_link_of_string link with
321328
| None -> Lwt.return_none
322329
| Some gh_link ->
323330
match gh_link with
324331
| Pull_request (repo, number) ->
325-
( match%lwt Github_api.get_pull_request ~ctx ~repo ~number with
326-
| Error _ -> Lwt.return_none
327-
| Ok pr -> Lwt.return_some @@ (link, Slack_message.populate_pull_request repo pr)
328-
)
332+
let%lwt result = Github_api.get_pull_request ~ctx ~repo ~number in
333+
with_gh_result_populate_slack ~api_result:result ~populate:Slack_message.populate_pull_request ~repo
329334
| Issue (repo, number) ->
330-
( match%lwt Github_api.get_issue ~ctx ~repo ~number with
331-
| Error _ -> Lwt.return_none
332-
| Ok issue -> Lwt.return_some @@ (link, Slack_message.populate_issue repo issue)
333-
)
335+
let%lwt result = Github_api.get_issue ~ctx ~repo ~number in
336+
with_gh_result_populate_slack ~api_result:result ~populate:Slack_message.populate_issue ~repo
334337
| Commit (repo, sha) ->
335-
( match%lwt Github_api.get_api_commit ~ctx ~repo ~sha with
336-
| Error _ -> Lwt.return_none
337-
| Ok commit -> Lwt.return_some @@ (link, Slack_message.populate_commit repo commit)
338-
)
339-
| Compare (repo, (base, merge)) ->
340-
( match%lwt Github_api.get_compare ~ctx ~repo ~basehead:(base, merge) with
341-
| Error _ -> Lwt.return_none
342-
| Ok compare -> Lwt.return_some @@ (link, Slack_message.populate_compare repo compare)
343-
)
338+
let%lwt result = Github_api.get_api_commit ~ctx ~repo ~sha in
339+
with_gh_result_populate_slack ~api_result:result ~populate:Slack_message.populate_commit ~repo
340+
| Compare (repo, basehead) ->
341+
let%lwt result = Github_api.get_compare ~ctx ~repo ~basehead in
342+
with_gh_result_populate_slack ~api_result:result ~populate:Slack_message.populate_compare ~repo
344343
in
345-
346344
let%lwt bot_user_id =
347345
match State.get_bot_user_id ctx.state with
348346
| Some id -> Lwt.return_some id

lib/api.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ open Slack_t
44

55
module type Github = sig
66
val get_config : ctx:Context.t -> repo:repository -> (Config_t.config, string) Result.t Lwt.t
7-
val get_branch : ctx:Context.t -> repo:repository -> name:string -> (branch, string) Result.t Lwt.t
87
val get_api_commit : ctx:Context.t -> repo:repository -> sha:string -> (api_commit, string) Result.t Lwt.t
98
val get_pull_request : ctx:Context.t -> repo:repository -> number:int -> (pull_request, string) Result.t Lwt.t
109
val get_issue : ctx:Context.t -> repo:repository -> number:int -> (issue, string) Result.t Lwt.t
1110
val get_compare : ctx:Context.t -> repo:repository -> basehead:Github.basehead -> (compare, string) Result.t Lwt.t
12-
val get_release_tag : ctx:Context.t -> repo:repository -> release_tag:string -> (release_tag, string) Result.t Lwt.t
1311

1412
val request_reviewers
1513
: ctx:Context.t ->

lib/api_local.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ module Github : Api.Github = struct
3434
let url = Caml.Filename.concat cwd ctx.config_filename in
3535
with_cache_file url Config_j.config_of_string
3636

37-
let get_branch ~ctx:_ ~(repo : Github_t.repository) ~name =
38-
get_repo_member_cache ~repo ~kind:"branch" ~ref_:name ~of_string:Github_j.branch_of_string
39-
4037
let get_api_commit ~ctx:_ ~repo ~sha =
4138
get_repo_member_cache ~repo ~kind:"commit" ~ref_:sha ~of_string:Github_j.api_commit_of_string
4239

@@ -50,9 +47,6 @@ module Github : Api.Github = struct
5047
get_repo_member_cache ~repo ~kind:"compare" ~ref_:(sprintf "%s...%s" base merge)
5148
~of_string:Github_j.compare_of_string
5249

53-
let get_release_tag ~ctx:_ ~(repo : Github_t.repository) ~release_tag =
54-
get_repo_member_cache ~repo ~kind:"release_tag" ~ref_:release_tag ~of_string:Github_j.release_tag_of_string
55-
5650
let request_reviewers ~ctx:_ ~repo:_ ~number:_ ~reviewers:_ = Lwt.return @@ Error "undefined for local setup"
5751
end
5852

lib/api_remote.ml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ module Github : Api.Github = struct
1616
let issues_url ~(repo : Github_t.repository) ~number =
1717
String.substr_replace_first ~pattern:"{/number}" ~with_:(sprintf "/%d" number) repo.issues_url
1818

19-
let branches_url ~(repo : Github_t.repository) ~name =
20-
String.substr_replace_first ~pattern:"{/branch}" ~with_:(sprintf "/%s" name) repo.branches_url
21-
2219
let compare_url ~(repo : Github_t.repository) ~basehead:(base, merge) =
2320
String.substr_replace_first ~pattern:"{/basehead}" ~with_:(sprintf "/%s...%s" base merge) repo.compare_url
2421

25-
let releases_tags_url ~(repo : Github_t.repository) ~release_tag =
26-
String.substr_replace_first ~pattern:"{/release_tag}" ~with_:(sprintf "/tags/%s" release_tag) repo.releases_url
27-
2822
let build_headers ?token () =
2923
let headers = [ "Accept: application/vnd.github.v3+json" ] in
3024
Option.value_map token ~default:headers ~f:(fun v -> sprintf "Authorization: token %s" v :: headers)
@@ -72,10 +66,6 @@ module Github : Api.Github = struct
7266
let%lwt res = commits_url ~repo ~sha |> get_resource ~secrets:(Context.get_secrets_exn ctx) ~repo_url:repo.url in
7367
Lwt.return @@ Result.map res ~f:Github_j.api_commit_of_string
7468

75-
let get_branch ~(ctx : Context.t) ~(repo : Github_t.repository) ~name =
76-
let%lwt res = branches_url ~repo ~name |> get_resource ~secrets:(Context.get_secrets_exn ctx) ~repo_url:repo.url in
77-
Lwt.return @@ Result.map res ~f:Github_j.branch_of_string
78-
7969
let get_pull_request ~(ctx : Context.t) ~(repo : Github_t.repository) ~number =
8070
let%lwt res = pulls_url ~repo ~number |> get_resource ~secrets:(Context.get_secrets_exn ctx) ~repo_url:repo.url in
8171
Lwt.return @@ Result.map res ~f:Github_j.pull_request_of_string
@@ -90,12 +80,6 @@ module Github : Api.Github = struct
9080
in
9181
Lwt.return @@ Result.map res ~f:Github_j.compare_of_string
9282

93-
let get_release_tag ~(ctx : Context.t) ~(repo : Github_t.repository) ~release_tag =
94-
let%lwt res =
95-
releases_tags_url ~repo ~release_tag |> get_resource ~secrets:(Context.get_secrets_exn ctx) ~repo_url:repo.url
96-
in
97-
Lwt.return @@ Result.map res ~f:Github_j.release_tag_of_string
98-
9983
let request_reviewers ~(ctx : Context.t) ~(repo : Github_t.repository) ~number ~reviewers =
10084
let body = Github_j.string_of_request_reviewers_req reviewers in
10185
let%lwt res =

lib/github.atd

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ type repository = {
5959
full_name: string;
6060
html_url <ocaml name="url"> : string;
6161
commits_url: string;
62-
branches_url: string;
6362
contents_url: string;
6463
pulls_url: string;
6564
issues_url: string;
6665
compare_url: string;
67-
releases_url: string;
6866
}
6967

7068
type commit_pushed_notification = {
@@ -135,11 +133,6 @@ type compare = {
135133
files: file list;
136134
}
137135

138-
type release_tag = {
139-
url: string;
140-
name: string;
141-
}
142-
143136
type pr_action = [
144137
Assigned <json name="assigned">
145138
| Unassigned <json name="unassigned">

lib/github.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ let gh_link_of_string url_str =
134134
pulls_url = sprintf "%s/pulls{/number}" api_base;
135135
issues_url = sprintf "%s/issues{/number}" api_base;
136136
compare_url = sprintf "%s/compare{/basehead}" api_base;
137-
branches_url = sprintf "%s/branches{/branch}" api_base;
138-
releases_url = sprintf "%s/releases{/release_tag}" api_base;
139137
}
140138
in
141139
let repo = make_repo owner name in

test/github_link_test.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ let mk_repo ?(owner = "ahrefs") ?(scheme = "https") prefix prefix_api : reposito
1414
pulls_url = sprintf "%s://%s/repos/%s/monorepo/pulls{/number}" scheme prefix_api owner;
1515
issues_url = sprintf "%s://%s/repos/%s/monorepo/issues{/number}" scheme prefix_api owner;
1616
compare_url = sprintf "%s://%s/repos/%s/monorepo/compare{/basehead}" scheme prefix_api owner;
17-
branches_url = sprintf "%s://%s/repos/%s/monorepo/branches{/branch}" scheme prefix_api owner;
18-
releases_url = sprintf "%s://%s/repos/%s/monorepo/releases{/release_tag}" scheme prefix_api owner;
1917
}
2018

2119
let enterprise_repo1 = mk_repo "example.org" "example.org/api/v3"

test/test.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ let () =
8686
pulls_url = "";
8787
issues_url = "";
8888
compare_url = "";
89-
branches_url = "";
90-
releases_url = "";
9189
}
9290
in
9391
let ctx = Context.make ~state_filepath:"state.json" () in

0 commit comments

Comments
 (0)