Merge: ci: generate metrics output in an artifact file
[nit.git] / lib / github / api.nit
index cc2abf4..bd01519 100644 (file)
@@ -19,9 +19,7 @@
 # For most use-cases you need to use the `GithubAPI` client.
 module api
 
-# TODO to remove
 intrude import json::serialization_read
-import json::static
 
 import base64
 import curl
@@ -93,12 +91,6 @@ class GithubAPI
        # Default is `https://api.github.com` and should not be changed.
        var api_url = "https://api.github.com"
 
-       # Verbosity level.
-       #
-       # * `0`: only errors (default)
-       # * `1`: verbose
-       var verbose_lvl = 0 is public writable
-
        # Send a HTTPRequest to the Github API
        fun send(method, path: String, headers: nullable HeaderMap, body: nullable String): nullable String do
                last_error = null
@@ -145,11 +137,6 @@ class GithubAPI
                return res
        end
 
-       # Display a message depending on `verbose_lvl`.
-       fun message(lvl: Int, message: String) do
-               if lvl <= verbose_lvl then print message
-       end
-
        # Escape `uri` in an acceptable format for Github.
        private fun sanitize_uri(uri: String): String do
                # TODO better URI escape.
@@ -282,11 +269,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`.
@@ -320,6 +303,13 @@ class GithubAPI
                return get("/repos/{repo_slug}/commits/{sha}").as(nullable Commit)
        end
 
+       # Get the status of a commit
+       #
+       # The status holds the result of each check ran on a commit like CI, reviews etc.
+       fun get_commit_status(repo_slug: String, sha: String): nullable CommitStatus do
+               return get("/repos/{repo_slug}/commits/{sha}/status").as(nullable CommitStatus)
+       end
+
        # Get the Github issue #`number`.
        #
        # Returns `null` if the issue cannot be found.
@@ -363,6 +353,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.
@@ -446,23 +447,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
@@ -556,7 +540,7 @@ class Repo
        var owner: User is writable
 
        # Repo default branch name.
-       var default_branch: String is writable
+       var default_branch: nullable String = null is optional, writable
 end
 
 # A Github branch.
@@ -597,23 +581,9 @@ class Commit
        # Authoring date as String.
        var author_date: nullable String is writable
 
-       # Authoring date as ISODate.
-       fun iso_author_date: nullable ISODate do
-               var author_date = self.author_date
-               if author_date == null then return null
-               return new ISODate.from_string(author_date)
-       end
-
        # Commit date as String.
        var commit_date: nullable String is writable
 
-       # Commit date as ISODate.
-       fun iso_commit_date: nullable ISODate do
-               var commit_date = self.commit_date
-               if commit_date == null then return null
-               return new ISODate.from_string(commit_date)
-       end
-
        # List files staged in this commit.
        var files: nullable Array[GithubFile] = null is optional, writable
 
@@ -650,13 +620,6 @@ class GitUser
 
        # Authoring date.
        var date: nullable String = null is writable
-
-       # Authoring date as ISODate.
-       fun iso_date: nullable ISODate do
-               var date = self.date
-               if date == null then return null
-               return new ISODate.from_string(date)
-       end
 end
 
 # A Github issue.
@@ -700,31 +663,12 @@ class Issue
        # Creation time as String.
        var created_at: String is writable
 
-       # Creation time as ISODate.
-       fun iso_created_at: ISODate do
-               return new ISODate.from_string(created_at)
-       end
-
        # Last update time as String (if any).
        var updated_at: nullable String is writable
 
-       # Last update date as ISODate.
-       fun iso_updated_at: nullable ISODate do
-               var updated_at = self.updated_at
-               if updated_at == null then return null
-               return new ISODate.from_string(updated_at)
-       end
-
        # Close time as String (if any).
        var closed_at: nullable String is writable
 
-       # Close time as ISODate.
-       fun iso_closed_at: nullable ISODate do
-               var closed_at = self.closed_at
-               if closed_at == null then return null
-               return new ISODate.from_string(closed_at)
-       end
-
        # Full description of the issue.
        var body: nullable String is writable
 
@@ -748,13 +692,6 @@ class PullRequest
        # Merge time as String (if any).
        var merged_at: nullable String is writable
 
-       # Merge time as ISODate.
-       fun iso_merged_at: nullable ISODate do
-               var merged_at = self.merged_at
-               if merged_at == null then return null
-               return new ISODate.from_string(merged_at)
-       end
-
        # Merge commit SHA.
        var merge_commit_sha: nullable String is writable
 
@@ -863,45 +800,17 @@ class Milestone
        # Creation time as String.
        var created_at: nullable String is writable
 
-       # Creation time as ISODate.
-       fun iso_created_at: nullable ISODate do
-               var created_at = self.created_at
-               if created_at == null then return null
-               return new ISODate.from_string(created_at)
-       end
-
        # User that created this milestone.
        var creator: nullable User is writable
 
        # Due time as String (if any).
        var due_on: nullable String is writable
 
-       # Due time in ISODate format (if any).
-       fun iso_due_on: nullable ISODate do
-               var due_on = self.due_on
-               if due_on == null then return null
-               return new ISODate.from_string(due_on)
-       end
-
        # Last update time as String (if any).
        var updated_at: nullable String is writable
 
-       # Last update date as ISODate.
-       fun iso_updated_at: nullable ISODate do
-               var updated_at = self.updated_at
-               if updated_at == null then return null
-               return new ISODate.from_string(updated_at)
-       end
-
        # Close time as String (if any).
        var closed_at: nullable String is writable
-
-       # Close time as ISODate.
-       fun iso_closed_at: nullable ISODate do
-               var closed_at = self.closed_at
-               if closed_at == null then return null
-               return new ISODate.from_string(closed_at)
-       end
 end
 
 # A Github comment
@@ -910,7 +819,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
 
@@ -923,21 +832,9 @@ abstract class Comment
        # Creation time as String.
        var created_at: String is writable
 
-       # Creation time as ISODate.
-       fun iso_created_at: nullable ISODate do
-               return new ISODate.from_string(created_at)
-       end
-
        # Last update time as String (if any).
        var updated_at: nullable String is writable
 
-       # Last update date as ISODate.
-       fun iso_updated_at: nullable ISODate do
-               var updated_at = self.updated_at
-               if updated_at == null then return null
-               return new ISODate.from_string(updated_at)
-       end
-
        # Comment body text.
        var body: String is writable
 
@@ -965,6 +862,55 @@ class CommitComment
        var path: nullable String is writable
 end
 
+# Status of a commit
+#
+# Can contain sub-status for reviews, CI etc.
+class CommitStatus
+       serialize
+
+       # Global state of this commit
+       var state: nullable String = null is optional, writable
+
+       # Sha of the commit this status is for
+       var sha: nullable String = null is optional, writable
+
+       # Repository the commit belongs to
+       var repository: nullable Repo = null is optional, writable
+
+       # All sub statuses (one for each check)
+       var statuses = new Array[RepoStatus] is optional, writable
+
+       # Total count of sub statuses
+       var total_count: nullable Int = null is optional, writable
+end
+
+# Sub status of a CommitStatus
+#
+# Represents a check applied to a commit (reviews, CI, ...).
+class RepoStatus
+       serialize
+
+       # State of this check
+       var state: nullable String = null is optional, writable
+
+       # Description of this check
+       var description: nullable String = null is optional, writable
+
+       # External URL
+       var target_url: nullable String = null is optional, writable
+
+       # Context this status is related to
+       #
+       # Used to hold the name of the check applied.
+       var context: nullable String = null is optional, writable
+
+       # Date when this status was created
+       var created_at: nullable String = null is optional, writable
+
+       # Last date this status was updated
+       var updated_at: nullable String = null is optional, writable
+end
+
 # Comments made on Github issue and pull request pages.
 #
 # Should be accessed from `GithubAPI::get_issue_comment`.
@@ -986,7 +932,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
 
@@ -1032,11 +978,6 @@ class IssueEvent
        # Creation time as String.
        var created_at: String is writable
 
-       # Creation time as ISODate.
-       fun iso_created_at: nullable ISODate do
-               return new ISODate.from_string(created_at)
-       end
-
        # Event descriptor.
        var event: String is writable
 
@@ -1077,22 +1018,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.
@@ -1117,48 +1072,58 @@ class SearchResults
        var items: Array[Object]
 end
 
-# Make ISO Datew serilizable
-redef class ISODate
-       serialize
-end
-
 # JsonDeserializer specific for Github objects.
 class GithubDeserializer
        super JsonDeserializer
 
-       redef fun class_name_heuristic(json_object) do
-               if json_object.has_key("login") then
-                       return "User"
-               else if json_object.has_key("full_name") then
-                       return "Repo"
-               else if json_object.has_key("name") and json_object.has_key("commit") then
+       private var pattern_base = "https://api.github.com"
+
+       # Url patterns to class names
+       var url_patterns: Map[Regex, String] is lazy do
+               var map = new HashMap[Regex, String]
+               map["{pattern_base}/users/[^/]*$".to_re] = "User"
+               map["{pattern_base}/repos/[^/]*/[^/]*$".to_re] = "Repo"
+               map["{pattern_base}/repos/[^/]*/[^/]*/labels/[^/]+$".to_re] = "Label"
+               map["{pattern_base}/repos/[^/]*/[^/]*/milestones/[0-9]+$".to_re] = "Milestone"
+               map["{pattern_base}/repos/[^/]*/[^/]*/issues/[0-9]+$".to_re] = "Issue"
+               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] = "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"
+               map["{pattern_base}/repos/[^/]*/[^/]*/statuses/[a-f0-9]+$".to_re] = "RepoStatus"
+               return map
+       end
+
+       # Match `url` property in object to a class name
+       fun url_heuristic(raw: Map[String, nullable Object]): nullable String do
+               if not raw.has_key("url") then return null
+
+               var url = raw["url"].as(String)
+               for re, class_name in url_patterns do
+                       if url.has(re) then return class_name
+               end
+               return null
+       end
+
+       redef fun class_name_heuristic(raw) do
+               # Try with url
+               var class_name = url_heuristic(raw)
+               if class_name != null then return class_name
+
+               # print raw.serialize_to_json(true, true) # debug
+
+               # Use properties heuristics
+               if raw.has_key("name") and raw.has_key("commit") then
                        return "Branch"
-               else if json_object.has_key("sha") and json_object.has_key("ref") then
-                       return "PullRef"
-               else if (json_object.has_key("sha") and json_object.has_key("commit")) or (json_object.has_key("id") and json_object.has_key("tree_id")) then
-                       return "Commit"
-               else if json_object.has_key("sha") and json_object.has_key("tree") then
-                       return "GitCommit"
-               else if json_object.has_key("name") and json_object.has_key("date") then
-                       return "GitUser"
-               else if json_object.has_key("number") and json_object.has_key("patch_url") then
-                       return "PullRequest"
-               else if json_object.has_key("open_issues") and json_object.has_key("closed_issues") then
-                       return "Milestone"
-               else if json_object.has_key("number") and json_object.has_key("title") then
-                       return "Issue"
-               else if json_object.has_key("color") then
-                       return "Label"
-               else if json_object.has_key("event") then
-                       return "IssueEvent"
-               else if json_object.has_key("original_commit_id") then
-                       return "ReviewComment"
-               else if json_object.has_key("commit_id") then
-                       return "CommitComment"
-               else if json_object.has_key("issue_url") then
-                       return "IssueComment"
-               else if json_object.has_key("total_count") then
+               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