misc/vim: inform the user when no results are found
[nit.git] / lib / github / api.nit
index 815172a..ea516fa 100644 (file)
@@ -133,7 +133,7 @@ class GithubAPI
 
        # Load the json object from Github.
        # See `GithubEntity::load_from_github`.
-       private fun load_from_github(key: String): JsonObject do
+       protected fun load_from_github(key: String): JsonObject do
                message(1, "Get {key} (github)")
                var res = get(key)
                if was_error then return new JsonObject
@@ -266,9 +266,7 @@ class GithubAPI
        #     assert event.issue.number == 945
        fun load_issue_event(repo: Repo, id: Int): nullable IssueEvent do
                var event = new IssueEvent(self, repo, id)
-               event.load_from_github
-               if was_error then return null
-               return event
+               return event.load_from_github
        end
 
        # Get the Github commit comment with `id`.
@@ -346,6 +344,9 @@ abstract class GithubEntity
        end
 
        redef fun to_s do return json.to_json
+
+       # Github page url.
+       fun html_url: String do return json["html_url"].to_s
 end
 
 # A Github user.
@@ -367,9 +368,6 @@ class User
                self.json = json
        end
 
-       # Github User page url.
-       fun html_url: String do return json["html_url"].to_s
-
        # Avatar image url for this user.
        fun avatar_url: String do return json["avatar_url"].to_s
 end
@@ -396,9 +394,6 @@ class Repo
        # Repo short name on Github.
        fun name: String do return json["name"].to_s
 
-       # Github User page url.
-       fun html_url: String do return json["html_url"].to_s
-
        # Get the repo owner.
        fun owner: User do
                return new User.from_json(api, json["owner"].as(JsonObject))
@@ -433,6 +428,27 @@ class Repo
                return res
        end
 
+       # Search issues in this repo form an advanced query.
+       #
+       # Example:
+       #
+       # ~~~nitish
+       # var issues = repo.search_issues("is:open label:need_review")
+       # ~~~
+       #
+       # See <https://developer.github.com/v3/search/#search-issues>.
+       fun search_issues(query: String): nullable Array[Issue] do
+               query = "search/issues?q={query} repo:{full_name}"
+               var response = api.get(query)
+               if api.was_error then return null
+               var arr = response.as(JsonObject)["items"].as(JsonArray)
+               var res = new Array[Issue]
+               for obj in arr do
+                       res.add new Issue.from_json(api, self, obj.as(JsonObject))
+               end
+               return res
+       end
+
        # Get the last published issue.
        fun last_issue: nullable Issue do
                var array = api.get("repos/{full_name}/issues")
@@ -494,6 +510,19 @@ class Repo
                return res
        end
 
+       # List of contributor related statistics.
+       fun contrib_stats: Array[ContributorStats] do
+               api.message(1, "Get contributor stats for {full_name}")
+               var res = new Array[ContributorStats]
+               var array = api.get("{key}/stats/contributors")
+               if array isa JsonArray then
+                       for obj in array do
+                               res.add new ContributorStats.from_json(api, obj.as(JsonObject))
+                       end
+               end
+               return res
+       end
+
        # Repo default branch.
        fun default_branch: Branch do
                var name = json["default_branch"].to_s
@@ -623,6 +652,17 @@ class Commit
                return new ISODate.from_string(author["date"].to_s)
        end
 
+       # List files staged in this commit.
+       fun files: Array[GithubFile] do
+               var res = new Array[GithubFile]
+               var files = json["files"]
+               if not files isa JsonArray then return res
+               for obj in files do
+                       res.add(new GithubFile(obj.as(JsonObject)))
+               end
+               return res
+       end
+
        # Commit message.
        fun message: String do return json["commit"].as(JsonObject)["message"].to_s
 end
@@ -656,6 +696,7 @@ class Issue
        # List of labels on this issue associated to their names.
        fun labels: Map[String, Label] do
                var res = new HashMap[String, Label]
+               if not json.has_key("labels") then return res
                for obj in json["labels"].as(JsonArray) do
                        if not obj isa JsonObject then continue
                        var name = obj["name"].to_s
@@ -1150,3 +1191,52 @@ class RenameAction
        # Name after renaming.
        fun to: String do return json["to"].to_s
 end
+
+# Contributors list with additions, deletions, and commit counts.
+#
+# Should be accessed from `Repo::contrib_stats`.
+#
+# See <https://developer.github.com/v3/repos/statistics/>.
+class ContributorStats
+       super Comparable
+
+       redef type OTHER: ContributorStats
+
+       # Github API client.
+       var api: GithubAPI
+
+       # Json content.
+       var json: JsonObject
+
+       # Init `self` from a `json` object.
+       init from_json(api: GithubAPI, json: JsonObject) do
+               self.api = api
+               self.json = json
+       end
+
+       # User these statistics are about.
+       fun author: User do
+               return new User.from_json(api, json["author"].as(JsonObject))
+       end
+
+       # Total number of commit.
+       fun total: Int do return json["total"].to_s.to_i
+
+       # Are of weeks of activity with detailed statistics.
+       fun weeks: JsonArray do return json["weeks"].as(JsonArray)
+
+       # ContributorStats can be compared on the total amount of commits.
+       redef fun <(o) do return total < o.total
+end
+
+# A Github file representation.
+#
+# Mostly a wrapper around a json object.
+class GithubFile
+
+       # Json content.
+       var json: JsonObject
+
+       # File name.
+       fun filename: String do return json["filename"].to_s
+end