github: Clean `get_contributor_stats`
[nit.git] / lib / github / api.nit
index 901ec6a..ee62031 100644 (file)
@@ -20,7 +20,6 @@
 module api
 
 intrude import json::serialization_read
-import json::static
 
 import base64
 import curl
@@ -281,11 +280,7 @@ class GithubAPI
 
        # List of contributor related statistics.
        fun get_repo_contrib_stats(repo_slug: String): Array[ContributorStats] do
-               message(1, "Get contributor stats for {repo_slug}")
-               var res = new Array[ContributorStats]
-               var array = get("/repos/{repo_slug}/stats/contributors")
-               if not array isa JsonArray then return res
-               return deserialize(array.to_json).as(Array[ContributorStats])
+               return new GithubArray[ContributorStats].from(get("/repos/{repo_slug}/stats/contributors"))
        end
 
        # Get the Github branch with `name`.
@@ -369,6 +364,17 @@ class GithubAPI
                return get("/repos/{repo_slug}/pulls/{number}").as(nullable PullRequest)
        end
 
+       # List of comments on a pull request
+       fun get_pull_comments(repo_slug: String, pull_number: Int, page, per_page: nullable Int): Array[PullComment] do
+               return new GithubArray[PullComment].from(get(
+                       "/repos/{repo_slug}/pulls/{pull_number}/comments?{pagination(page, per_page)}"))
+       end
+
+       # Get a specific pull request comment
+       fun get_pull_comment(repo_slug: String, id: Int): nullable PullComment do
+               return get("/repos/{repo_slug}/pulls/comments/{id}").as(nullable PullComment)
+       end
+
        # Get the Github label with `name`.
        #
        # Returns `null` if the label cannot be found.
@@ -452,23 +458,6 @@ class GithubAPI
                return get("/repos/{repo_slug}/issues/comments/{id}").as(nullable IssueComment)
        end
 
-       # Get the Github diff comment with `id`.
-       #
-       # Returns `null` if the comment cannot be found.
-       #
-       # ~~~nitish
-       # var api = new GithubAPI(get_github_oauth)
-       # var repo = api.get_repo("nitlang/nit")
-       # assert repo != null
-       # var comment = api.get_review_comment(repo, 21010363)
-       # assert comment.path == "src/modelize/modelize_property.nit"
-       # assert comment.original_position == 26
-       # assert comment.pull_number == 945
-       # ~~~
-       fun get_review_comment(repo_slug: String, id: Int): nullable ReviewComment do
-               return get("/repos/{repo_slug}/pulls/comments/{id}").as(nullable ReviewComment)
-       end
-
        private fun pagination(page, per_page: nullable Int): String do
                return "page={page or else 1}&per_page={per_page or else 30}"
        end
@@ -841,7 +830,7 @@ end
 #
 # * `CommitComment` are made on a commit page.
 # * `IssueComment` are made on an issue or pull request page.
-# * `ReviewComment` are made on the diff associated to a pull request.
+# * `PullComment` are made on the diff associated to a pull request.
 abstract class Comment
        serialize
 
@@ -954,7 +943,7 @@ end
 # Should be accessed from `GithubAPI::get_diff_comment`.
 #
 # See <https://developer.github.com/v3/pulls/comments/>.
-class ReviewComment
+class PullComment
        super Comment
        serialize
 
@@ -1040,22 +1029,36 @@ class ContributorStats
 
        redef type OTHER: ContributorStats
 
-       # Github API client.
-       var api: GithubAPI is writable
-
        # User these statistics are about.
        var author: User is writable
 
        # Total number of commit.
        var total: Int is writable
 
-       # Are of weeks of activity with detailed statistics.
-       var weeks: JsonArray is writable
+       # Array of weeks of activity with detailed statistics.
+       var weeks: Array[ContributorWeek] is writable
 
        # ContributorStats can be compared on the total amount of commits.
        redef fun <(o) do return total < o.total
 end
 
+# Contributor stats weekly hash
+class ContributorWeek
+       serialize
+
+       # Start of week given a Unix timestamp
+       var w: Int
+
+       # Number of additions
+       var a: Int
+
+       # Number of deletions
+       var d: Int
+
+       # Number of commits
+       var c: Int
+end
+
 # A Github file representation.
 #
 # Mostly a wrapper around a json object.
@@ -1097,7 +1100,7 @@ class GithubDeserializer
                map["{pattern_base}/repos/[^/]*/[^/]*/issues/comments/[0-9]+$".to_re] = "IssueComment"
                map["{pattern_base}/repos/[^/]*/[^/]*/issues/events/[0-9]+$".to_re] = "IssueEvent"
                map["{pattern_base}/repos/[^/]*/[^/]*/pulls/[0-9]+$".to_re] = "PullRequest"
-               map["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re] = "ReviewComment"
+               map["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re] = "PullComment"
                map["{pattern_base}/repos/[^/]*/[^/]*/comments/[0-9]+$".to_re] = "CommitComment"
                map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+$".to_re] = "Commit"
                map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+/status$".to_re] = "CommitStatus"
@@ -1128,6 +1131,10 @@ class GithubDeserializer
                        return "Branch"
                else if raw.has_key("total_count") and raw.has_key("items") then
                        return "SearchResults"
+               else if raw.has_key("total") and raw.has_key("weeks") then
+                       return "ContributorStats"
+               else if raw.has_key("a") and raw.has_key("d") and raw.has_key("c") then
+                       return "ContributorWeek"
                end
                return null
        end