+ return load_from_github("/repos/{full_name}").as(nullable Repo)
+ end
+
+ # List of branches associated with their names.
+ fun load_repo_branches(repo: Repo): Array[Branch] do
+ message(1, "Get branches for {repo.full_name}")
+ var array = get("/repos/{repo.full_name}/branches")
+ var res = new Array[Branch]
+ if not array isa JsonArray then return res
+ var deser = deserialize(array.to_json)
+ if not deser isa Array[Object] then return res # empty array
+ for branch in deser do
+ if not branch isa Branch then continue
+ res.add branch
+ end
+ return res
+ end
+
+ # List of issues associated with their ids.
+ fun load_repo_issues(repo: Repo): Array[Issue] do
+ message(1, "Get issues for {repo.full_name}")
+ var res = new Array[Issue]
+ var issue = load_repo_last_issue(repo)
+ if issue == null then return res
+ res.add issue
+ while issue != null and issue.number > 1 do
+ issue = load_issue(repo, issue.number - 1)
+ if issue == null then continue
+ res.add issue
+ end
+ 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_repo_issues(repo: Repo, query: String): Array[Issue] do
+ query = "/search/issues?q={query} repo:{repo.full_name}"
+ var res = new Array[Issue]
+ var response = get(query)
+ if was_error then return res
+ var arr = response.as(JsonObject)["items"].as(JsonArray)
+ return deserialize(arr.to_json).as(Array[Issue])
+ end
+
+ # Get the last published issue.
+ fun load_repo_last_issue(repo: Repo): nullable Issue do
+ var array = get("/repos/{repo.full_name}/issues")
+ if not array isa JsonArray then return null
+ if array.is_empty then return null
+ var obj = array.first
+ if not obj isa JsonObject then return null
+ return deserialize(obj.to_json).as(nullable Issue)
+ end
+
+ # List of labels associated with their names.
+ fun load_repo_labels(repo: Repo): Array[Label] do
+ message(1, "Get labels for {repo.full_name}")
+ var array = get("repos/{repo.full_name}/labels")
+ if not array isa JsonArray then return new Array[Label]
+ return deserialize(array.to_json).as(Array[Label])
+ end
+
+ # List of milestones associated with their ids.
+ fun load_repo_milestones(repo: Repo): Array[Milestone] do
+ message(1, "Get milestones for {repo.full_name}")
+ var array = get("/repos/{repo.full_name}/milestones")
+ if not array isa JsonArray then return new Array[Milestone]
+ return deserialize(array.to_json).as(Array[Milestone])
+ end
+
+ # List of pull-requests associated with their ids.
+ #
+ # Implementation notes: because PR numbers are not consecutive,
+ # PR are loaded from pages.
+ # See: https://developer.github.com/v3/pulls/#list-pull-requests
+ fun load_repo_pulls(repo: Repo): Array[PullRequest] do
+ message(1, "Get pulls for {repo.full_name}")
+ var key = "/repos/{repo.full_name}"
+ var res = new Array[PullRequest]
+ var page = 1
+ loop
+ var array = get("{key}/pulls?page={page}").as(JsonArray)
+ if array.is_empty then break
+ for obj in array do
+ if not obj isa JsonObject then continue
+ var pr = deserialize(array.to_json).as(nullable PullRequest)
+ if pr == null then continue
+ res.add pr
+ end
+ page += 1
+ end
+ return res
+ end
+
+ # List of contributor related statistics.
+ fun load_repo_contrib_stats(repo: Repo): Array[ContributorStats] do
+ message(1, "Get contributor stats for {repo.full_name}")
+ var res = new Array[ContributorStats]
+ var array = get("/repos/{repo.full_name}/stats/contributors")
+ if not array isa JsonArray then return res
+ return deserialize(array.to_json).as(Array[ContributorStats])
+ end
+
+ # Get the Github branch with `name`.
+ #
+ # Returns `null` if the branch cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var branch = api.load_branch(repo, "master")
+ # assert branch.name == "master"
+ # assert branch.commit isa Commit
+ # ~~~
+ fun load_branch(repo: Repo, name: String): nullable Branch do
+ return load_from_github("/repos/{repo.full_name}/branches/{name}").as(nullable Branch)
+ end
+
+ # List all commits in `self`.
+ #
+ # This can be long depending on the branch size.
+ # Commit are returned in an unspecified order.
+ fun load_branch_commits(branch: Branch): Array[Commit] do
+ var res = new Array[Commit]
+ var done = new HashSet[String]
+ var todos = new Array[Commit]
+ todos.add branch.commit
+ loop
+ if todos.is_empty then break
+ var commit = todos.pop
+ if done.has(commit.sha) then continue
+ done.add commit.sha
+ res.add commit
+ var parents = commit.parents
+ if parents == null then continue
+ for parent in parents do
+ todos.add parent
+ end
+ end
+ return res
+ end
+
+ # Get the Github commit with `sha`.
+ #
+ # Returns `null` if the commit cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var commit = api.load_commit(repo, "64ce1f")
+ # assert commit isa Commit
+ # ~~~
+ fun load_commit(repo: Repo, sha: String): nullable Commit do
+ return load_from_github("/repos/{repo.full_name}/commits/{sha}").as(nullable Commit)
+ end
+
+ # Get the Github issue #`number`.
+ #
+ # Returns `null` if the issue cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var issue = api.load_issue(repo, 1)
+ # assert issue.title == "Doc"
+ # ~~~
+ fun load_issue(repo: Repo, number: Int): nullable Issue do
+ return load_from_github("/repos/{repo.full_name}/issues/{number}").as(nullable Issue)
+ end
+
+ # List of event on this issue.
+ fun load_issue_comments(repo: Repo, issue: Issue): Array[IssueComment] do
+ var res = new Array[IssueComment]
+ var count = issue.comments or else 0
+ var page = 1
+ loop
+ var array = get("/repos/{repo.full_name}/issues/{issue.number}/comments?page={page}")
+ if not array isa JsonArray then break
+ if array.is_empty then break
+ for obj in array do
+ if not obj isa JsonObject then continue
+ var id = obj["id"].as(Int)
+ var comment = load_issue_comment(repo, id)
+ if comment == null then continue
+ res.add(comment)
+ end
+ if res.length >= count then break
+ page += 1
+ end
+ return res
+ end
+
+ # List of events on this issue.
+ fun load_issue_events(repo: Repo, issue: Issue): Array[IssueEvent] do
+ var res = new Array[IssueEvent]
+ var key = "/repos/{repo.full_name}/issues/{issue.number}"
+ var page = 1
+ loop
+ var array = get("{key}/events?page={page}")
+ if not array isa JsonArray or array.is_empty then break
+ for obj in array do
+ if not obj isa JsonObject then continue
+ var event = deserialize(obj.to_json).as(nullable IssueEvent)
+ if event == null then continue
+ res.add event
+ end
+ page += 1
+ end
+ return res
+ end
+
+ # Get the Github pull request #`number`.
+ #
+ # Returns `null` if the pull request cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var pull = api.load_pull(repo, 1)
+ # assert pull.title == "Doc"
+ # assert pull.user.login == "Morriar"
+ # ~~~
+ fun load_pull(repo: Repo, number: Int): nullable PullRequest do
+ return load_from_github("/repos/{repo.full_name}/pulls/{number}").as(nullable PullRequest)
+ end
+
+ # Get the Github label with `name`.
+ #
+ # Returns `null` if the label cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var labl = api.load_label(repo, "ok_will_merge")
+ # assert labl != null
+ # ~~~
+ fun load_label(repo: Repo, name: String): nullable Label do
+ return load_from_github("/repos/{repo.full_name}/labels/{name}").as(nullable Label)
+ end
+
+ # Get the Github milestone with `id`.
+ #
+ # Returns `null` if the milestone cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var stone = api.load_milestone(repo, 4)
+ # assert stone.title == "v1.0prealpha"
+ # ~~~
+ fun load_milestone(repo: Repo, id: Int): nullable Milestone do
+ return load_from_github("/repos/{repo.full_name}/milestones/{id}").as(nullable Milestone)
+ end
+
+ # Get the Github issue event with `id`.
+ #
+ # Returns `null` if the event cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo isa Repo
+ # var event = api.load_issue_event(repo, 199674194)
+ # assert event isa IssueEvent
+ # assert event.actor.login == "privat"
+ # assert event.event == "labeled"
+ # assert event.labl isa Label
+ # assert event.labl.name == "need_review"
+ # ~~~
+ fun load_issue_event(repo: Repo, id: Int): nullable IssueEvent do
+ return load_from_github("/repos/{repo.full_name}/issues/events/{id}").as(nullable IssueEvent)
+ end
+
+ # Get the Github commit comment with `id`.
+ #
+ # Returns `null` if the comment cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/nit")
+ # assert repo != null
+ # var comment = api.load_commit_comment(repo, 8982707)
+ # assert comment.user.login == "Morriar"
+ # assert comment.body == "For testing purposes...\n"
+ # assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
+ # ~~~
+ fun load_commit_comment(repo: Repo, id: Int): nullable CommitComment do
+ return load_from_github("/repos/{repo.full_name}/comments/{id}").as(nullable CommitComment)
+ end
+
+ # Get the Github issue comment with `id`.
+ #
+ # Returns `null` if the comment cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/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
+ return load_from_github("/repos/{repo.full_name}/issues/comments/{id}").as(nullable IssueComment)
+ end
+
+ # Get the Github diff comment with `id`.
+ #
+ # Returns `null` if the comment cannot be found.
+ #
+ # ~~~nitish
+ # var api = new GithubAPI(get_github_oauth)
+ # var repo = api.load_repo("nitlang/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
+ return load_from_github("/repos/{repo.full_name}/pulls/comments/{id}").as(nullable ReviewComment)