Skip to content

Commit

Permalink
Implement GitHub issues query
Browse files Browse the repository at this point in the history
  • Loading branch information
chshersh committed Jan 19, 2025
1 parent 0ea21c9 commit f71814e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
(terminal_size (>= "0.2.0"))
(tty (>= "0.0.2"))
(uutf (>= "1.0.3"))
yojson
)
(tags
(tui cli git github)))
1 change: 1 addition & 0 deletions github_tui.opam
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ depends: [
"terminal_size" {>= "0.2.0"}
"tty" {>= "0.0.2"}
"uutf" {>= "1.0.3"}
"yojson"
"odoc" {with-doc}
]
build: [
Expand Down
2 changes: 1 addition & 1 deletion lib/gh/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(library
(name gh)
(libraries ezcurl))
(libraries ezcurl yojson))
1 change: 1 addition & 0 deletions lib/gh/gh.ml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module Client = Client
module Issues = Issues
4 changes: 4 additions & 0 deletions lib/gh/gh.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(** This module contains functions to query GitHub API *)

(** Queries related to issues. *)
module Issues = Issues

(** Generic client to submit GraphQL queries to GitHub API. *)
module Client = Client
79 changes: 79 additions & 0 deletions lib/gh/issues.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module Json = Yojson.Basic.Util

type t = {
number : int;
title : string;
author : string;
}

let mk_issues_query ~owner ~repo =
Printf.sprintf
{|
query {
repository(owner: "%s", name: "%s") {
issues(first: 2, states: [OPEN], orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
number
title
author {
login
}
}
}
}
}
|}
owner repo

(* Parse single issue from JSON:
{
"number": 18,
"title": "GitHub TUI: Release Tracker for v1.0",
"author": {
"login": "chshersh"
}
}
*)
let parse_issue json =
let number = Json.(member "number" json |> to_int) in
let title = Json.(member "title" json |> to_string) in
let author = Json.(member "author" json |> member "login" |> to_string) in
{ number; title; author }

(* Parse a list of 't' from the following JSON:
{
"data": {
"repository": {
"issues": {
"nodes": [
{
"number": 18,
"title": "GitHub TUI: Release Tracker for v1.0",
"author": {
"login": "chshersh"
}
},
{
"number": 17,
"title": "Refactor fs.ml: Separate concerns in go_down and go_up functions",
"author": {
"login": "chshersh"
}
}
]
}
}
}
}
*)
let parse_issues json =
json
|> Yojson.Basic.from_string
|> Json.path [ "data"; "repository"; "issues"; "nodes" ]
|> Option.value ~default:(`List [])
|> Json.convert_each parse_issue

let issues ~owner ~repo =
mk_issues_query ~owner ~repo |> Client.query |> parse_issues
8 changes: 8 additions & 0 deletions lib/gh/issues.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type t = {
number : int;
title : string;
author : string;
}

(** Query all open issues *)
val issues : owner:string -> repo:string -> t list

0 comments on commit f71814e

Please sign in to comment.