Skip to content

Commit

Permalink
Add username to the activity
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Feb 26, 2024
1 parent 95a76cd commit 7bf07fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 17 additions & 12 deletions lib/contributions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let ( / ) a b = Json.Util.member b a
let query =
{| query($from: DateTime!, $to: DateTime!) {
viewer {
login
contributionsCollection(from: $from, to: $to) {
issueContributions(first: 100) {
nodes {
Expand Down Expand Up @@ -82,6 +83,8 @@ type item = {
body : string;
}

type t = { username : string; activity : item list Repo_map.t }

let read_issues json =
Json.Util.to_list (json / "nodes") |> List.filter ((<>) `Null) |> List.map @@ fun node ->
let date = Datetime.parse (node / "occurredAt") in
Expand Down Expand Up @@ -123,20 +126,24 @@ let read_repos json =
{ kind = `New_repo; date; url; title = "Created new repository"; body = ""; repo }

let of_json ~from json =
let username = json / "data" / "viewer" / "login" |> Json.Util.to_string in
let contribs = json / "data" / "viewer" / "contributionsCollection" in
let items =
read_issues (contribs / "issueContributions") @
read_prs (contribs / "pullRequestContributions") @
read_reviews (contribs / "pullRequestReviewContributions") @
read_repos (contribs / "repositoryContributions")
in
(* GitHub seems to ignore the time part, so do the filtering here. *)
items
|> List.filter (fun item -> item.date >= from)
|> List.fold_left (fun acc item ->
let items = Repo_map.find_opt item.repo acc |> Option.value ~default:[] in
Repo_map.add item.repo (item :: items) acc
) Repo_map.empty
let activity =
(* GitHub seems to ignore the time part, so do the filtering here. *)
items
|> List.filter (fun item -> item.date >= from)
|> List.fold_left (fun acc item ->
let items = Repo_map.find_opt item.repo acc |> Option.value ~default:[] in
Repo_map.add item.repo (item :: items) acc
) Repo_map.empty
in
{ username; activity }

let id url =
match Astring.String.cut ~sep:"/" ~rev:true url with
Expand Down Expand Up @@ -171,12 +178,10 @@ let pp_items = Fmt.(list ~sep:(cut ++ cut) pp_item)
let pp_repo f (name, items) =
Fmt.pf f "### %s@,@,%a" name pp_items items

type t = item list Repo_map.t

let is_empty = Repo_map.is_empty
let is_empty { activity; _} = Repo_map.is_empty activity

let pp f t =
let by_repo = Repo_map.bindings t in
let pp f { activity; _ } =
let by_repo = Repo_map.bindings activity in
match by_repo with
| [] -> Fmt.string f "(no activity)"
| [(_, items)] -> pp_items f items
Expand Down
4 changes: 2 additions & 2 deletions lib/contributions.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
type t

module Datetime : sig
type t = string
end
Expand All @@ -15,6 +13,8 @@ type item = {

module Repo_map : Map.S with type key = string

type t = { username : string; activity : item list Repo_map.t }

val fetch : period:(string * string) -> token:Token.t -> Yojson.Safe.t

val of_json : from:string -> Yojson.Safe.t -> t
Expand Down

0 comments on commit 7bf07fe

Please sign in to comment.