X-Git-Url: http://nitlanguage.org diff --git a/lib/github/api.nit b/lib/github/api.nit index 61a27f7..ea516fa 100644 --- a/lib/github/api.nit +++ b/lib/github/api.nit @@ -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 @@ -252,6 +252,23 @@ class GithubAPI return milestone.load_from_github end + # Get the Github issue event with `id`. + # + # Returns `null` if the event cannot be found. + # + # var api = new GithubAPI(get_github_oauth) + # var repo = api.load_repo("privat/nit") + # assert repo isa Repo + # var event = api.load_issue_event(repo, 199674194) + # assert event.actor.login == "privat" + # assert event.event == "labeled" + # assert event.labl.name == "need_review" + # assert event.issue.number == 945 + fun load_issue_event(repo: Repo, id: Int): nullable IssueEvent do + var event = new IssueEvent(self, repo, id) + return event.load_from_github + end + # Get the Github commit comment with `id`. # # Returns `null` if the comment cannot be found. @@ -267,6 +284,38 @@ class GithubAPI var comment = new CommitComment(self, repo, id) return comment.load_from_github end + + # Get the Github issue 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_issue_comment(repo, 6020149) + # assert comment.user.login == "privat" + # assert comment.created_at.to_s == "2012-05-30T20:16:54Z" + # assert comment.issue.number == 10 + fun load_issue_comment(repo: Repo, id: Int): nullable IssueComment do + var comment = new IssueComment(self, repo, id) + return comment.load_from_github + end + + # Get the Github diff 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_review_comment(repo, 21010363) + # assert comment.path == "src/modelize/modelize_property.nit" + # assert comment.original_position == 26 + # assert comment.pull.number == 945 + fun load_review_comment(repo: Repo, id: Int): nullable ReviewComment do + var comment = new ReviewComment(self, repo, id) + return comment.load_from_github + end end # Something returned by the Github API. @@ -295,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. @@ -316,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 @@ -345,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)) @@ -382,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 . + 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") @@ -443,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 @@ -572,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 @@ -605,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 @@ -633,6 +725,27 @@ class Issue return new Milestone.from_json(api, repo, milestone) 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"].to_s.to_i @@ -660,6 +773,22 @@ class Issue # Full description of the issue. fun body: String do return json["body"].to_s + # 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}").as(JsonArray) + 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["closed_by"] @@ -919,3 +1048,195 @@ class CommitComment # Path of the commented file. fun path: String do return json["path"].to_s end + +# Comments made on Github issue and pull request pages. +# +# Should be accessed from `GithubAPI::load_issue_comment`. +# +# See . +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"].to_s +end + +# Comments made on Github pull request diffs. +# +# Should be accessed from `GithubAPI::load_diff_comment`. +# +# See . +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"].to_s + + # Diff hunk. + fun diff_hunk: String do return json["diff_hunk"].to_s + + # Path of commented file. + fun path: String do return json["path"].to_s + + # Position of the comment on the file. + fun position: Int do return json["position"].to_s.to_i + + # Original position in the diff. + fun original_position: Int do return json["original_position"].to_s.to_i + + # Commit referenced by this comment. + fun commit_id: String do return json["commit_id"].to_s + + # Original commit id. + fun original_commit_id: String do return json["original_commit_id"].to_s +end + +# An event that occurs on a Github `Issue`. +# +# Should be accessed from `GithubAPI::load_issue_event`. +# +# See . +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"].to_s) + end + + # Event descriptor. + fun event: String do return json["event"].to_s + + # Commit linked to this event (if any). + fun commit_id: nullable String do + var res = json["commit_id"] + if res == null then return null + return res.to_s + end + + # Label linked to this event (if any). + fun labl: nullable Label do + var res = json["label"] + if not res isa JsonObject then return null + return new Label.from_json(api, repo, res) + end + + # User linked to this event (if any). + fun assignee: nullable User do + var res = json["assignee"] + if not res isa JsonObject then return null + return new User.from_json(api, res) + end + + # Milestone linked to this event (if any). + fun milestone: nullable Milestone do + var res = json["milestone"] + if not res isa JsonObject then return null + return new Milestone.from_json(api, repo, res) + end + + # Rename linked to this event (if any). + fun rename: nullable RenameAction do + var res = json["rename"] + if res == null then return null + return new RenameAction(res.as(JsonObject)) + 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"].to_s + + # 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 . +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