+ fun message: String do return json["commit"].as(JsonObject)["message"].as(String)
+end
+
+# A Github issue.
+#
+# Should be accessed from `GithubAPI::load_issue`.
+#
+# See <https://developer.github.com/v3/issues/>.
+class Issue
+ super RepoEntity
+
+ redef var key is lazy do return "{repo.key}/issues/{number}"
+
+ # Issue Github ID.
+ var number: Int
+
+ redef init from_json(api, repo, json) do
+ self.number = json["number"].as(Int)
+ super
+ end
+
+ # Issue title.
+ fun title: String do return json["title"].as(String)
+
+ # User that created this issue.
+ fun user: User do
+ return new User.from_json(api, json["user"].as(JsonObject))
+ end
+
+ # List of labels on this issue associated to their names.
+ fun labels: Map[String, Label] do
+ var res = new HashMap[String, Label]
+ var lbls = json.get_or_null("labels")
+ if not lbls isa JsonArray then return res
+ for obj in lbls do
+ if not obj isa JsonObject then continue
+ var name = obj["name"].as(String)
+ res[name] = new Label.from_json(api, repo, obj)
+ end
+ return res
+ end
+
+ # State of the issue on Github.
+ fun state: String do return json["state"].as(String)
+
+ # Is the issue locked?
+ fun locked: Bool do return json["locked"].as(Bool)
+
+ # Assigned `User` (if any).
+ fun assignee: nullable User do
+ var assignee = json.get_or_null("assignee")
+ if assignee isa JsonObject then return new User.from_json(api, assignee)
+ return null
+ end
+
+ # `Milestone` (if any).
+ fun milestone: nullable Milestone do
+ var milestone = json.get_or_null("milestone")
+ if milestone isa JsonObject then return new Milestone.from_json(api, repo, milestone)
+ return null
+ end
+
+ # List of comments made on this issue.
+ fun comments: Array[IssueComment] do
+ var res = new Array[IssueComment]
+ var count = comments_count
+ var page = 1
+ var array = api.get("{key}/comments?page={page}")
+ if not array isa JsonArray then
+ return res
+ end
+ while not array.is_empty and res.length < count do
+ for obj in array do
+ if not obj isa JsonObject then continue
+ var id = obj["id"].as(Int)
+ res.add(api.load_issue_comment(repo, id).as(not null))
+ end
+ page += 1
+ array = api.get("{key}/comments?page={page}").as(JsonArray)
+ end
+ return res
+ end
+
+ # Number of comments on this issue.
+ fun comments_count: Int do return json["comments"].as(Int)
+
+ # Creation time in ISODate format.
+ fun created_at: ISODate do
+ return new ISODate.from_string(json["created_at"].as(String))
+ end
+
+ # Last update time in ISODate format (if any).
+ fun updated_at: nullable ISODate do
+ var res = json.get_or_null("updated_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # Close time in ISODate format (if any).
+ fun closed_at: nullable ISODate do
+ var res = json.get_or_null("closed_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # TODO link to pull request
+
+ # Full description of the issue.
+ fun body: String do return json["body"].as(String)
+
+ # List of events on this issue.
+ fun events: Array[IssueEvent] do
+ var res = new Array[IssueEvent]
+ var page = 1
+ var array = api.get("{key}/events?page={page}")
+ if not array isa JsonArray then return res
+ while not array.is_empty do
+ for obj in array do
+ if not obj isa JsonObject then continue
+ res.add new IssueEvent.from_json(api, repo, obj)
+ end
+ page += 1
+ array = api.get("{key}/events?page={page}").as(JsonArray)
+ end
+ return res
+ end
+
+ # User that closed this issue (if any).
+ fun closed_by: nullable User do
+ var closer = json.get_or_null("closed_by")
+ if closer isa JsonObject then return new User.from_json(api, closer)
+ return null
+ end
+end
+
+# A Github pull request.
+#
+# Should be accessed from `GithubAPI::load_pull`.
+#
+# PullRequest are basically Issues with more data.
+# See <https://developer.github.com/v3/pulls/>.
+class PullRequest
+ super Issue
+
+ redef var key is lazy do return "{repo.key}/pulls/{number}"
+
+ # Merge time in ISODate format (if any).
+ fun merged_at: nullable ISODate do
+ var res = json.get_or_null("merged_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # Merge commit SHA.
+ fun merge_commit_sha: String do return json["merge_commit_sha"].as(String)
+
+ # Count of comments made on the pull request diff.
+ fun review_comments: Int do return json["review_comments"].as(Int)
+
+ # Pull request head (can be a commit SHA or a branch name).
+ fun head: PullRef do
+ var json = json["head"].as(JsonObject)
+ return new PullRef(api, json)
+ end
+
+ # Pull request base (can be a commit SHA or a branch name).
+ fun base: PullRef do
+ var json = json["base"].as(JsonObject)
+ return new PullRef(api, json)
+ end
+
+ # Is this pull request merged?
+ fun merged: Bool do return json["merged"].as(Bool)
+
+ # Is this pull request mergeable?
+ fun mergeable: Bool do return json["mergeable"].as(Bool)
+
+ # Mergeable state of this pull request.
+ #
+ # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
+ fun mergeable_state: Int do return json["mergeable_state"].as(Int)
+
+ # User that merged this pull request (if any).
+ fun merged_by: nullable User do
+ var merger = json.get_or_null("merged_by")
+ if merger isa JsonObject then return new User.from_json(api, merger)
+ return null
+ end
+
+ # Count of commits in this pull request.
+ fun commits: Int do return json["commits"].as(Int)
+
+ # Added line count.
+ fun additions: Int do return json["additions"].as(Int)
+
+ # Deleted line count.
+ fun deletions: Int do return json["deletions"].as(Int)
+
+ # Changed files count.
+ fun changed_files: Int do return json["changed_files"].as(Int)
+end
+
+# A pull request reference (used for head and base).
+class PullRef
+
+ # Api instance that maintains self.
+ var api: GithubAPI
+
+ # JSON representation.
+ var json: JsonObject
+
+ # Label pointed by `self`.
+ fun labl: String do return json["label"].as(String)
+
+ # Reference pointed by `self`.
+ fun ref: String do return json["ref"].as(String)
+
+ # Commit SHA pointed by `self`.
+ fun sha: String do return json["sha"].as(String)
+
+ # User pointed by `self`.
+ fun user: User do
+ return new User.from_json(api, json["user"].as(JsonObject))
+ end
+
+ # Repo pointed by `self`.
+ fun repo: Repo do
+ return new Repo.from_json(api, json["repo"].as(JsonObject))
+ end
+end
+
+# A Github label.
+#
+# Should be accessed from `GithubAPI::load_label`.
+#
+# See <https://developer.github.com/v3/issues/labels/>.
+class Label
+ super RepoEntity
+
+ redef var key is lazy do return "{repo.key}/labels/{name}"
+
+ # Label name.
+ var name: String
+
+ redef init from_json(api, repo, json) do
+ self.name = json["name"].as(String)
+ super
+ end
+
+ # Label color code.
+ fun color: String do return json["color"].as(String)
+end
+
+# A Github milestone.
+#
+# Should be accessed from `GithubAPI::load_milestone`.
+#
+# See <https://developer.github.com/v3/issues/milestones/>.
+class Milestone
+ super RepoEntity
+
+ redef var key is lazy do return "{repo.key}/milestones/{number}"
+
+ # The milestone id on Github.
+ var number: Int
+
+ redef init from_json(api, repo, json) do
+ super
+ self.number = json["number"].as(Int)
+ end
+
+ # Milestone title.
+ fun title: String do return json["title"].as(String)
+
+ # Milestone long description.
+ fun description: String do return json["description"].as(String)
+
+ # Count of opened issues linked to this milestone.
+ fun open_issues: Int do return json["open_issues"].as(Int)
+
+ # Count of closed issues linked to this milestone.
+ fun closed_issues: Int do return json["closed_issues"].as(Int)
+
+ # Milestone state.
+ fun state: String do return json["state"].as(String)
+
+ # Creation time in ISODate format.
+ fun created_at: ISODate do
+ return new ISODate.from_string(json["created_at"].as(String))
+ end
+
+ # User that created this milestone.
+ fun creator: User do
+ return new User.from_json(api, json["creator"].as(JsonObject))
+ end
+
+ # Due time in ISODate format (if any).
+ fun due_on: nullable ISODate do
+ var res = json.get_or_null("updated_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # Update time in ISODate format (if any).
+ fun updated_at: nullable ISODate do
+ var res = json.get_or_null("updated_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # Close time in ISODate format (if any).
+ fun closed_at: nullable ISODate do
+ var res = json.get_or_null("closed_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+end
+
+# A Github comment
+#
+# There is two kinds of comments:
+#
+# * `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.
+abstract class Comment
+ super RepoEntity
+
+ # Identifier of this comment.
+ var id: Int
+
+ redef init from_json(api, repo, json) do
+ self.id = json["id"].as(Int)
+ super
+ end
+
+ # User that made this comment.
+ fun user: User do
+ return new User.from_json(api, json["user"].as(JsonObject))
+ end
+
+ # Creation time in ISODate format.
+ fun created_at: ISODate do
+ return new ISODate.from_string(json["created_at"].as(String))
+ end
+
+ # Last update time in ISODate format (if any).
+ fun updated_at: nullable ISODate do
+ var res = json.get_or_null("updated_at")
+ if res isa String then return new ISODate.from_string(res)
+ return null
+ end
+
+ # Comment body text.
+ fun body: String do return json["body"].as(String)
+
+ # 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.
+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"].as(String)).as(not null)
+ end
+
+ # Position of the comment on the line.
+ fun position: nullable String do
+ var res = json.get_or_null("position")
+ if res isa String then return res
+ return null
+ end
+
+ # Line of the comment.
+ fun line: nullable String do
+ var res = json.get_or_null("line")
+ if res isa String then return res
+ return null
+ end
+
+ # Path of the commented file.
+ fun path: String do return json["path"].as(String)
+end
+
+# Comments made on Github issue and pull request pages.
+#
+# Should be accessed from `GithubAPI::load_issue_comment`.
+#
+# See <https://developer.github.com/v3/issues/comments/>.
+class IssueComment
+ super Comment
+
+ redef var key is lazy do return "{repo.key}/issues/comments/{id}"
+
+ # Issue that contains `self`.
+ fun issue: Issue do
+ var number = issue_url.split("/").last.to_i
+ return api.load_issue(repo, number).as(not null)
+ end
+
+ # Link to the issue document on API.
+ fun issue_url: String do return json["issue_url"].as(String)
+end
+
+# Comments made on Github pull request diffs.
+#
+# Should be accessed from `GithubAPI::load_diff_comment`.
+#
+# See <https://developer.github.com/v3/pulls/comments/>.
+class ReviewComment
+ super Comment
+
+ redef var key is lazy do return "{repo.key}/pulls/comments/{id}"
+
+ # Pull request that contains `self`.
+ fun pull: PullRequest do
+ var number = pull_request_url.split("/").last.to_i
+ return api.load_pull(repo, number).as(not null)
+ end
+
+ # Link to the pull request on API.
+ fun pull_request_url: String do return json["pull_request_url"].as(String)
+
+ # Diff hunk.
+ fun diff_hunk: String do return json["diff_hunk"].as(String)
+
+ # Path of commented file.
+ fun path: String do return json["path"].as(String)
+
+ # Position of the comment on the file.
+ fun position: Int do return json["position"].as(Int)
+
+ # Original position in the diff.
+ fun original_position: Int do return json["original_position"].as(Int)
+
+ # Commit referenced by this comment.
+ fun commit_id: String do return json["commit_id"].as(String)
+
+ # Original commit id.
+ fun original_commit_id: String do return json["original_commit_id"].as(String)
+end
+
+# An event that occurs on a Github `Issue`.
+#
+# Should be accessed from `GithubAPI::load_issue_event`.
+#
+# See <https://developer.github.com/v3/issues/events/>.
+class IssueEvent
+ super RepoEntity
+
+ redef var key is lazy do return "{repo.key}/issues/events/{id}"
+
+ # Event id on Github.
+ var id: Int
+
+ redef init from_json(api, repo, json) do
+ self.id = json["id"].as(Int)
+ super
+ end
+
+ # Issue that contains `self`.
+ fun issue: Issue do
+ return new Issue.from_json(api, repo, json["issue"].as(JsonObject))
+ end
+
+ # User that initiated the event.
+ fun actor: User do
+ return new User.from_json(api, json["actor"].as(JsonObject))
+ end
+
+ # Creation time in ISODate format.
+ fun created_at: ISODate do
+ return new ISODate.from_string(json["created_at"].as(String))
+ end
+
+ # Event descriptor.
+ fun event: String do return json["event"].as(String)
+
+ # Commit linked to this event (if any).
+ fun commit_id: nullable String do
+ var res = json.get_or_null("commit_id")
+ if res isa String then return res
+ return null
+ end
+
+ # Label linked to this event (if any).
+ fun labl: nullable Label do
+ var res = json.get_or_null("label")
+ if res isa JsonObject then return new Label.from_json(api, repo, res)
+ return null
+ end
+
+ # User linked to this event (if any).
+ fun assignee: nullable User do
+ var res = json.get_or_null("assignee")
+ if res isa JsonObject then return new User.from_json(api, res)
+ return null
+ end
+
+ # Milestone linked to this event (if any).
+ fun milestone: nullable Milestone do
+ var res = json.get_or_null("milestone")
+ if res isa JsonObject then return new Milestone.from_json(api, repo, res)
+ return null
+ end
+
+ # Rename linked to this event (if any).
+ fun rename: nullable RenameAction do
+ var res = json.get_or_null("rename")
+ if res isa JsonObject then return new RenameAction(res)
+ return null
+ end
+end
+
+# A rename action maintains the name before and after a renaming action.
+class RenameAction
+
+ # JSON content.
+ var json: JsonObject
+
+ # Name before renaming.
+ fun from: String do return json["from"].as(String)
+
+ # Name after renaming.
+ fun to: String do return json["to"].as(String)
+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"].as(Int)
+
+ # 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"].as(String)