Skip to content

Commit 5da9b03

Browse files
committed
Refactor owner_name
Signed-off-by: Tim McGilchrist <timmcgil@gmail.com>
1 parent 488f773 commit 5da9b03

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

plugins/github/api.ml

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -158,38 +158,36 @@ module Ref_map = Map.Make(Ref)
158158

159159
module Commit_id = struct
160160
type t = {
161-
owner_name : string; (* e.g. "owner/name" *)
161+
owner: string;
162+
repo : string;
162163
id : Ref.t;
163164
hash : string;
164165
committed_date : string;
165166
} [@@deriving to_yojson]
166167

167-
let to_git { owner_name; id; hash; committed_date = _ } =
168-
let repo = Fmt.str "https://github.com/%s.git" owner_name in
168+
let to_git { owner; repo; id; hash; committed_date = _ } =
169+
let repo = Fmt.str "https://github.com/%s/%s.git" owner repo in
169170
let gref = Ref.to_git id in
170171
Current_git.Commit_id.v ~repo ~gref ~hash
171172

172-
let name t =
173-
match String.split_on_char '/' t.owner_name with
174-
| [owner;repo_name] -> Some (owner, repo_name)
175-
| _ -> None
176-
173+
let owner_name { owner; repo; _} = Fmt.str "%s/%s" owner repo
174+
177175
let uri t =
178-
Uri.make ~scheme:"https" ~host:"github.com" ~path:(Printf.sprintf "/%s/commit/%s" t.owner_name t.hash) ()
176+
Uri.make ~scheme:"https" ~host:"github.com" ~path:(Printf.sprintf "/%s/commit/%s/%s" t.owner t.repo t.hash) ()
179177

180178
let pp_id = Ref.pp
181179

182-
let compare {owner_name; id; hash; committed_date = _} b =
180+
let compare {owner; repo; id; hash; committed_date = _} b =
183181
match compare hash b.hash with
184182
| 0 ->
185183
begin match Ref.compare id b.id with
186-
| 0 -> compare owner_name b.owner_name
184+
| 0 -> compare (owner, repo) (b.owner, b.repo)
187185
| x -> x
188186
end
189187
| x -> x
190188

191-
let pp f { owner_name; id; hash; committed_date } =
192-
Fmt.pf f "%s@ %a@ %s@ %s" owner_name pp_id id (Astring.String.with_range ~len:8 hash) committed_date
189+
let pp f { owner; repo; id; hash; committed_date } =
190+
Fmt.pf f "%s/%s@ %a@ %s@ %s" owner repo pp_id id (Astring.String.with_range ~len:8 hash) committed_date
193191

194192
let digest t = Yojson.Safe.to_string (to_yojson t)
195193
end
@@ -325,13 +323,12 @@ let get_default_ref t { Repo_id.owner; name } =
325323
let data = json / "data" in
326324
handle_rate_limit t "default_ref" (data / "rateLimit");
327325
let repo = data / "repository" in
328-
let owner_name = repo / "nameWithOwner" |> to_string in
329326
let def = repo / "defaultBranchRef" in
330327
let prefix = def / "prefix" |> to_string in
331328
let name = def / "name" |> to_string in
332329
let hash = def / "target" / "oid" |> to_string in
333330
let committed_date = def / "target" / "committedDate" |> to_string in
334-
{ Commit_id.owner_name; id = `Ref (prefix ^ name); hash; committed_date }
331+
{ Commit_id.owner; repo = name ; id = `Ref (prefix ^ name); hash; committed_date }
335332
with ex ->
336333
let pp f j = Yojson.Safe.pretty_print f j in
337334
Log.err (fun f -> f "@[<v2>Invalid JSON: %a@,%a@]" Fmt.exn ex pp json);
@@ -425,23 +422,23 @@ let query_branches_and_open_prs = {|
425422
}
426423
|}
427424

428-
let parse_ref ~owner_name ~prefix json =
425+
let parse_ref ~owner ~repo ~prefix json =
429426
let open Yojson.Safe.Util in
430427
let node = json / "node" in
431428
let name = node / "name" |> to_string in
432429
let hash = node / "target" / "oid" |> to_string in
433430
let committed_date = node / "target" / "committedDate" |> to_string in
434-
{ Commit_id.owner_name; id = `Ref (prefix ^ name); hash; committed_date }
431+
{ Commit_id.owner; Commit_id.repo; id = `Ref (prefix ^ name); hash; committed_date }
435432

