lib/github: add `Comment::is_ack` to detect acknowledgments
[nit.git] / lib / github / api.nit
index 782d598..6f7c7d9 100644 (file)
@@ -344,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.
@@ -365,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
@@ -394,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))
@@ -431,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")
@@ -678,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
@@ -997,6 +1016,12 @@ abstract class Comment
 
        # Comment body text.
        fun body: String do return json["body"].to_s
+
+       # Does the comment contain an acknowledgement (+1)
+       fun is_ack: Bool
+       do
+               return body.has("\\+1\\b".to_re) or body.has(":+1:") or body.has(":shipit:")
+       end
 end
 
 # A comment made on a commit.