lib/github: handles commit comments
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 13 Jan 2015 16:37:14 +0000 (17:37 +0100)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 13 Jan 2015 16:39:55 +0000 (17:39 +0100)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/github/api.nit

index 4103878..61a27f7 100644 (file)
@@ -251,6 +251,22 @@ class GithubAPI
                var milestone = new Milestone(self, repo, id)
                return milestone.load_from_github
        end
+
+       # Get the Github commit comment with `id`.
+       #
+       # Returns `null` if the comment cannot be found.
+       #
+       #     var api = new GithubAPI(get_github_oauth)
+       #     var repo = api.load_repo("privat/nit")
+       #     assert repo != null
+       #     var comment = api.load_commit_comment(repo, 8982707)
+       #     assert comment.user.login == "Morriar"
+       #     assert comment.body == "For testing purposes..."
+       #     assert comment.commit.sha == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
+       fun load_commit_comment(repo: Repo, id: Int): nullable CommitComment do
+               var comment = new CommitComment(self, repo, id)
+               return comment.load_from_github
+       end
 end
 
 # Something returned by the Github API.
@@ -872,3 +888,34 @@ abstract class Comment
        # Comment body text.
        fun body: String do return json["body"].to_s
 end
+
+# A comment made on a commit.
+class CommitComment
+       super Comment
+
+       redef var key is lazy do return "{repo.key}/comments/{id}"
+
+       # Commented commit.
+       fun commit: Commit do
+               return api.load_commit(repo, json["commit_id"].to_s).as(not null)
+       end
+
+       # Position of the comment on the line.
+       fun position: nullable String do
+               if not json.has_key("position") then return null
+               var res = json["position"]
+               if res == null then return null
+               return res.to_s
+       end
+
+       # Line of the comment.
+       fun line: nullable String do
+               if not json.has_key("line") then return null
+               var res = json["line"]
+               if res == null then return null
+               return res.to_s
+       end
+
+       # Path of the commented file.
+       fun path: String do return json["path"].to_s
+end