+ 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 id.
+ fun id: Int do return json["id"].as(Int)
+
+ # Set issue id.
+ fun id=(id: Int) do json["id"] = id
+
+ # Issue title.
+ fun title: String do return json["title"].as(String)
+
+ # Set issue title
+ fun title=(title: String) do json["title"] = title
+
+ # User that created this issue.
+ fun user: User do return new User.from_json(api, json["user"].as(JsonObject))
+
+ # Set issue creator.
+ fun user=(user: User) do json["user"] = user.json
+
+ # 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)
+
+ # Set the state of this issue.
+ fun state=(state: String) do json["state"] = state
+
+ # Is the issue locked?
+ fun locked: Bool do return json["locked"].as(Bool)
+
+ # Set issue locked state.
+ fun locked=(locked: Bool) do json["locked"] = locked
+
+ # 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
+
+ # Set issue assignee.
+ fun assignee=(user: nullable User) do
+ if user == null then
+ json["assignee"] = null
+ else
+ json["assignee"] = user.json
+ end
+ 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
+
+ # Set issue milestone.
+ fun milestone=(milestone: nullable Milestone) do
+ if milestone == null then
+ json["milestone"] = null
+ else
+ json["milestone"] = milestone.json
+ end
+ 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)
+ var comment = api.load_issue_comment(repo, id)
+ if comment == null then continue
+ res.add(comment)
+ end
+ page += 1
+ var json = api.get("{key}/comments?page={page}")
+ if not json isa JsonArray then
+ return res
+ end
+ array = json
+ 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))
+
+ # Set issue creation time.
+ fun created_at=(created_at: nullable ISODate) do
+ if created_at == null then
+ json["created_at"] = null
+ else
+ json["created_at"] = created_at.to_s
+ end
+ 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
+
+ # Set issue last update time.
+ fun updated_at=(updated_at: nullable ISODate) do
+ if updated_at == null then
+ json["updated_at"] = null
+ else
+ json["updated_at"] = updated_at.to_s
+ end
+ 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
+
+ # Set issue close time.
+ fun closed_at=(closed_at: nullable ISODate) do
+ if closed_at == null then
+ json["closed_at"] = null
+ else
+ json["closed_at"] = closed_at.to_s
+ end
+ end
+
+ # TODO link to pull request
+
+ # Full description of the issue.
+ fun body: String do return json["body"].as(String)
+
+ # Set description body
+ fun body=(body: String) do json["body"] = body
+
+ # 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
+
+ # Set user that closed the issue.
+ fun closed_by=(user: nullable User) do
+ if user == null then
+ json["closed_by"] = null
+ else
+ json["closed_by"] = user.json
+ end
+ end
+
+ # Is this issue linked to a pull request?
+ fun is_pull_request: Bool do return json.has_key("pull_request")
+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
+
+ # Set pull request merge time.
+ fun merged_at=(merged_at: nullable ISODate) do
+ if merged_at == null then
+ json["merged_at"] = null
+ else
+ json["merged_at"] = merged_at.to_s
+ end
+ end
+
+ # Merge commit SHA.
+ fun merge_commit_sha: String do return json["merge_commit_sha"].as(String)
+
+ # Set merge_commit_sha
+ fun merge_commit_sha=(sha: String) do json["merge_commit_sha"] = sha
+
+ # Count of comments made on the pull request diff.
+ fun review_comments: Int do return json["review_comments"].as(Int)
+
+ # Set review_comments
+ fun review_comments=(count: Int) do json["review_comments"] = count
+
+ # 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
+
+ # Set head
+ fun head=(head: PullRef) do json["head"] = head.json
+
+ # 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
+
+ # Set base
+ fun base=(base: PullRef) do json["base"] = base.json
+
+ # Is this pull request merged?
+ fun merged: Bool do return json["merged"].as(Bool)
+
+ # Set merged
+ fun merged=(merged: Bool) do json["merged"] = merged
+
+ # Is this pull request mergeable?
+ fun mergeable: Bool do return json["mergeable"].as(Bool)
+
+ # Set mergeable
+ fun mergeable=(mergeable: Bool) do json["mergeable"] = mergeable
+
+ # 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)
+
+ # Set mergeable_state
+ fun mergeable_state=(mergeable_state: Int) do json["mergeable_state"] = mergeable_state
+
+ # 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
+
+ # Set merged_by.
+ fun merged_by=(merged_by: nullable User) do
+ if merged_by == null then
+ json["merged_by"] = null
+ else
+ json["merged_by"] = merged_by.json
+ end
+ end
+
+ # Count of commits in this pull request.
+ fun commits: Int do return json["commits"].as(Int)
+
+ # Set commits
+ fun commits=(commits: Int) do json["commits"] = commits
+
+ # Added line count.
+ fun additions: Int do return json["additions"].as(Int)
+
+ # Set additions
+ fun additions=(additions: Int) do json["additions"] = additions
+
+ # Deleted line count.
+ fun deletions: Int do return json["deletions"].as(Int)
+
+ # Set deletions
+ fun deletions=(deletions: Int) do json["deletions"] = deletions
+
+ # Changed files count.
+ fun changed_files: Int do return json["changed_files"].as(Int)
+
+ # Set changed_files
+ fun changed_files=(changed_files: Int) do json["changed_files"] = changed_files
+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)
+
+ # Set labl
+ fun labl=(labl: String) do json["label"] = labl
+
+ # Reference pointed by `self`.
+ fun ref: String do return json["ref"].as(String)
+
+ # Set ref
+ fun ref=(ref: String) do json["ref"] = ref
+
+ # Commit SHA pointed by `self`.
+ fun sha: String do return json["sha"].as(String)
+
+ # Set sha
+ fun sha=(sha: String) do json["sha"] = sha
+
+ # User pointed by `self`.
+ fun user: User do
+ return new User.from_json(api, json["user"].as(JsonObject))
+ end
+
+ # Set user
+ fun user=(user: User) do json["user"] = user.json
+
+ # Repo pointed by `self`.
+ fun repo: Repo do
+ return new Repo.from_json(api, json["repo"].as(JsonObject))
+ end
+
+ # Set repo
+ fun repo=(repo: Repo) do json["repo"] = repo.json