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.
17 # This modules reifies Github API elements as Nit classes.
19 # For most use-cases you need to use the `GithubAPI` client.
24 # Interface to Github REST API.
26 # Used by all `GithubEntity` to perform requests.
31 # # Get Github authentification token.
32 # var token = get_github_oauth
33 # assert not token.is_empty
36 # var api = new GithubAPI(token)
39 # The API client allows to get Github API entities:
42 # var repo = api.load_repo("privat/nit")
44 # assert repo.name == "nit"
46 # var user = api.load_user("Morriar")
48 # assert user.login == "Morriar"
52 # Github API OAuth token.
54 # This token is used to authenticate the application on Github API.
55 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
56 # associated to the key.
59 # Github API base url.
61 # Default is `https://api.github.com` and should not be changed.
62 var api_url
= "https://api.github.com"
64 # User agent used for HTTP requests.
66 # Default is `nit_github_api`.
68 # See <https://developer.github.com/v3/#user-agent-required>
69 var user_agent
= "nit_github_api"
73 # Internal Curl instance used to perform API calls.
74 private var ghcurl
: GithubCurl is noinit
78 # * `0`: only errors (default)
80 var verbose_lvl
= 0 is public
writable
83 ghcurl
= new GithubCurl(auth
, user_agent
)
86 # Execute a GET request on Github API.
88 # This method returns raw json data.
89 # See other `load_*` methods to use more expressive types.
91 # var api = new GithubAPI(get_github_oauth)
92 # var obj = api.get("repos/privat/nit")
93 # assert obj isa JsonObject
94 # assert obj["name"] == "nit"
96 # Returns `null` in case of `error`.
98 # obj = api.get("foo/bar/baz")
100 # assert api.was_error
101 # var err = api.last_error
102 # assert err isa GithubError
103 # assert err.name == "GithubAPIError"
104 # assert err.message == "Not Found"
105 fun get
(path
: String): nullable Jsonable do
106 path
= sanitize_uri
(path
)
107 var res
= ghcurl
.get_and_parse
("{api_url}/{path}")
108 if res
isa Error then
117 # Display a message depending on `verbose_lvl`.
118 fun message
(lvl
: Int, message
: String) do
119 if lvl
<= verbose_lvl
then print message
122 # Escape `uri` in an acceptable format for Github.
123 private fun sanitize_uri
(uri
: String): String do
124 # TODO better URI escape.
125 return uri
.replace
(" ", "%20")
128 # Last error occured during Github API communications.
129 var last_error
: nullable Error = null is public
writable
131 # Does the last request provoqued an error?
132 var was_error
= false is protected writable
134 # Load the json object from Github.
135 # See `GithubEntity::load_from_github`.
136 private fun load_from_github
(key
: String): JsonObject do
137 message
(1, "Get {key} (github)")
139 if was_error
then return new JsonObject
140 return res
.as(JsonObject)
143 # Get the Github user with `login`.
145 # Returns `null` if the user cannot be found.
147 # var api = new GithubAPI(get_github_oauth)
148 # var user = api.load_user("Morriar")
149 # assert user.login == "Morriar"
150 fun load_user
(login
: String): nullable User do
151 var user
= new User(self, login
)
152 return user
.load_from_github
155 # Get the Github repo with `full_name`.
157 # Returns `null` if the repo cannot be found.
159 # var api = new GithubAPI(get_github_oauth)
160 # var repo = api.load_repo("privat/nit")
161 # assert repo.name == "nit"
162 # assert repo.owner.login == "privat"
163 # assert repo.default_branch.name == "master"
164 fun load_repo
(full_name
: String): nullable Repo do
165 var repo
= new Repo(self, full_name
)
166 return repo
.load_from_github
169 # Get the Github branch with `name`.
171 # Returns `null` if the branch cannot be found.
173 # var api = new GithubAPI(get_github_oauth)
174 # var repo = api.load_repo("privat/nit")
175 # assert repo != null
176 # var branch = api.load_branch(repo, "master")
177 # assert branch.name == "master"
178 # assert branch.commit isa Commit
179 fun load_branch
(repo
: Repo, name
: String): nullable Branch do
180 var branch
= new Branch(self, repo
, name
)
181 return branch
.load_from_github
184 # Get the Github commit with `sha`.
186 # Returns `null` if the commit cannot be found.
188 # var api = new GithubAPI(get_github_oauth)
189 # var repo = api.load_repo("privat/nit")
190 # assert repo != null
191 # var commit = api.load_commit(repo, "64ce1f")
192 # assert commit isa Commit
193 fun load_commit
(repo
: Repo, sha
: String): nullable Commit do
194 var commit
= new Commit(self, repo
, sha
)
195 return commit
.load_from_github
198 # Get the Github issue #`number`.
200 # Returns `null` if the issue cannot be found.
202 # var api = new GithubAPI(get_github_oauth)
203 # var repo = api.load_repo("privat/nit")
204 # assert repo != null
205 # var issue = api.load_issue(repo, 1)
206 # assert issue.title == "Doc"
207 fun load_issue
(repo
: Repo, number
: Int): nullable Issue do
208 var issue
= new Issue(self, repo
, number
)
209 return issue
.load_from_github
212 # Get the Github pull request #`number`.
214 # Returns `null` if the pull request cannot be found.
216 # var api = new GithubAPI(get_github_oauth)
217 # var repo = api.load_repo("privat/nit")
218 # assert repo != null
219 # var pull = api.load_pull(repo, 1)
220 # assert pull.title == "Doc"
221 # assert pull.user.login == "Morriar"
222 fun load_pull
(repo
: Repo, number
: Int): nullable PullRequest do
223 var pull
= new PullRequest(self, repo
, number
)
224 return pull
.load_from_github
227 # Get the Github label with `name`.
229 # Returns `null` if the label cannot be found.
231 # var api = new GithubAPI(get_github_oauth)
232 # var repo = api.load_repo("privat/nit")
233 # assert repo != null
234 # var labl = api.load_label(repo, "ok_will_merge")
235 # assert labl != null
236 fun load_label
(repo
: Repo, name
: String): nullable Label do
237 var labl
= new Label(self, repo
, name
)
238 return labl
.load_from_github
241 # Get the Github milestone with `id`.
243 # Returns `null` if the milestone cannot be found.
245 # var api = new GithubAPI(get_github_oauth)
246 # var repo = api.load_repo("privat/nit")
247 # assert repo != null
248 # var stone = api.load_milestone(repo, 4)
249 # assert stone.title == "v1.0prealpha"
250 fun load_milestone
(repo
: Repo, id
: Int): nullable Milestone do
251 var milestone
= new Milestone(self, repo
, id
)
252 return milestone
.load_from_github
255 # Get the Github issue event with `id`.
257 # Returns `null` if the event cannot be found.
259 # var api = new GithubAPI(get_github_oauth)
260 # var repo = api.load_repo("privat/nit")
261 # assert repo isa Repo
262 # var event = api.load_issue_event(repo, 199674194)
263 # assert event.actor.login == "privat"
264 # assert event.event == "labeled"
265 # assert event.labl.name == "need_review"
266 # assert event.issue.number == 945
267 fun load_issue_event
(repo
: Repo, id
: Int): nullable IssueEvent do
268 var event
= new IssueEvent(self, repo
, id
)
269 event
.load_from_github
270 if was_error
then return null
274 # Get the Github commit comment with `id`.
276 # Returns `null` if the comment cannot be found.
278 # var api = new GithubAPI(get_github_oauth)
279 # var repo = api.load_repo("privat/nit")
280 # assert repo != null
281 # var comment = api.load_commit_comment(repo, 8982707)
282 # assert comment.user.login == "Morriar"
283 # assert comment.body == "For testing purposes..."
284 # assert comment.commit.sha == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
285 fun load_commit_comment
(repo
: Repo, id
: Int): nullable CommitComment do
286 var comment
= new CommitComment(self, repo
, id
)
287 return comment
.load_from_github
290 # Get the Github issue comment with `id`.
292 # Returns `null` if the comment cannot be found.
294 # var api = new GithubAPI(get_github_oauth)
295 # var repo = api.load_repo("privat/nit")
296 # assert repo != null
297 # var comment = api.load_issue_comment(repo, 6020149)
298 # assert comment.user.login == "privat"
299 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
300 # assert comment.issue.number == 10
301 fun load_issue_comment
(repo
: Repo, id
: Int): nullable IssueComment do
302 var comment
= new IssueComment(self, repo
, id
)
303 return comment
.load_from_github
306 # Get the Github diff comment with `id`.
308 # Returns `null` if the comment cannot be found.
310 # var api = new GithubAPI(get_github_oauth)
311 # var repo = api.load_repo("privat/nit")
312 # assert repo != null
313 # var comment = api.load_review_comment(repo, 21010363)
314 # assert comment.path == "src/modelize/modelize_property.nit"
315 # assert comment.original_position == 26
316 # assert comment.pull.number == 945
317 fun load_review_comment
(repo
: Repo, id
: Int): nullable ReviewComment do
318 var comment
= new ReviewComment(self, repo
, id
)
319 return comment
.load_from_github
323 # Something returned by the Github API.
325 # Mainly a Nit wrapper around a JSON objet.
326 abstract class GithubEntity
328 # Github API instance.
331 # FIXME constructor should be private
333 # Key used to access this entity from Github api base.
334 fun key
: String is abstract
336 # JSON representation of `self`.
338 # This is the same json structure than used by Github API.
339 var json
: JsonObject is noinit
, protected writable
341 # Load `json` from Github API.
342 private fun load_from_github
: nullable SELF do
343 json
= api
.load_from_github
(key
)
344 if api
.was_error
then return null
348 redef fun to_s
do return json
.to_json
353 # Should be accessed from `GithubAPI::load_user`.
355 # See <https://developer.github.com/v3/users/>.
359 redef var key
is lazy
do return "users/{login}"
364 # Init `self` from a `json` object.
365 init from_json
(api
: GithubAPI, json
: JsonObject) do
366 init(api
, json
["login"].to_s
)
370 # Github User page url.
371 fun html_url
: String do return json
["html_url"].to_s
373 # Avatar image url for this user.
374 fun avatar_url
: String do return json
["avatar_url"].to_s
377 # A Github repository.
379 # Should be accessed from `GithubAPI::load_repo`.
381 # See <https://developer.github.com/v3/repos/>.
385 redef var key
is lazy
do return "repos/{full_name}"
387 # Repo full name on Github.
388 var full_name
: String
390 # Init `self` from a `json` object.
391 init from_json
(api
: GithubAPI, json
: JsonObject) do
392 init(api
, json
["full_name"].to_s
)
396 # Repo short name on Github.
397 fun name
: String do return json
["name"].to_s
399 # Github User page url.
400 fun html_url
: String do return json
["html_url"].to_s
402 # Get the repo owner.
404 return new User.from_json
(api
, json
["owner"].as(JsonObject))
407 # List of branches associated with their names.
408 fun branches
: Map[String, Branch] do
409 api
.message
(1, "Get branches for {full_name}")
410 var array
= api
.get
("repos/{full_name}/branches")
411 var res
= new HashMap[String, Branch]
412 if not array
isa JsonArray then return res
414 if not obj
isa JsonObject then continue
415 var name
= obj
["name"].to_s
416 res
[name
] = new Branch.from_json
(api
, self, obj
)
421 # List of issues associated with their ids.
422 fun issues
: Map[Int, Issue] do
423 api
.message
(1, "Get issues for {full_name}")
424 var res
= new HashMap[Int, Issue]
425 var issue
= last_issue
426 if issue
== null then return res
427 res
[issue
.number
] = issue
428 while issue
.number
> 1 do
429 issue
= api
.load_issue
(self, issue
.number
- 1)
430 assert issue
isa Issue
431 res
[issue
.number
] = issue
436 # Get the last published issue.
437 fun last_issue
: nullable Issue do
438 var array
= api
.get
("repos/{full_name}/issues")
439 if not array
isa JsonArray then return null
440 if array
.is_empty
then return null
441 var obj
= array
.first
442 if not obj
isa JsonObject then return null
443 return new Issue.from_json
(api
, self, obj
)
446 # List of labels associated with their names.
447 fun labels
: Map[String, Label] do
448 api
.message
(1, "Get labels for {full_name}")
449 var array
= api
.get
("repos/{full_name}/labels")
450 var res
= new HashMap[String, Label]
451 if not array
isa JsonArray then return res
453 if not obj
isa JsonObject then continue
454 var name
= obj
["name"].to_s
455 res
[name
] = new Label.from_json
(api
, self, obj
)
460 # List of milestones associated with their ids.
461 fun milestones
: Map[Int, Milestone] do
462 api
.message
(1, "Get milestones for {full_name}")
463 var array
= api
.get
("repos/{full_name}/milestones")
464 var res
= new HashMap[Int, Milestone]
465 if array
isa JsonArray then
467 if not obj
isa JsonObject then continue
468 var number
= obj
["number"].as(Int)
469 res
[number
] = new Milestone.from_json
(api
, self, obj
)
475 # List of pull-requests associated with their ids.
477 # Implementation notes: because PR numbers are not consecutive,
478 # PR are loaded from pages.
479 # See: https://developer.github.com/v3/pulls/#list-pull-requests
480 fun pulls
: Map[Int, PullRequest] do
481 api
.message
(1, "Get pulls for {full_name}")
482 var res
= new HashMap[Int, PullRequest]
484 var array
= api
.get
("{key}/pulls?page={page}").as(JsonArray)
485 while not array
.is_empty
do
487 if not obj
isa JsonObject then continue
488 var number
= obj
["number"].as(Int)
489 res
[number
] = new PullRequest.from_json
(api
, self, obj
)
492 array
= api
.get
("{key}/pulls?page={page}").as(JsonArray)
497 # Repo default branch.
498 fun default_branch
: Branch do
499 var name
= json
["default_branch"].to_s
500 var branch
= api
.load_branch
(self, name
)
501 assert branch
isa Branch
506 # A `RepoEntity` is something contained in a `Repo`.
507 abstract class RepoEntity
510 # Repo that contains `self`.
513 # Init `self` from a `json` object.
514 init from_json
(api
: GithubAPI, repo
: Repo, json
: JsonObject) do
523 # Should be accessed from `GithubAPI::load_branch`.
525 # See <https://developer.github.com/v3/repos/#list-branches>.
529 redef var key
is lazy
do return "{repo.key}/branches/{name}"
534 redef init from_json
(api
, repo
, json
) do
535 self.name
= json
["name"].to_s
539 # Get the last commit of `self`.
540 fun commit
: Commit do
541 return new Commit.from_json
(api
, repo
, json
["commit"].as(JsonObject))
544 # List all commits in `self`.
546 # This can be long depending on the branch size.
547 # Commit are returned in an unspecified order.
548 fun commits
: Array[Commit] do
549 var res
= new Array[Commit]
550 var done
= new HashSet[String]
551 var todos
= new Array[Commit]
553 while not todos
.is_empty
do
554 var commit
= todos
.pop
555 if done
.has
(commit
.sha
) then continue
558 for parent
in commit
.parents
do
568 # Should be accessed from `GithubAPI::load_commit`.
570 # See <https://developer.github.com/v3/commits/>.
574 redef var key
is lazy
do return "{repo.key}/commits/{sha}"
579 redef init from_json
(api
, repo
, json
) do
580 self.sha
= json
["sha"].to_s
584 # Parent commits of `self`.
585 fun parents
: Array[Commit] do
586 var res
= new Array[Commit]
587 var parents
= json
["parents"]
588 if not parents
isa JsonArray then return res
589 for obj
in parents
do
590 if not obj
isa JsonObject then continue
591 res
.add
(api
.load_commit
(repo
, obj
["sha"].to_s
).as(not null))
596 # Author of the commit.
597 fun author
: nullable User do
598 if not json
.has_key
("author") then return null
599 var user
= json
["author"]
600 if not user
isa JsonObject then return null
601 return new User.from_json
(api
, user
)
604 # Committer of the commit.
605 fun committer
: nullable User do
606 if not json
.has_key
("committer") then return null
607 var user
= json
["author"]
608 if not user
isa JsonObject then return null
609 return new User.from_json
(api
, user
)
612 # Authoring date as ISODate.
613 fun author_date
: ISODate do
614 var commit
= json
["commit"].as(JsonObject)
615 var author
= commit
["author"].as(JsonObject)
616 return new ISODate.from_string
(author
["date"].to_s
)
619 # Commit date as ISODate.
620 fun commit_date
: ISODate do
621 var commit
= json
["commit"].as(JsonObject)
622 var author
= commit
["committer"].as(JsonObject)
623 return new ISODate.from_string
(author
["date"].to_s
)
627 fun message
: String do return json
["commit"].as(JsonObject)["message"].to_s
632 # Should be accessed from `GithubAPI::load_issue`.
634 # See <https://developer.github.com/v3/issues/>.
638 redef var key
is lazy
do return "{repo.key}/issues/{number}"
643 redef init from_json
(api
, repo
, json
) do
644 self.number
= json
["number"].as(Int)
649 fun title
: String do return json
["title"].to_s
651 # User that created this issue.
653 return new User.from_json
(api
, json
["user"].as(JsonObject))
656 # List of labels on this issue associated to their names.
657 fun labels
: Map[String, Label] do
658 var res
= new HashMap[String, Label]
659 for obj
in json
["labels"].as(JsonArray) do
660 if not obj
isa JsonObject then continue
661 var name
= obj
["name"].to_s
662 res
[name
] = new Label.from_json
(api
, repo
, obj
)
667 # State of the issue on Github.
668 fun state
: String do return json
["state"].to_s
670 # Is the issue locked?
671 fun locked
: Bool do return json
["locked"].as(Bool)
673 # Assigned `User` (if any).
674 fun assignee
: nullable User do
675 var assignee
= json
["assignee"]
676 if not assignee
isa JsonObject then return null
677 return new User.from_json
(api
, assignee
)
680 # `Milestone` (if any).
681 fun milestone
: nullable Milestone do
682 var milestone
= json
["milestone"]
683 if not milestone
isa JsonObject then return null
684 return new Milestone.from_json
(api
, repo
, milestone
)
687 # List of comments made on this issue.
688 fun comments
: Array[IssueComment] do
689 var res
= new Array[IssueComment]
690 var count
= comments_count
692 var array
= api
.get
("{key}/comments?page={page}")
693 if not array
isa JsonArray then
696 while not array
.is_empty
and res
.length
< count
do
698 if not obj
isa JsonObject then continue
699 var id
= obj
["id"].as(Int)
700 res
.add
(api
.load_issue_comment
(repo
, id
).as(not null))
703 array
= api
.get
("{key}/comments?page={page}").as(JsonArray)
708 # Number of comments on this issue.
709 fun comments_count
: Int do return json
["comments"].to_s
.to_i
711 # Creation time in ISODate format.
712 fun created_at
: ISODate do
713 return new ISODate.from_string
(json
["created_at"].to_s
)
716 # Last update time in ISODate format (if any).
717 fun updated_at
: nullable ISODate do
718 var res
= json
["updated_at"]
719 if res
== null then return null
720 return new ISODate.from_string
(res
.to_s
)
723 # Close time in ISODate format (if any).
724 fun closed_at
: nullable ISODate do
725 var res
= json
["closed_at"]
726 if res
== null then return null
727 return new ISODate.from_string
(res
.to_s
)
730 # TODO link to pull request
732 # Full description of the issue.
733 fun body
: String do return json
["body"].to_s
735 # List of events on this issue.
736 fun events
: Array[IssueEvent] do
737 var res
= new Array[IssueEvent]
739 var array
= api
.get
("{key}/events?page={page}").as(JsonArray)
740 while not array
.is_empty
do
742 if not obj
isa JsonObject then continue
743 res
.add
new IssueEvent.from_json
(api
, repo
, obj
)
746 array
= api
.get
("{key}/events?page={page}").as(JsonArray)
751 # User that closed this issue (if any).
752 fun closed_by
: nullable User do
753 var closer
= json
["closed_by"]
754 if not closer
isa JsonObject then return null
755 return new User.from_json
(api
, closer
)
759 # A Github pull request.
761 # Should be accessed from `GithubAPI::load_pull`.
763 # PullRequest are basically Issues with more data.
764 # See <https://developer.github.com/v3/pulls/>.
768 redef var key
is lazy
do return "{repo.key}/pulls/{number}"
770 # Merge time in ISODate format (if any).
771 fun merged_at
: nullable ISODate do
772 var res
= json
["merged_at"]
773 if res
== null then return null
774 return new ISODate.from_string
(res
.to_s
)
778 fun merge_commit_sha
: String do return json
["merge_commit_sha"].to_s
780 # Count of comments made on the pull request diff.
781 fun review_comments
: Int do return json
["review_comments"].to_s
.to_i
783 # Pull request head (can be a commit SHA or a branch name).
785 var json
= json
["head"].as(JsonObject)
786 return new PullRef(api
, json
)
789 # Pull request base (can be a commit SHA or a branch name).
791 var json
= json
["base"].as(JsonObject)
792 return new PullRef(api
, json
)
795 # Is this pull request merged?
796 fun merged
: Bool do return json
["merged"].as(Bool)
798 # Is this pull request mergeable?
799 fun mergeable
: Bool do return json
["mergeable"].as(Bool)
801 # Mergeable state of this pull request.
803 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
804 fun mergeable_state
: Int do return json
["mergeable_state"].to_s
.to_i
806 # User that merged this pull request (if any).
807 fun merged_by
: nullable User do
808 var merger
= json
["merged_by"]
809 if not merger
isa JsonObject then return null
810 return new User.from_json
(api
, merger
)
813 # Count of commits in this pull request.
814 fun commits
: Int do return json
["commits"].to_s
.to_i
817 fun additions
: Int do return json
["additions"].to_s
.to_i
819 # Deleted line count.
820 fun deletions
: Int do return json
["deletions"].to_s
.to_i
822 # Changed files count.
823 fun changed_files
: Int do return json
["changed_files"].to_s
.to_i
826 # A pull request reference (used for head and base).
829 # Api instance that maintains self.
832 # JSON representation.
835 # Label pointed by `self`.
836 fun labl
: String do return json
["label"].to_s
838 # Reference pointed by `self`.
839 fun ref
: String do return json
["ref"].to_s
841 # Commit SHA pointed by `self`.
842 fun sha
: String do return json
["sha"].to_s
844 # User pointed by `self`.
846 return new User.from_json
(api
, json
["user"].as(JsonObject))
849 # Repo pointed by `self`.
851 return new Repo.from_json
(api
, json
["repo"].as(JsonObject))
857 # Should be accessed from `GithubAPI::load_label`.
859 # See <https://developer.github.com/v3/issues/labels/>.
863 redef var key
is lazy
do return "{repo.key}/labels/{name}"
868 redef init from_json
(api
, repo
, json
) do
869 self.name
= json
["name"].to_s
874 fun color
: String do return json
["color"].to_s
877 # A Github milestone.
879 # Should be accessed from `GithubAPI::load_milestone`.
881 # See <https://developer.github.com/v3/issues/milestones/>.
885 redef var key
is lazy
do return "{repo.key}/milestones/{number}"
887 # The milestone id on Github.
890 redef init from_json
(api
, repo
, json
) do
892 self.number
= json
["number"].as(Int)
896 fun title
: String do return json
["title"].to_s
898 # Milestone long description.
899 fun description
: String do return json
["description"].to_s
901 # Count of opened issues linked to this milestone.
902 fun open_issues
: Int do return json
["open_issues"].to_s
.to_i
904 # Count of closed issues linked to this milestone.
905 fun closed_issues
: Int do return json
["closed_issues"].to_s
.to_i
908 fun state
: String do return json
["state"].to_s
910 # Creation time in ISODate format.
911 fun created_at
: ISODate do
912 return new ISODate.from_string
(json
["created_at"].to_s
)
915 # User that created this milestone.
917 return new User.from_json
(api
, json
["creator"].as(JsonObject))
920 # Due time in ISODate format (if any).
921 fun due_on
: nullable ISODate do
922 var res
= json
["updated_at"]
923 if res
== null then return null
924 return new ISODate.from_string
(res
.to_s
)
927 # Update time in ISODate format (if any).
928 fun updated_at
: nullable ISODate do
929 var res
= json
["updated_at"]
930 if res
== null then return null
931 return new ISODate.from_string
(res
.to_s
)
934 # Close time in ISODate format (if any).
935 fun closed_at
: nullable ISODate do
936 var res
= json
["closed_at"]
937 if res
== null then return null
938 return new ISODate.from_string
(res
.to_s
)
944 # There is two kinds of comments:
946 # * `CommitComment` are made on a commit page.
947 # * `IssueComment` are made on an issue or pull request page.
948 # * `ReviewComment` are made on the diff associated to a pull request.
949 abstract class Comment
952 # Identifier of this comment.
955 redef init from_json
(api
, repo
, json
) do
956 self.id
= json
["id"].as(Int)
960 # User that made this comment.
962 return new User.from_json
(api
, json
["user"].as(JsonObject))
965 # Creation time in ISODate format.
966 fun created_at
: ISODate do
967 return new ISODate.from_string
(json
["created_at"].to_s
)
970 # Last update time in ISODate format (if any).
971 fun updated_at
: nullable ISODate do
972 if not json
.has_key
("updated_at") then return null
973 return new ISODate.from_string
(json
["updated_at"].to_s
)
977 fun body
: String do return json
["body"].to_s
980 # A comment made on a commit.
984 redef var key
is lazy
do return "{repo.key}/comments/{id}"
987 fun commit
: Commit do
988 return api
.load_commit
(repo
, json
["commit_id"].to_s
).as(not null)
991 # Position of the comment on the line.
992 fun position
: nullable String do
993 if not json
.has_key
("position") then return null
994 var res
= json
["position"]
995 if res
== null then return null
999 # Line of the comment.
1000 fun line
: nullable String do
1001 if not json
.has_key
("line") then return null
1002 var res
= json
["line"]
1003 if res
== null then return null
1007 # Path of the commented file.
1008 fun path
: String do return json
["path"].to_s
1011 # Comments made on Github issue and pull request pages.
1013 # Should be accessed from `GithubAPI::load_issue_comment`.
1015 # See <https://developer.github.com/v3/issues/comments/>.
1019 redef var key
is lazy
do return "{repo.key}/issues/comments/{id}"
1021 # Issue that contains `self`.
1023 var number
= issue_url
.split
("/").last
.to_i
1024 return api
.load_issue
(repo
, number
).as(not null)
1027 # Link to the issue document on API.
1028 fun issue_url
: String do return json
["issue_url"].to_s
1031 # Comments made on Github pull request diffs.
1033 # Should be accessed from `GithubAPI::load_diff_comment`.
1035 # See <https://developer.github.com/v3/pulls/comments/>.
1039 redef var key
is lazy
do return "{repo.key}/pulls/comments/{id}"
1041 # Pull request that contains `self`.
1042 fun pull
: PullRequest do
1043 var number
= pull_request_url
.split
("/").last
.to_i
1044 return api
.load_pull
(repo
, number
).as(not null)
1047 # Link to the pull request on API.
1048 fun pull_request_url
: String do return json
["pull_request_url"].to_s
1051 fun diff_hunk
: String do return json
["diff_hunk"].to_s
1053 # Path of commented file.
1054 fun path
: String do return json
["path"].to_s
1056 # Position of the comment on the file.
1057 fun position
: Int do return json
["position"].to_s
.to_i
1059 # Original position in the diff.
1060 fun original_position
: Int do return json
["original_position"].to_s
.to_i
1062 # Commit referenced by this comment.
1063 fun commit_id
: String do return json
["commit_id"].to_s
1065 # Original commit id.
1066 fun original_commit_id
: String do return json
["original_commit_id"].to_s
1069 # An event that occurs on a Github `Issue`.
1071 # Should be accessed from `GithubAPI::load_issue_event`.
1073 # See <https://developer.github.com/v3/issues/events/>.
1077 redef var key
is lazy
do return "{repo.key}/issues/events/{id}"
1079 # Event id on Github.
1082 redef init from_json
(api
, repo
, json
) do
1083 self.id
= json
["id"].as(Int)
1087 # Issue that contains `self`.
1089 return new Issue.from_json
(api
, repo
, json
["issue"].as(JsonObject))
1092 # User that initiated the event.
1094 return new User.from_json
(api
, json
["actor"].as(JsonObject))
1097 # Creation time in ISODate format.
1098 fun created_at
: ISODate do
1099 return new ISODate.from_string
(json
["created_at"].to_s
)
1103 fun event
: String do return json
["event"].to_s
1105 # Commit linked to this event (if any).
1106 fun commit_id
: nullable String do
1107 var res
= json
["commit_id"]
1108 if res
== null then return null
1112 # Label linked to this event (if any).
1113 fun labl
: nullable Label do
1114 var res
= json
["label"]
1115 if not res
isa JsonObject then return null
1116 return new Label.from_json
(api
, repo
, res
)
1119 # User linked to this event (if any).
1120 fun assignee
: nullable User do
1121 var res
= json
["assignee"]
1122 if not res
isa JsonObject then return null
1123 return new User.from_json
(api
, res
)
1126 # Milestone linked to this event (if any).
1127 fun milestone
: nullable Milestone do
1128 var res
= json
["milestone"]
1129 if not res
isa JsonObject then return null
1130 return new Milestone.from_json
(api
, repo
, res
)
1133 # Rename linked to this event (if any).
1134 fun rename
: nullable RenameAction do
1135 var res
= json
["rename"]
1136 if res
== null then return null
1137 return new RenameAction(res
.as(JsonObject))
1141 # A rename action maintains the name before and after a renaming action.
1145 var json
: JsonObject
1147 # Name before renaming.
1148 fun from
: String do return json
["from"].to_s
1150 # Name after renaming.
1151 fun to
: String do return json
["to"].to_s