1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Nit object oriented interface to [Github api](https://developer.github.com/v3/).
17 # This modules reifies Github API elements as Nit classes.
19 # For most use-cases you need to use the `GithubAPI` client.
24 # Client to Github API
26 # To access the API you need an instance of a `GithubAPI` client.
29 # # Get Github authentification token.
30 # var token = get_github_oauth
31 # assert not token.is_empty
34 # var api = new GithubAPI(token)
37 # The API client allows you to get Github API entities.
40 # var repo = api.load_repo("nitlang/nit")
42 # assert repo.name == "nit"
44 # var user = api.load_user("Morriar")
46 # assert user.login == "Morriar"
50 # Github API OAuth token
52 # To access your private ressources, you must
53 # [authenticate](https://developer.github.com/guides/basics-of-authentication/).
55 # For client applications, Github recommands to use the
56 # [OAuth tokens](https://developer.github.com/v3/oauth/) authentification method.
60 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
61 # associated to the key.
64 # Github API base url.
66 # Default is `https://api.github.com` and should not be changed.
67 var api_url
= "https://api.github.com"
69 # User agent used for HTTP requests.
71 # Default is `nit_github_api`.
73 # See <https://developer.github.com/v3/#user-agent-required>
74 var user_agent
= "nit_github_api"
78 # Internal Curl instance used to perform API calls.
79 private var ghcurl
: GithubCurl is noinit
83 # * `0`: only errors (default)
85 var verbose_lvl
= 0 is public
writable
88 ghcurl
= new GithubCurl(auth
, user_agent
)
91 # Execute a GET request on Github API.
93 # This method returns raw json data.
94 # See other `load_*` methods to use more expressive types.
96 # var api = new GithubAPI(get_github_oauth)
97 # var obj = api.get("repos/nitlang/nit")
98 # assert obj isa JsonObject
99 # assert obj["name"] == "nit"
101 # Returns `null` in case of `error`.
103 # obj = api.get("foo/bar/baz")
105 # assert api.was_error
106 # var err = api.last_error
107 # assert err isa GithubError
108 # assert err.name == "GithubAPIError"
109 # assert err.message == "Not Found"
110 fun get
(path
: String): nullable Jsonable do
111 path
= sanitize_uri
(path
)
112 var res
= ghcurl
.get_and_parse
("{api_url}/{path}")
113 if res
isa Error then
122 # Display a message depending on `verbose_lvl`.
123 fun message
(lvl
: Int, message
: String) do
124 if lvl
<= verbose_lvl
then print message
127 # Escape `uri` in an acceptable format for Github.
128 private fun sanitize_uri
(uri
: String): String do
129 # TODO better URI escape.
130 return uri
.replace
(" ", "%20")
133 # Last error occured during Github API communications.
134 var last_error
: nullable Error = null is public
writable
136 # Does the last request provoqued an error?
137 var was_error
= false is protected writable
139 # Load the json object from Github.
140 # See `GithubEntity::load_from_github`.
141 protected fun load_from_github
(key
: String): JsonObject do
142 message
(1, "Get {key} (github)")
144 if was_error
then return new JsonObject
145 return res
.as(JsonObject)
148 # Get the Github user with `login`
150 # Loads the `User` from the API or returns `null` if the user cannot be found.
152 # var api = new GithubAPI(get_github_oauth)
153 # var user = api.load_user("Morriar")
154 # assert user.login == "Morriar"
155 fun load_user
(login
: String): nullable User do
156 var user
= new User(self, login
)
157 return user
.load_from_github
160 # Get the Github repo with `full_name`.
162 # Loads the `Repo` from the API or returns `null` if the repo cannot be found.
164 # var api = new GithubAPI(get_github_oauth)
165 # var repo = api.load_repo("nitlang/nit")
166 # assert repo.name == "nit"
167 # assert repo.owner.login == "nitlang"
168 # assert repo.default_branch.name == "master"
169 fun load_repo
(full_name
: String): nullable Repo do
170 var repo
= new Repo(self, full_name
)
171 return repo
.load_from_github
174 # Get the Github branch with `name`.
176 # Returns `null` if the branch cannot be found.
178 # var api = new GithubAPI(get_github_oauth)
179 # var repo = api.load_repo("nitlang/nit")
180 # assert repo != null
181 # var branch = api.load_branch(repo, "master")
182 # assert branch.name == "master"
183 # assert branch.commit isa Commit
184 fun load_branch
(repo
: Repo, name
: String): nullable Branch do
185 var branch
= new Branch(self, repo
, name
)
186 return branch
.load_from_github
189 # Get the Github commit with `sha`.
191 # Returns `null` if the commit cannot be found.
193 # var api = new GithubAPI(get_github_oauth)
194 # var repo = api.load_repo("nitlang/nit")
195 # assert repo != null
196 # var commit = api.load_commit(repo, "64ce1f")
197 # assert commit isa Commit
198 fun load_commit
(repo
: Repo, sha
: String): nullable Commit do
199 var commit
= new Commit(self, repo
, sha
)
200 return commit
.load_from_github
203 # Get the Github issue #`number`.
205 # Returns `null` if the issue cannot be found.
207 # var api = new GithubAPI(get_github_oauth)
208 # var repo = api.load_repo("nitlang/nit")
209 # assert repo != null
210 # var issue = api.load_issue(repo, 1)
211 # assert issue.title == "Doc"
212 fun load_issue
(repo
: Repo, number
: Int): nullable Issue do
213 var issue
= new Issue(self, repo
, number
)
214 return issue
.load_from_github
217 # Get the Github pull request #`number`.
219 # Returns `null` if the pull request cannot be found.
221 # var api = new GithubAPI(get_github_oauth)
222 # var repo = api.load_repo("nitlang/nit")
223 # assert repo != null
224 # var pull = api.load_pull(repo, 1)
225 # assert pull.title == "Doc"
226 # assert pull.user.login == "Morriar"
227 fun load_pull
(repo
: Repo, number
: Int): nullable PullRequest do
228 var pull
= new PullRequest(self, repo
, number
)
229 return pull
.load_from_github
232 # Get the Github label with `name`.
234 # Returns `null` if the label cannot be found.
236 # var api = new GithubAPI(get_github_oauth)
237 # var repo = api.load_repo("nitlang/nit")
238 # assert repo != null
239 # var labl = api.load_label(repo, "ok_will_merge")
240 # assert labl != null
241 fun load_label
(repo
: Repo, name
: String): nullable Label do
242 var labl
= new Label(self, repo
, name
)
243 return labl
.load_from_github
246 # Get the Github milestone with `id`.
248 # Returns `null` if the milestone cannot be found.
250 # var api = new GithubAPI(get_github_oauth)
251 # var repo = api.load_repo("nitlang/nit")
252 # assert repo != null
253 # var stone = api.load_milestone(repo, 4)
254 # assert stone.title == "v1.0prealpha"
255 fun load_milestone
(repo
: Repo, id
: Int): nullable Milestone do
256 var milestone
= new Milestone(self, repo
, id
)
257 return milestone
.load_from_github
260 # Get the Github issue event with `id`.
262 # Returns `null` if the event cannot be found.
264 # var api = new GithubAPI(get_github_oauth)
265 # var repo = api.load_repo("nitlang/nit")
266 # assert repo isa Repo
267 # var event = api.load_issue_event(repo, 199674194)
268 # assert event.actor.login == "privat"
269 # assert event.event == "labeled"
270 # assert event.labl.name == "need_review"
271 # assert event.issue.number == 945
272 fun load_issue_event
(repo
: Repo, id
: Int): nullable IssueEvent do
273 var event
= new IssueEvent(self, repo
, id
)
274 return event
.load_from_github
277 # Get the Github commit comment with `id`.
279 # Returns `null` if the comment cannot be found.
281 # var api = new GithubAPI(get_github_oauth)
282 # var repo = api.load_repo("nitlang/nit")
283 # assert repo != null
284 # var comment = api.load_commit_comment(repo, 8982707)
285 # assert comment.user.login == "Morriar"
286 # assert comment.body == "For testing purposes..."
287 # assert comment.commit.sha == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
288 fun load_commit_comment
(repo
: Repo, id
: Int): nullable CommitComment do
289 var comment
= new CommitComment(self, repo
, id
)
290 return comment
.load_from_github
293 # Get the Github issue comment with `id`.
295 # Returns `null` if the comment cannot be found.
297 # var api = new GithubAPI(get_github_oauth)
298 # var repo = api.load_repo("nitlang/nit")
299 # assert repo != null
300 # var comment = api.load_issue_comment(repo, 6020149)
301 # assert comment.user.login == "privat"
302 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
303 # assert comment.issue.number == 10
304 fun load_issue_comment
(repo
: Repo, id
: Int): nullable IssueComment do
305 var comment
= new IssueComment(self, repo
, id
)
306 return comment
.load_from_github
309 # Get the Github diff comment with `id`.
311 # Returns `null` if the comment cannot be found.
313 # var api = new GithubAPI(get_github_oauth)
314 # var repo = api.load_repo("nitlang/nit")
315 # assert repo != null
316 # var comment = api.load_review_comment(repo, 21010363)
317 # assert comment.path == "src/modelize/modelize_property.nit"
318 # assert comment.original_position == 26
319 # assert comment.pull.number == 945
320 fun load_review_comment
(repo
: Repo, id
: Int): nullable ReviewComment do
321 var comment
= new ReviewComment(self, repo
, id
)
322 return comment
.load_from_github
326 # Something returned by the Github API.
328 # Mainly a Nit wrapper around a JSON objet.
329 abstract class GithubEntity
331 # Github API instance.
334 # FIXME constructor should be private
336 # Key used to access this entity from Github api base.
337 fun key
: String is abstract
339 # JSON representation of `self`.
341 # This is the same json structure than used by Github API.
342 var json
: JsonObject is noinit
, protected writable
344 # Load `json` from Github API.
345 private fun load_from_github
: nullable SELF do
346 json
= api
.load_from_github
(key
)
347 if api
.was_error
then return null
351 redef fun to_s
do return json
.to_json
354 fun html_url
: String do return json
["html_url"].as(String)
357 fun html_url
=(url
: String) do json
["html_url"] = url
362 # Provides access to [Github user data](https://developer.github.com/v3/users/).
363 # Should be accessed from `GithubAPI::load_user`.
367 redef var key
is lazy
do return "users/{login}"
372 # Init `self` from a `json` object.
373 init from_json
(api
: GithubAPI, json
: JsonObject) do
374 init(api
, json
["login"].as(String))
378 # Avatar image url for this user.
379 fun avatar_url
: String do return json
["avatar_url"].as(String)
382 fun avatar_url
=(url
: String) do json
["avatar_url"] = url
385 # A Github repository.
387 # Provides access to [Github repo data](https://developer.github.com/v3/repos/).
388 # Should be accessed from `GithubAPI::load_repo`.
392 redef var key
is lazy
do return "repos/{full_name}"
394 # Repo full name on Github.
395 var full_name
: String
397 # Init `self` from a `json` object.
398 init from_json
(api
: GithubAPI, json
: JsonObject) do
399 init(api
, json
["full_name"].as(String))
403 # Repo short name on Github.
404 fun name
: String do return json
["name"].as(String)
407 fun name
=(name
: String) do json
["name"] = name
409 # Get the repo owner.
410 fun owner
: User do return new User.from_json
(api
, json
["owner"].as(JsonObject))
413 fun owner
=(owner
: User) do json
["owner"] = owner
.json
415 # List of branches associated with their names.
416 fun branches
: Map[String, Branch] do
417 api
.message
(1, "Get branches for {full_name}")
418 var array
= api
.get
("repos/{full_name}/branches")
419 var res
= new HashMap[String, Branch]
420 if not array
isa JsonArray then return res
422 if not obj
isa JsonObject then continue
423 var name
= obj
["name"].as(String)
424 res
[name
] = new Branch.from_json
(api
, self, obj
)
429 # List of issues associated with their ids.
430 fun issues
: Map[Int, Issue] do
431 api
.message
(1, "Get issues for {full_name}")
432 var res
= new HashMap[Int, Issue]
433 var issue
= last_issue
434 if issue
== null then return res
435 res
[issue
.number
] = issue
436 while issue
.number
> 1 do
437 issue
= api
.load_issue
(self, issue
.number
- 1)
438 assert issue
isa Issue
439 res
[issue
.number
] = issue
444 # Search issues in this repo form an advanced query.
449 # var issues = repo.search_issues("is:open label:need_review")
452 # See <https://developer.github.com/v3/search/#search-issues>.
453 fun search_issues
(query
: String): nullable Array[Issue] do
454 query
= "search/issues?q={query} repo:{full_name}"
455 var response
= api
.get
(query
)
456 if api
.was_error
then return null
457 var arr
= response
.as(JsonObject)["items"].as(JsonArray)
458 var res
= new Array[Issue]
460 res
.add
new Issue.from_json
(api
, self, obj
.as(JsonObject))
465 # Get the last published issue.
466 fun last_issue
: nullable Issue do
467 var array
= api
.get
("repos/{full_name}/issues")
468 if not array
isa JsonArray then return null
469 if array
.is_empty
then return null
470 var obj
= array
.first
471 if not obj
isa JsonObject then return null
472 return new Issue.from_json
(api
, self, obj
)
475 # List of labels associated with their names.
476 fun labels
: Map[String, Label] do
477 api
.message
(1, "Get labels for {full_name}")
478 var array
= api
.get
("repos/{full_name}/labels")
479 var res
= new HashMap[String, Label]
480 if not array
isa JsonArray then return res
482 if not obj
isa JsonObject then continue
483 var name
= obj
["name"].as(String)
484 res
[name
] = new Label.from_json
(api
, self, obj
)
489 # List of milestones associated with their ids.
490 fun milestones
: Map[Int, Milestone] do
491 api
.message
(1, "Get milestones for {full_name}")
492 var array
= api
.get
("repos/{full_name}/milestones")
493 var res
= new HashMap[Int, Milestone]
494 if array
isa JsonArray then
496 if not obj
isa JsonObject then continue
497 var number
= obj
["number"].as(Int)
498 res
[number
] = new Milestone.from_json
(api
, self, obj
)
504 # List of pull-requests associated with their ids.
506 # Implementation notes: because PR numbers are not consecutive,
507 # PR are loaded from pages.
508 # See: https://developer.github.com/v3/pulls/#list-pull-requests
509 fun pulls
: Map[Int, PullRequest] do
510 api
.message
(1, "Get pulls for {full_name}")
511 var res
= new HashMap[Int, PullRequest]
513 var array
= api
.get
("{key}/pulls?page={page}").as(JsonArray)
514 while not array
.is_empty
do
516 if not obj
isa JsonObject then continue
517 var number
= obj
["number"].as(Int)
518 res
[number
] = new PullRequest.from_json
(api
, self, obj
)
521 array
= api
.get
("{key}/pulls?page={page}").as(JsonArray)
526 # List of contributor related statistics.
527 fun contrib_stats
: Array[ContributorStats] do
528 api
.message
(1, "Get contributor stats for {full_name}")
529 var res
= new Array[ContributorStats]
530 var array
= api
.get
("{key}/stats/contributors")
531 if array
isa JsonArray then
533 res
.add
new ContributorStats.from_json
(api
, obj
.as(JsonObject))
539 # Repo default branch.
540 fun default_branch
: Branch do
541 var name
= json
["default_branch"].as(String)
542 var branch
= api
.load_branch
(self, name
)
543 assert branch
isa Branch
547 # Set the default branch
548 fun default_branch
=(branch
: Branch) do json
["default_branch"] = branch
.json
551 # A `RepoEntity` is something contained in a `Repo`.
552 abstract class RepoEntity
555 # Repo that contains `self`.
558 # Init `self` from a `json` object.
559 init from_json
(api
: GithubAPI, repo
: Repo, json
: JsonObject) do
567 # Should be accessed from `GithubAPI::load_branch`.
569 # See <https://developer.github.com/v3/repos/#list-branches>.
573 redef var key
is lazy
do return "{repo.key}/branches/{name}"
578 redef init from_json
(api
, repo
, json
) do
579 self.name
= json
["name"].as(String)
583 # Get the last commit of `self`.
584 fun commit
: Commit do return new Commit.from_json
(api
, repo
, json
["commit"].as(JsonObject))
586 # Set the last commit
587 fun commit
=(commit
: Commit) do json
["commit"] = commit
.json
589 # List all commits in `self`.
591 # This can be long depending on the branch size.
592 # Commit are returned in an unspecified order.
593 fun commits
: Array[Commit] do
594 var res
= new Array[Commit]
595 var done
= new HashSet[String]
596 var todos
= new Array[Commit]
598 while not todos
.is_empty
do
599 var commit
= todos
.pop
600 if done
.has
(commit
.sha
) then continue
603 for parent
in commit
.parents
do
613 # Should be accessed from `GithubAPI::load_commit`.
615 # See <https://developer.github.com/v3/repos/commits/>.
619 redef var key
is lazy
do return "{repo.key}/commits/{sha}"
624 redef init from_json
(api
, repo
, json
) do
625 self.sha
= json
["sha"].as(String)
629 # Parent commits of `self`.
630 fun parents
: Array[Commit] do
631 var res
= new Array[Commit]
632 var parents
= json
.get_or_null
("parents")
633 if not parents
isa JsonArray then return res
634 for obj
in parents
do
635 if not obj
isa JsonObject then continue
636 res
.add
(api
.load_commit
(repo
, obj
["sha"].as(String)).as(not null))
641 # Set parent commits.
642 fun parents
=(parents
: Array[Commit]) do
643 var res
= new JsonArray
644 for parent
in parents
do res
.add parent
.json
645 json
["parents"] = res
648 # Author of the commit.
649 fun author
: nullable User do
650 var user
= json
.get_or_null
("author")
651 if user
isa JsonObject then return new User.from_json
(api
, user
)
656 fun author
=(user
: nullable User) do
658 json
["author"] = null
660 json
["author"] = user
.json
664 # Committer of the commit.
665 fun committer
: nullable User do
666 var user
= json
.get_or_null
("author")
667 if user
isa JsonObject then return new User.from_json
(api
, user
)
671 # Set commit committer.
672 fun committer
=(user
: nullable User) do
674 json
["committer"] = null
676 json
["committer"] = user
.json
680 # Authoring date as ISODate.
681 fun author_date
: ISODate do
682 var commit
= json
["commit"].as(JsonObject)
683 var author
= commit
["author"].as(JsonObject)
684 return new ISODate.from_string
(author
["date"].as(String))
687 # Commit date as ISODate.
688 fun commit_date
: ISODate do
689 var commit
= json
["commit"].as(JsonObject)
690 var author
= commit
["committer"].as(JsonObject)
691 return new ISODate.from_string
(author
["date"].as(String))
694 # List files staged in this commit.
695 fun files
: Array[GithubFile] do
696 var res
= new Array[GithubFile]
697 var files
= json
.get_or_null
("files")
698 if not files
isa JsonArray then return res
700 res
.add
(new GithubFile(obj
.as(JsonObject)))
706 fun files
=(files
: Array[GithubFile]) do
707 var res
= new JsonArray
708 for file
in files
do res
.add file
.json
713 fun message
: String do return json
["commit"].as(JsonObject)["message"].as(String)
718 # Should be accessed from `GithubAPI::load_issue`.
720 # See <https://developer.github.com/v3/issues/>.
724 redef var key
is lazy
do return "{repo.key}/issues/{number}"
729 redef init from_json
(api
, repo
, json
) do
730 self.number
= json
["number"].as(Int)
735 fun id
: Int do return json
["id"].as(Int)
738 fun id
=(id
: Int) do json
["id"] = id
741 fun title
: String do return json
["title"].as(String)
744 fun title
=(title
: String) do json
["title"] = title
746 # User that created this issue.
747 fun user
: User do return new User.from_json
(api
, json
["user"].as(JsonObject))
750 fun user
=(user
: User) do json
["user"] = user
.json
752 # List of labels on this issue associated to their names.
753 fun labels
: Map[String, Label] do
754 var res
= new HashMap[String, Label]
755 var lbls
= json
.get_or_null
("labels")
756 if not lbls
isa JsonArray then return res
758 if not obj
isa JsonObject then continue
759 var name
= obj
["name"].as(String)
760 res
[name
] = new Label.from_json
(api
, repo
, obj
)
765 # State of the issue on Github.
766 fun state
: String do return json
["state"].as(String)
768 # Set the state of this issue.
769 fun state
=(state
: String) do json
["state"] = state
771 # Is the issue locked?
772 fun locked
: Bool do return json
["locked"].as(Bool)
774 # Set issue locked state.
775 fun locked
=(locked
: Bool) do json
["locked"] = locked
777 # Assigned `User` (if any).
778 fun assignee
: nullable User do
779 var assignee
= json
.get_or_null
("assignee")
780 if assignee
isa JsonObject then return new User.from_json
(api
, assignee
)
784 # Set issue assignee.
785 fun assignee
=(user
: nullable User) do
787 json
["assignee"] = null
789 json
["assignee"] = user
.json
793 # `Milestone` (if any).
794 fun milestone
: nullable Milestone do
795 var milestone
= json
.get_or_null
("milestone")
796 if milestone
isa JsonObject then return new Milestone.from_json
(api
, repo
, milestone
)
800 # Set issue milestone.
801 fun milestone
=(milestone
: nullable Milestone) do
802 if milestone
== null then
803 json
["milestone"] = null
805 json
["milestone"] = milestone
.json
809 # List of comments made on this issue.
810 fun comments
: Array[IssueComment] do
811 var res
= new Array[IssueComment]
812 var count
= comments_count
814 var array
= api
.get
("{key}/comments?page={page}")
815 if not array
isa JsonArray then
818 while not array
.is_empty
and res
.length
< count
do
820 if not obj
isa JsonObject then continue
821 var id
= obj
["id"].as(Int)
822 var comment
= api
.load_issue_comment
(repo
, id
)
823 if comment
== null then continue
827 var json
= api
.get
("{key}/comments?page={page}")
828 if not json
isa JsonArray then
836 # Number of comments on this issue.
837 fun comments_count
: Int do return json
["comments"].as(Int)
839 # Creation time in ISODate format.
840 fun created_at
: ISODate do return new ISODate.from_string
(json
["created_at"].as(String))
842 # Set issue creation time.
843 fun created_at
=(created_at
: nullable ISODate) do
844 if created_at
== null then
845 json
["created_at"] = null
847 json
["created_at"] = created_at
.to_s
851 # Last update time in ISODate format (if any).
852 fun updated_at
: nullable ISODate do
853 var res
= json
.get_or_null
("updated_at")
854 if res
isa String then return new ISODate.from_string
(res
)
858 # Set issue last update time.
859 fun updated_at
=(updated_at
: nullable ISODate) do
860 if updated_at
== null then
861 json
["updated_at"] = null
863 json
["updated_at"] = updated_at
.to_s
867 # Close time in ISODate format (if any).
868 fun closed_at
: nullable ISODate do
869 var res
= json
.get_or_null
("closed_at")
870 if res
isa String then return new ISODate.from_string
(res
)
874 # Set issue close time.
875 fun closed_at
=(closed_at
: nullable ISODate) do
876 if closed_at
== null then
877 json
["closed_at"] = null
879 json
["closed_at"] = closed_at
.to_s
883 # TODO link to pull request
885 # Full description of the issue.
886 fun body
: String do return json
["body"].as(String)
888 # Set description body
889 fun body
=(body
: String) do json
["body"] = body
891 # List of events on this issue.
892 fun events
: Array[IssueEvent] do
893 var res
= new Array[IssueEvent]
895 var array
= api
.get
("{key}/events?page={page}")
896 if not array
isa JsonArray then return res
897 while not array
.is_empty
do
899 if not obj
isa JsonObject then continue
900 res
.add
new IssueEvent.from_json
(api
, repo
, obj
)
903 array
= api
.get
("{key}/events?page={page}").as(JsonArray)
908 # User that closed this issue (if any).
909 fun closed_by
: nullable User do
910 var closer
= json
.get_or_null
("closed_by")
911 if closer
isa JsonObject then return new User.from_json
(api
, closer
)
915 # Set user that closed the issue.
916 fun closed_by
=(user
: nullable User) do
918 json
["closed_by"] = null
920 json
["closed_by"] = user
.json
924 # Is this issue linked to a pull request?
925 fun is_pull_request
: Bool do return json
.has_key
("pull_request")
928 # A Github pull request.
930 # Should be accessed from `GithubAPI::load_pull`.
932 # PullRequest are basically Issues with more data.
933 # See <https://developer.github.com/v3/pulls/>.
937 redef var key
is lazy
do return "{repo.key}/pulls/{number}"
939 # Merge time in ISODate format (if any).
940 fun merged_at
: nullable ISODate do
941 var res
= json
.get_or_null
("merged_at")
942 if res
isa String then return new ISODate.from_string
(res
)
946 # Set pull request merge time.
947 fun merged_at
=(merged_at
: nullable ISODate) do
948 if merged_at
== null then
949 json
["merged_at"] = null
951 json
["merged_at"] = merged_at
.to_s
956 fun merge_commit_sha
: String do return json
["merge_commit_sha"].as(String)
958 # Set merge_commit_sha
959 fun merge_commit_sha
=(sha
: String) do json
["merge_commit_sha"] = sha
961 # Count of comments made on the pull request diff.
962 fun review_comments
: Int do return json
["review_comments"].as(Int)
964 # Set review_comments
965 fun review_comments
=(count
: Int) do json
["review_comments"] = count
967 # Pull request head (can be a commit SHA or a branch name).
969 var json
= json
["head"].as(JsonObject)
970 return new PullRef(api
, json
)
974 fun head
=(head
: PullRef) do json
["head"] = head
.json
976 # Pull request base (can be a commit SHA or a branch name).
978 var json
= json
["base"].as(JsonObject)
979 return new PullRef(api
, json
)
983 fun base
=(base
: PullRef) do json
["base"] = base
.json
985 # Is this pull request merged?
986 fun merged
: Bool do return json
["merged"].as(Bool)
989 fun merged
=(merged
: Bool) do json
["merged"] = merged
991 # Is this pull request mergeable?
992 fun mergeable
: Bool do return json
["mergeable"].as(Bool)
995 fun mergeable
=(mergeable
: Bool) do json
["mergeable"] = mergeable
997 # Mergeable state of this pull request.
999 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
1000 fun mergeable_state
: Int do return json
["mergeable_state"].as(Int)
1002 # Set mergeable_state
1003 fun mergeable_state
=(mergeable_state
: Int) do json
["mergeable_state"] = mergeable_state
1005 # User that merged this pull request (if any).
1006 fun merged_by
: nullable User do
1007 var merger
= json
.get_or_null
("merged_by")
1008 if merger
isa JsonObject then return new User.from_json
(api
, merger
)
1013 fun merged_by
=(merged_by
: nullable User) do
1014 if merged_by
== null then
1015 json
["merged_by"] = null
1017 json
["merged_by"] = merged_by
.json
1021 # Count of commits in this pull request.
1022 fun commits
: Int do return json
["commits"].as(Int)
1025 fun commits
=(commits
: Int) do json
["commits"] = commits
1028 fun additions
: Int do return json
["additions"].as(Int)
1031 fun additions
=(additions
: Int) do json
["additions"] = additions
1033 # Deleted line count.
1034 fun deletions
: Int do return json
["deletions"].as(Int)
1037 fun deletions
=(deletions
: Int) do json
["deletions"] = deletions
1039 # Changed files count.
1040 fun changed_files
: Int do return json
["changed_files"].as(Int)
1043 fun changed_files
=(changed_files
: Int) do json
["changed_files"] = changed_files
1046 # A pull request reference (used for head and base).
1049 # Api instance that maintains self.
1052 # JSON representation.
1053 var json
: JsonObject
1055 # Label pointed by `self`.
1056 fun labl
: String do return json
["label"].as(String)
1059 fun labl
=(labl
: String) do json
["label"] = labl
1061 # Reference pointed by `self`.
1062 fun ref
: String do return json
["ref"].as(String)
1065 fun ref
=(ref
: String) do json
["ref"] = ref
1067 # Commit SHA pointed by `self`.
1068 fun sha
: String do return json
["sha"].as(String)
1071 fun sha
=(sha
: String) do json
["sha"] = sha
1073 # User pointed by `self`.
1075 return new User.from_json
(api
, json
["user"].as(JsonObject))
1079 fun user
=(user
: User) do json
["user"] = user
.json
1081 # Repo pointed by `self`.
1083 return new Repo.from_json
(api
, json
["repo"].as(JsonObject))
1087 fun repo
=(repo
: Repo) do json
["repo"] = repo
.json
1092 # Should be accessed from `GithubAPI::load_label`.
1094 # See <https://developer.github.com/v3/issues/labels/>.
1098 redef var key
is lazy
do return "{repo.key}/labels/{name}"
1103 redef init from_json
(api
, repo
, json
) do
1104 self.name
= json
["name"].as(String)
1109 fun color
: String do return json
["color"].as(String)
1112 fun color
=(color
: String) do json
["color"] = color
1115 # A Github milestone.
1117 # Should be accessed from `GithubAPI::load_milestone`.
1119 # See <https://developer.github.com/v3/issues/milestones/>.
1123 redef var key
is lazy
do return "{repo.key}/milestones/{number}"
1125 # The milestone id on Github.
1128 redef init from_json
(api
, repo
, json
) do
1130 self.number
= json
["number"].as(Int)
1134 fun title
: String do return json
["title"].as(String)
1137 fun title
=(title
: String) do json
["title"] = title
1139 # Milestone long description.
1140 fun description
: String do return json
["description"].as(String)
1143 fun description
=(description
: String) do json
["description"] = description
1145 # Count of opened issues linked to this milestone.
1146 fun open_issues
: Int do return json
["open_issues"].as(Int)
1149 fun open_issues
=(open_issues
: Int) do json
["open_issues"] = open_issues
1151 # Count of closed issues linked to this milestone.
1152 fun closed_issues
: Int do return json
["closed_issues"].as(Int)
1155 fun closed_issues
=(closed_issues
: Int) do json
["closed_issues"] = closed_issues
1158 fun state
: String do return json
["state"].as(String)
1161 fun state
=(state
: String) do json
["state"] = state
1163 # Creation time in ISODate format.
1164 fun created_at
: ISODate do
1165 return new ISODate.from_string
(json
["created_at"].as(String))
1169 fun created_at
=(created_at
: ISODate) do json
["created_at"] = created_at
.to_s
1171 # User that created this milestone.
1172 fun creator
: User do
1173 return new User.from_json
(api
, json
["creator"].as(JsonObject))
1177 fun creator
=(creator
: User) do json
["creator"] = creator
.json
1179 # Due time in ISODate format (if any).
1180 fun due_on
: nullable ISODate do
1181 var res
= json
.get_or_null
("updated_at")
1182 if res
isa String then return new ISODate.from_string
(res
)
1187 fun due_on
=(due_on
: nullable ISODate) do
1188 if due_on
== null then
1189 json
["due_on"] = null
1191 json
["due_on"] = due_on
.to_s
1195 # Update time in ISODate format (if any).
1196 fun updated_at
: nullable ISODate do
1197 var res
= json
.get_or_null
("updated_at")
1198 if res
isa String then return new ISODate.from_string
(res
)
1203 fun updated_at
=(updated_at
: nullable ISODate) do
1204 if updated_at
== null then
1205 json
["updated_at"] = null
1207 json
["updated_at"] = updated_at
.to_s
1211 # Close time in ISODate format (if any).
1212 fun closed_at
: nullable ISODate do
1213 var res
= json
.get_or_null
("closed_at")
1214 if res
isa String then return new ISODate.from_string
(res
)
1219 fun closed_at
=(closed_at
: nullable ISODate) do
1220 if closed_at
== null then
1221 json
["closed_at"] = null
1223 json
["closed_at"] = closed_at
.to_s
1230 # There is two kinds of comments:
1232 # * `CommitComment` are made on a commit page.
1233 # * `IssueComment` are made on an issue or pull request page.
1234 # * `ReviewComment` are made on the diff associated to a pull request.
1235 abstract class Comment
1238 # Identifier of this comment.
1241 redef init from_json
(api
, repo
, json
) do
1242 self.id
= json
["id"].as(Int)
1246 # User that made this comment.
1248 return new User.from_json
(api
, json
["user"].as(JsonObject))
1252 fun user
=(user
: User) do json
["user"] = user
.json
1254 # Creation time in ISODate format.
1255 fun created_at
: ISODate do
1256 return new ISODate.from_string
(json
["created_at"].as(String))
1260 fun created_at
=(created_at
: ISODate) do json
["created_at"] = created_at
.to_s
1262 # Last update time in ISODate format (if any).
1263 fun updated_at
: nullable ISODate do
1264 var res
= json
.get_or_null
("updated_at")
1265 if res
isa String then return new ISODate.from_string
(res
)
1270 fun updated_at
=(updated_at
: nullable ISODate) do
1271 if updated_at
== null then
1272 json
["updated_at"] = null
1274 json
["updated_at"] = updated_at
.to_s
1278 # Comment body text.
1279 fun body
: String do return json
["body"].as(String)
1282 fun body
=(body
: String) do json
["body"] = body
1284 # Does the comment contain an acknowledgement (+1)
1287 return body
.has
("\\+1\\b".to_re
) or body
.has
(":+1:") or body
.has
(":shipit:")
1291 # A comment made on a commit.
1295 redef var key
is lazy
do return "{repo.key}/comments/{id}"
1298 fun commit
: Commit do
1299 return api
.load_commit
(repo
, json
["commit_id"].as(String)).as(not null)
1303 fun commit
=(commit
: Commit) do json
["commit_id"] = commit
.json
1305 # Position of the comment on the line.
1306 fun position
: nullable String do
1307 var res
= json
.get_or_null
("position")
1308 if res
isa String then return res
1313 fun position
=(position
: nullable String) do json
["position"] = position
1315 # Line of the comment.
1316 fun line
: nullable String do
1317 var res
= json
.get_or_null
("line")
1318 if res
isa String then return res
1323 fun line
=(line
: nullable String) do json
["line"] = line
1325 # Path of the commented file.
1326 fun path
: String do return json
["path"].as(String)
1329 fun path
=(path
: String) do json
["path"] = path
1332 # Comments made on Github issue and pull request pages.
1334 # Should be accessed from `GithubAPI::load_issue_comment`.
1336 # See <https://developer.github.com/v3/issues/comments/>.
1340 redef var key
is lazy
do return "{repo.key}/issues/comments/{id}"
1342 # Issue that contains `self`.
1344 var number
= issue_url
.split
("/").last
.to_i
1345 return api
.load_issue
(repo
, number
).as(not null)
1348 # Link to the issue document on API.
1349 fun issue_url
: String do return json
["issue_url"].as(String)
1352 fun issue_url
=(issue_url
: String) do json
["issue_url"] = issue_url
1355 # Comments made on Github pull request diffs.
1357 # Should be accessed from `GithubAPI::load_diff_comment`.
1359 # See <https://developer.github.com/v3/pulls/comments/>.
1363 redef var key
is lazy
do return "{repo.key}/pulls/comments/{id}"
1365 # Pull request that contains `self`.
1366 fun pull
: PullRequest do
1367 var number
= pull_request_url
.split
("/").last
.to_i
1368 return api
.load_pull
(repo
, number
).as(not null)
1371 # Link to the pull request on API.
1372 fun pull_request_url
: String do return json
["pull_request_url"].as(String)
1374 # Set pull_request_url.
1375 fun pull_request_url
=(pull_request_url
: String) do json
["pull_request_url"] = pull_request_url
1378 fun diff_hunk
: String do return json
["diff_hunk"].as(String)
1381 fun diff_hunk
=(diff_hunk
: String) do json
["diff_hunk"] = diff_hunk
1383 # Path of commented file.
1384 fun path
: String do return json
["path"].as(String)
1387 fun path
=(path
: String) do json
["path"] = path
1389 # Position of the comment on the file.
1390 fun position
: Int do return json
["position"].as(Int)
1393 fun position
=(position
: Int) do json
["position"] = position
1395 # Original position in the diff.
1396 fun original_position
: Int do return json
["original_position"].as(Int)
1398 # Set original_position.
1399 fun original_position
=(original_position
: Int) do json
["original_position"] = original_position
1401 # Commit referenced by this comment.
1402 fun commit_id
: String do return json
["commit_id"].as(String)
1405 fun commit_id
=(commit_id
: String) do json
["commit_id"] = commit_id
1407 # Original commit id.
1408 fun original_commit_id
: String do return json
["original_commit_id"].as(String)
1410 # Set original_commit_id.
1411 fun original_commit_id
=(commit_id
: String) do json
["original_commit_id"] = commit_id
1414 # An event that occurs on a Github `Issue`.
1416 # Should be accessed from `GithubAPI::load_issue_event`.
1418 # See <https://developer.github.com/v3/issues/events/>.
1422 redef var key
is lazy
do return "{repo.key}/issues/events/{id}"
1424 # Event id on Github.
1427 redef init from_json
(api
, repo
, json
) do
1428 self.id
= json
["id"].as(Int)
1432 # Issue that contains `self`.
1434 return new Issue.from_json
(api
, repo
, json
["issue"].as(JsonObject))
1438 fun issue
=(issue
: Issue) do json
["issue"] = issue
.json
1440 # User that initiated the event.
1442 return new User.from_json
(api
, json
["actor"].as(JsonObject))
1446 fun actor
=(actor
: User) do json
["actor"] = actor
.json
1448 # Creation time in ISODate format.
1449 fun created_at
: ISODate do
1450 return new ISODate.from_string
(json
["created_at"].as(String))
1454 fun created_at
=(created_at
: ISODate) do json
["created_at"] = created_at
.to_s
1457 fun event
: String do return json
["event"].as(String)
1460 fun event
=(event
: String) do json
["event"] = event
1462 # Commit linked to this event (if any).
1463 fun commit_id
: nullable String do
1464 var res
= json
.get_or_null
("commit_id")
1465 if res
isa String then return res
1470 fun commit_id
=(commit_id
: nullable String) do json
["commit_id"] = commit_id
1472 # Label linked to this event (if any).
1473 fun labl
: nullable Label do
1474 var res
= json
.get_or_null
("label")
1475 if res
isa JsonObject then return new Label.from_json
(api
, repo
, res
)
1480 fun labl
=(labl
: nullable Label) do
1481 if labl
== null then
1484 json
["labl"] = labl
.json
1488 # User linked to this event (if any).
1489 fun assignee
: nullable User do
1490 var res
= json
.get_or_null
("assignee")
1491 if res
isa JsonObject then return new User.from_json
(api
, res
)
1496 fun assignee
=(assignee
: nullable User) do
1497 if assignee
== null then
1498 json
["assignee"] = null
1500 json
["assignee"] = assignee
.json
1504 # Milestone linked to this event (if any).
1505 fun milestone
: nullable Milestone do
1506 var res
= json
.get_or_null
("milestone")
1507 if res
isa JsonObject then return new Milestone.from_json
(api
, repo
, res
)
1512 fun milestone
=(milestone
: nullable User) do
1513 if milestone
== null then
1514 json
["milestone"] = null
1516 json
["milestone"] = milestone
.json
1520 # Rename linked to this event (if any).
1521 fun rename
: nullable RenameAction do
1522 var res
= json
.get_or_null
("rename")
1523 if res
isa JsonObject then return new RenameAction(res
)
1528 fun rename
=(rename
: nullable User) do
1529 if rename
== null then
1530 json
["rename"] = null
1532 json
["rename"] = rename
.json
1537 # A rename action maintains the name before and after a renaming action.
1541 var json
: JsonObject
1543 # Name before renaming.
1544 fun from
: String do return json
["from"].as(String)
1547 fun from
=(from
: String) do json
["from"] = from
1549 # Name after renaming.
1550 fun to
: String do return json
["to"].as(String)
1553 fun to
=(to
: String) do json
["to"] = to
1556 # Contributors list with additions, deletions, and commit counts.
1558 # Should be accessed from `Repo::contrib_stats`.
1560 # See <https://developer.github.com/v3/repos/statistics/>.
1561 class ContributorStats
1564 redef type OTHER: ContributorStats
1566 # Github API client.
1570 var json
: JsonObject
1572 # Init `self` from a `json` object.
1573 init from_json
(api
: GithubAPI, json
: JsonObject) do
1577 # User these statistics are about.
1579 return new User.from_json
(api
, json
["author"].as(JsonObject))
1583 fun author
=(author
: User) do json
["author"] = author
.json
1585 # Total number of commit.
1586 fun total
: Int do return json
["total"].as(Int)
1589 fun total
=(total
: Int) do json
["total"] = total
1591 # Are of weeks of activity with detailed statistics.
1592 fun weeks
: JsonArray do return json
["weeks"].as(JsonArray)
1595 fun weeks
=(weeks
: JsonArray) do json
["weeks"] = weeks
1597 # ContributorStats can be compared on the total amount of commits.
1598 redef fun <(o
) do return total
< o
.total
1601 # A Github file representation.
1603 # Mostly a wrapper around a json object.
1607 var json
: JsonObject
1610 fun filename
: String do return json
["filename"].as(String)
1613 fun filename
=(filename
: String) do json
["filename"] = filename