436-
let parse_pr ~owner_name json =
433+
let parse_pr ~owner ~repo json =
437434
let open Yojson.Safe.Util in
438435
let node = json / "node" in
439436
let hash = node / "headRefOid" |> to_string in
440437
let pr = node / "number" |> to_int in
441438
let nodes = node / "commits" / "nodes" |> to_list in
442-
if List.length nodes = 0 then Fmt.failwith "Failed to get latest commit for %s" owner_name else
439+
if List.length nodes = 0 then Fmt.failwith "Failed to get latest commit for %s/%s" owner repo else
443440
let committed_date = List.hd nodes / "commit" / "committedDate" |> to_string in
444-
{ Commit_id.owner_name; id = `PR pr; hash; committed_date }
441+
{ Commit_id.owner; Commit_id.repo; id = `PR pr; hash; committed_date }
445442

446443
let get_refs t { Repo_id.owner; name } =
447444
let variables = [
@@ -454,12 +451,12 @@ let get_refs t { Repo_id.owner; name } =
454451
let data = json / "data" in
455452
handle_rate_limit t "default_ref" (data / "rateLimit");
456453
let repo = data / "repository" in
457-
let owner_name = repo / "nameWithOwner" |> to_string in
454+
(* let owner_name = repo / "nameWithOwner" |> to_string in *)
458455
let default_ref = repo / "defaultBranchRef" / "name" |> to_string |> ( ^ ) "refs/heads/" in
459456
let refs =
460-
repo / "refs" / "edges" |> to_list |> List.map (parse_ref ~owner_name ~prefix:"refs/heads/") in
457+
repo / "refs" / "edges" |> to_list |> List.map (parse_ref ~owner ~repo:name ~prefix:"refs/heads/") in
461458
let prs =
462-
repo / "pullRequests" / "edges" |> to_list |> List.map (parse_pr ~owner_name) in
459+
repo / "pullRequests" / "edges" |> to_list |> List.map (parse_pr ~owner ~repo:name) in
463460
(* TODO: use cursors to get all results.
464461
For now, we just take the first 100 and warn if there are more. *)
465462
let n_branches = repo / "refs" / "totalCount" |> to_int in
@@ -604,11 +601,13 @@ module CheckRun = struct
604601
| Error (`Msg m) -> Lwt.fail_with m
605602
| Ok token ->
606603
let token = Github.Token.of_string token in
607-
let (owner, repo) = match Commit_id.name key.Key.commit with
608-
| Some s -> s
609-
| None ->
610-
let name = Commit_id.digest key.Key.commit in
611-
Fmt.failwith "GitHub owner/repo failed: %s" name in
604+
let owner = key.Key.commit.owner in
605+
let repo = key.Key.commit.repo in
606+
(* let (owner, repo) = match Commit_id.name key.Key.commit.owner with
607+
* | Some s -> s
608+
* | None ->
609+
* let name = Commit_id.digest key.Key.commit in
610+
* Fmt.failwith "GitHub owner/repo failed: %s" name in *)
612611
let sha = key.Key.commit.hash in
613612
let app_id = t.app_id in
614613
let check_name = key.Key.check_name in
@@ -707,7 +706,7 @@ module Commit = struct
707706
| Ok token ->
708707
let headers = Cohttp.Header.init_with "Authorization" ("bearer " ^ token) in
709708
let uri = status_endpoint
710-
~owner_name:commit.Commit_id.owner_name
709+
~owner_name:(Commit_id.owner_name commit)
711710
~commit:commit.Commit_id.hash
712711
in
713712
Current.Job.log job "@[<v2>POST %a:@,%a@]"
@@ -747,7 +746,7 @@ module Commit = struct
747746

748747
let compare (_, a) (_, b) = Commit_id.compare a b
749748

750-
let owner_name (_, id) = id.Commit_id.owner_name
749+
let owner_name (_, id) = Commit_id.owner_name id
751750

752751
let repo_id t =
753752
let full = owner_name t in
@@ -825,8 +824,8 @@ module Anonymous = struct
825824
Lwt.try_bind
826825
(fun () -> query_head repo gref)
827826
(fun hash ->
828-
let id = { Commit_id.owner_name; hash; id = gref; committed_date = "" } in
829-
Lwt_result.return (Commit_id.to_git id)
827+
let id = { Commit_id.owner = repo.owner; repo = repo.name; hash; id = gref; committed_date = "" } in
828+
Lwt_result.return (Commit_id.to_git id)
830829
)
831830
(fun ex ->
832831
Log.warn (fun f -> f "GitHub query_head failed: %a" Fmt.exn ex);

0 commit comments

Comments
 (0)