Permalink
Browse files

[enhance] github: Full v2 API (+1 v3 function)

  • Loading branch information...
1 parent 8f5eb7e commit a63f9239a027534b9e88958bb0da5b1ddd95e013 Nicolas Glondu committed Jun 30, 2011
@@ -4,9 +4,8 @@
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
- terms of the GNU Affero General Public License as published by the Free
- Software Foundation, either version 3 of the License, or (at your option)
- any later version.
+ terms of the GNU Affero General Public License, version 3, as published by
+ the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
@@ -0,0 +1,167 @@
+/*
+ Copyright © 2011 MLstate
+
+ This file is part of OPA.
+
+ OPA is free software: you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License, version 3, as published by
+ the Free Software Foundation.
+
+ OPA is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
+ more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with OPA. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ * Author : Nicolas Glondu <nicolas.glondu@mlstate.com>
+ **/
+
+/**
+ * GitHub commit API module
+ *
+ * @category api
+ * @author Nicolas Glondu, 2011
+ * @destination public
+ */
+
+//package stdlib.apis.github.commit
+import stdlib.apis.github
+import stdlib.apis.github.lib
+
+/* Types returned by API */
+
+type GitHub.commit = {
+ id : string
+ parents : list(string)
+ author : GitHub.commit_user
+ message : string
+ url : string
+ commited_date : Date.date
+ authored_date : Date.date
+ tree : string
+ committer : GitHub.commit_user
+}
+
+type GitHub.commit_more = {
+ base : GitHub.commit
+ modified : list((string,string))
+ removed : list(string)
+}
+
+@private GHCp = {{
+
+ @private GP = GHParse
+
+ get_commit_internal(m) =
+ parents = List.filter_map(
+ j -> match j:RPC.Json.json with
+ | {Record=r} ->
+ match List.assoc("id",r) with
+ | {some={String=s}} -> some(s)
+ | {some=v} -> some(Json.serialize(v))
+ | {none} -> none
+ end
+ | _ -> none,
+ m.list("parents"))
+ { id = m.str("id")
+ parents = parents
+ author = GP.get_commit_user(m.record("author"))
+ message = m.str("message")
+ url = m.str("url")
+ commited_date = m.date("commited_date")
+ authored_date = m.date("authored_date")
+ tree = m.str("tree")
+ committer = GP.get_commit_user(m.record("committer"))
+ } : GitHub.commit
+
+ get_commit(srcmap) =
+ m = GP.map_funs(srcmap)
+ if m.exists("id") then
+ some(get_commit_internal(m))
+ else none
+
+ get_commit_more(srcmap) =
+ m = GP.map_funs(srcmap)
+ if m.exists("id") then
+ base = get_commit_internal(m)
+ removed = m.list("removed")
+ |> List.filter_map(
+ x ->
+ match x with
+ | {String=s} -> some(s)
+ | _ -> none,
+ _ )
+ modified = m.list("modified")
+ |> List.fold(
+ e, acc ->
+ match e:RPC.Json.json with
+ | {Record=r} ->
+ List.fold(
+ (k,v), acc ->
+ vv = match v:RPC.Json.json with
+ | {String=s} -> s
+ | _ -> Json.serialize(v)
+ List.add((k,vv),acc),
+ r, acc
+ )
+ | _ -> acc,
+ _, [])
+ some(~{base modified removed}:GitHub.commit_more)
+ else none
+
+ multiple_commits(res) =
+ GP.dewrap_list(res, "commits", get_commit)
+
+ one_commit_more(res) =
+ GP.dewrap_obj(res, "commit", get_commit_more)
+
+}}
+
+GHCommit = {{
+
+ @private p(o:string,r:string) = "{o}/{r}"
+
+ /**
+ * Gets a list of commits on a branch of a repo
+ *
+ * @param owner Owner of the repo
+ * @param repo Repository to get commits from
+ * @param branch Branch to get commits from
+ * @param page Page of commits displayed. First page is 1. If a value inferior to 1 is provided, it is considered as 1.
+ */
+ get_commits(owner, repo, branch:string, page:int) =
+ path = "/commits/list/{p(owner,repo)}/{branch}"
+ /* Force the min value of page to 1, GitHub also does
+ it on its side anyway. */
+ page = if page < 1 then 1 else page
+ data = [("page",Int.to_string(page))]
+ GHLib.api_get(path, data, GHCp.multiple_commits)
+
+ /**
+ * Gets a list of commit on a file on a branch of a repo
+ *
+ * @param owner Owner of the repo
+ * @param repo Repository to get commits from
+ * @param branch Branch to get commits from
+ * @param file File to follow
+ * @param page Page of commits displayed. First page is 1. If a value inferior to 1 is provided, it is considered as 1.
+ */
+ get_file_commits(owner, repo, branch:string, file:string, page) =
+ path = "/commits/list/{p(owner,repo)}/{branch}/{file}"
+ /* Force the min value of page to 1, GitHub also does
+ it on its side anyway. */
+ page = if page < 1 then 1 else page
+ data = [("page",Int.to_string(page))]
+ GHLib.api_get(path, data, GHCp.multiple_commits)
+
+ /**
+ * Gets detailled information about one commit
+ */
+ get_commit(owner, repo, sha:string) =
+ path = "/commits/show/{p(owner,repo)}/{sha}"
+ GHLib.api_get(path, [], GHCp.one_commit_more)
+
+}}
@@ -0,0 +1,117 @@
+/*
+ Copyright © 2011 MLstate
+
+ This file is part of OPA.
+
+ OPA is free software: you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License, version 3, as published by
+ the Free Software Foundation.
+
+ OPA is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
+ more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with OPA. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ * Author : Nicolas Glondu <nicolas.glondu@mlstate.com>
+ **/
+
+/**
+ * GitHub gist API module
+ *
+ * @category api
+ * @author Nicolas Glondu, 2011
+ * @destination public
+ */
+
+//package stdlib.apis.github.gist
+import stdlib.apis.github
+import stdlib.apis.github.lib
+
+/* Types returned by API */
+
+type GitHub.gist_comment = {
+ id : int
+ user : string
+ created_at : Date.date
+ updated_at : Date.date
+ body : string
+}
+
+type GitHub.gist = {
+ owner : string
+ public : bool
+ repo : string
+ created_at : Date.date
+ description : string
+ files : list(string)
+ comments : list(GitHub.gist_comment)
+}
+
+@private GHGp = {{
+
+ @private GP = GHParse
+
+ get_comment(srcmap) =
+ m = GP.map_funs(srcmap)
+ { id = m.int("id")
+ user = m.str("user")
+ created_at = m.date("created_at")
+ updated_at = m.date("updated_at")
+ body = m.str("body")
+ } : GitHub.gist_comment
+
+ get_gist(srcmap) =
+ m = GP.map_funs(srcmap)
+ if m.exists("repo") then
+ files = List.map(
+ v -> match v:RPC.Json.json with
+ | {String=s} -> s
+ | _ -> Json.serialize(v),
+ m.list("files"))
+ comments = List.filter_map(
+ v -> match v:RPC.Json.json with
+ | {Record=_} -> some(get_comment(v))
+ | _ -> none,
+ m.list("comments"))
+ res = {
+ owner = m.str("owner")
+ public = m.bool("public")
+ repo = m.str("repo")
+ created_at = m.date("created_at")
+ description = m.str("description")
+ files = files
+ comments = comments
+ } : GitHub.gist
+ some(res)
+ else none
+
+ multiple_gists(res) =
+ GP.dewrap_list(res, "gists", get_gist)
+
+}}
+
+GHGist = {{
+
+ @private host = "https://gist.github.com/api/v1/json"
+
+ get_gist(gist_id:string) =
+ path = "{host}/{gist_id}"
+ r = GHLib.api_get_full(path, [], GHGp.multiple_gists)
+ match r with
+ | {some=l} ->
+ if l == [] then none else some(List.head(l))
+ | _ -> none
+
+ get_user_gists(user:string) =
+ path = "{host}/gists/{user}"
+ GHLib.api_get_full(path, [], GHGp.multiple_gists)
+
+ get_gist_file(gist_id:string, filename:string) =
+ path = "https://raw.github.com/gist/{gist_id}/{filename}"
+ GHLib.api_get_full(path, [], some)
+
+}}
@@ -31,6 +31,40 @@
/* Types returned by API */
+
+type GitHub.plan = {
+ name : string
+ collaborators : int
+ space : int
+ private_repos : int
+}
+
+type GitHub.user_more = {
+ total_private_repo_count : int
+ collaborators : int
+ disk_usage : int
+ owned_private_repo_count : int
+ private_gist_count : int
+ plan : GitHub.plan
+}
+
+type GitHub.user = {
+ id : int
+ login : string
+ name : string
+ company : string
+ gravatar_id : string
+ created_at : Date.date
+ location : string
+ blog : string
+ public_repo_count : int
+ public_gist_count : int
+ followers_count : int
+ following_count : int
+ user_type : string
+ more : option(GitHub.user_more)
+}
+
type GitHub.repository = {
name : string
owner : string
@@ -56,3 +90,9 @@ type GitHub.public_key = {
id : int
key : string
}
+
+type GitHub.commit_user = {
+ name : string
+ login : string
+ email : string
+}
Oops, something went wrong.

0 comments on commit a63f923

Please sign in to comment.