Skip to content

Commit

Permalink
Define a User type
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Mar 11, 2024
1 parent e7733db commit c43af04
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
13 changes: 11 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@ let period =
in
Term.(const f $ from $ to_ $ last_week)

let user =
let user : User.t Term.t =
let str_parser, str_printer = Arg.string in
let parser x =
match str_parser x with `Ok x -> `Ok (User.User x) | `Error e -> `Error e
in
let printer fs = function
| User.Viewer -> str_printer fs "viewer"
| User x -> str_printer fs x
in
let user_conv = (parser, printer) in
let doc = Arg.info ~doc:"User name" [ "user" ] in
Arg.(value & opt (some string) None & doc)
Arg.(value & opt user_conv Viewer & doc)

let version =
match Build_info.V1.version () with
Expand Down
24 changes: 8 additions & 16 deletions lib/contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module Json = Yojson.Safe
let ( / ) a b = Json.Util.member b a

let query user =
Format.sprintf
Format.asprintf
{|query($from: DateTime!, $to: DateTime!) {
%s {
%a {
login
contributionsCollection(from: $from, to: $to) {
issueContributions(first: 100) {
Expand Down Expand Up @@ -55,9 +55,7 @@ let query user =
}
}
}|}
(match user with
| Some u -> Format.sprintf "user(login: %S)" u
| None -> "viewer")
User.query user

let request ~period:(start, finish) ~user ~token =
let variables = [ ("from", `String start); ("to", `String finish) ] in
Expand Down Expand Up @@ -143,17 +141,11 @@ let read_repos json =
}

let of_json ~from ~user json =
let username, contribs =
match user with
| Some username ->
let contribs = json / "data" / "user" / "contributionsCollection" in
(username, contribs)
| None ->
let username =
json / "data" / "viewer" / "login" |> Json.Util.to_string
in
let contribs = json / "data" / "viewer" / "contributionsCollection" in
(username, contribs)
let username =
json / "data" / User.response_field user / "login" |> Json.Util.to_string
in
let contribs =
json / "data" / User.response_field user / "contributionsCollection"
in
let items =
read_issues (contribs / "issueContributions")
Expand Down
7 changes: 2 additions & 5 deletions lib/contributions.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ module Repo_map : Map.S with type key = string
type t = { username : string; activity : item list Repo_map.t }

val request :
period:string * string ->
user:string option ->
token:Token.t ->
Graphql.request
period:string * string -> user:User.t -> token:Token.t -> Graphql.request

val of_json : from:string -> user:string option -> Yojson.Safe.t -> t
val of_json : from:string -> user:User.t -> Yojson.Safe.t -> t
(** We pass [from] again here so we can filter out anything that GitHub included by accident. *)

val is_empty : t -> bool
Expand Down
25 changes: 11 additions & 14 deletions test/lib/test_contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ module Testable = struct
end

let request ~user =
Format.sprintf
Format.asprintf
{|query($from: DateTime!, $to: DateTime!) {
%s {
%a {
login
contributionsCollection(from: $from, to: $to) {
issueContributions(first: 100) {
Expand Down Expand Up @@ -136,9 +136,7 @@ let request ~user =
}
}
}|}
(match user with
| Some u -> Format.sprintf "user(login: %S)" u
| None -> "viewer")
User.query user

let test_request =
let make_test name ~period ~user ~token ~expected =
Expand All @@ -150,7 +148,7 @@ let test_request =
(name, `Quick, test_fun)
in
[
(let user = None in
(let user = User.Viewer in
make_test "no token" ~user ~token:"" ~period:("", "")
~expected:
{
Expand All @@ -165,7 +163,7 @@ let test_request =
`Assoc [ ("from", `String ""); ("to", `String "") ] );
];
});
(let user = Some "me" in
(let user = User.User "me" in
make_test "no token" ~user ~token:"" ~period:("", "")
~expected:
{
Expand All @@ -182,7 +180,7 @@ let test_request =
});
]

let or_viewer = function Some u -> u | None -> "gpetiot"
let or_viewer = function User.User u -> u | Viewer -> "gpetiot"

let activity_example ~user =
Format.sprintf
Expand Down Expand Up @@ -305,8 +303,7 @@ let activity_example ~user =
}
}
|}
(match user with Some _ -> "user" | None -> "viewer")
(user |> or_viewer)
(User.response_field user) (user |> or_viewer)

let activity_example_json ~user =
Yojson.Safe.from_string (activity_example ~user)
Expand Down Expand Up @@ -414,11 +411,11 @@ let test_of_json =
(name, `Quick, test_fun)
in
[
(let user = None in
(let user = User.Viewer in
make_test "no token" ~from:"" ~user
(activity_example_json ~user)
~expected:(contributions_example ~user));
(let user = Some "gpetiot" in
(let user = User.User "gpetiot" in
make_test "no token" ~from:"" ~user
(activity_example_json ~user)
~expected:(contributions_example ~user));
Expand All @@ -439,7 +436,7 @@ let test_is_empty =
{ Contributions.username = ""; activity = Contributions.Repo_map.empty }
~expected:true;
make_test "not empty"
~input:(contributions_example ~user:None)
~input:(contributions_example ~user:Viewer)
~expected:false;
]

Expand All @@ -458,7 +455,7 @@ let test_pp =
{ Contributions.username = ""; activity = Contributions.Repo_map.empty }
~expected:"(no activity)";
make_test "not empty"
~input:(contributions_example ~user:None)
~input:(contributions_example ~user:Viewer)
~expected:
"### gpetiot/config.ml\n\
Created repository \
Expand Down

0 comments on commit c43af04

Please sign in to comment.