tests: add some runtime error in nitin.input
[nit.git] / lib / github / api.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Nit object oriented interface to [Github api](https://developer.github.com/v3/).
16 #
17 # This modules reifies Github API elements as Nit classes.
18 #
19 # For most use-cases you need to use the `GithubAPI` client.
20 module api
21
22 import github_curl
23 intrude import json::serialization_read
24
25 # Client to Github API
26 #
27 # To access the API you need an instance of a `GithubAPI` client.
28 #
29 # ~~~
30 # # Get Github authentification token.
31 # var token = get_github_oauth
32 # assert not token.is_empty
33 #
34 # # Init the client.
35 # var api = new GithubAPI(token)
36 # ~~~
37 #
38 # The API client allows you to get Github API entities.
39 #
40 # ~~~
41 # var repo = api.load_repo("nitlang/nit")
42 # assert repo != null
43 # assert repo.name == "nit"
44 #
45 # var user = api.load_user("Morriar")
46 # assert user != null
47 # assert user.login == "Morriar"
48 # ~~~
49 class GithubAPI
50
51 # Github API OAuth token
52 #
53 # To access your private ressources, you must
54 # [authenticate](https://developer.github.com/guides/basics-of-authentication/).
55 #
56 # For client applications, Github recommands to use the
57 # [OAuth tokens](https://developer.github.com/v3/oauth/) authentification method.
58 #
59 #
60 #
61 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
62 # associated to the key.
63 var auth: String
64
65 # Github API base url.
66 #
67 # Default is `https://api.github.com` and should not be changed.
68 var api_url = "https://api.github.com"
69
70 # User agent used for HTTP requests.
71 #
72 # Default is `nit_github_api`.
73 #
74 # See <https://developer.github.com/v3/#user-agent-required>
75 var user_agent = "nit_github_api"
76
77 # Curl instance.
78 #
79 # Internal Curl instance used to perform API calls.
80 private var ghcurl: GithubCurl is noinit
81
82 # Verbosity level.
83 #
84 # * `0`: only errors (default)
85 # * `1`: verbose
86 var verbose_lvl = 0 is public writable
87
88 init do
89 ghcurl = new GithubCurl(auth, user_agent)
90 end
91
92 # Deserialize an object
93 fun deserialize(string: String): nullable Object do
94 var deserializer = new GithubDeserializer(string)
95 var res = deserializer.deserialize
96 # print deserializer.errors.join("\n") # DEBUG
97 return res
98 end
99
100 # Execute a GET request on Github API.
101 #
102 # This method returns raw json data.
103 # See other `load_*` methods to use more expressive types.
104 #
105 # var api = new GithubAPI(get_github_oauth)
106 # var obj = api.get("/repos/nitlang/nit")
107 # assert obj isa JsonObject
108 # assert obj["name"] == "nit"
109 #
110 # Returns `null` in case of `error`.
111 #
112 # obj = api.get("/foo/bar/baz")
113 # assert obj == null
114 # assert api.was_error
115 # var err = api.last_error
116 # assert err isa GithubError
117 # assert err.name == "GithubAPIError"
118 # assert err.message == "Not Found"
119 fun get(path: String): nullable Jsonable do
120 path = sanitize_uri(path)
121 var res = ghcurl.get_and_parse("{api_url}{path}")
122 if res isa Error then
123 last_error = res
124 was_error = true
125 return null
126 end
127 was_error = false
128 return res
129 end
130
131 # Display a message depending on `verbose_lvl`.
132 fun message(lvl: Int, message: String) do
133 if lvl <= verbose_lvl then print message
134 end
135
136 # Escape `uri` in an acceptable format for Github.
137 private fun sanitize_uri(uri: String): String do
138 # TODO better URI escape.
139 return uri.replace(" ", "%20")
140 end
141
142 # Last error occured during Github API communications.
143 var last_error: nullable Error = null is public writable
144
145 # Does the last request provoqued an error?
146 var was_error = false is protected writable
147
148 # Load the json object from Github.
149 # See `GithubEntity::load_from_github`.
150 protected fun load_from_github(key: String): nullable GithubEntity do
151 message(1, "Get {key} (github)")
152 var res = get(key)
153 if was_error then return null
154 return deserialize(res.as(JsonObject).to_json).as(nullable GithubEntity)
155 end
156
157 # Get the Github logged user from `auth` token.
158 #
159 # Loads the `User` from the API or returns `null` if the user cannot be found.
160 #
161 # ~~~nitish
162 # var api = new GithubAPI(get_github_oauth)
163 # var user = api.load_auth_user
164 # assert user.login == "Morriar"
165 # ~~~
166 fun load_auth_user: nullable User do
167 var user = load_from_github("/user")
168 if was_error then return null
169 return user.as(nullable User)
170 end
171
172 # Get the Github user with `login`
173 #
174 # Loads the `User` from the API or returns `null` if the user cannot be found.
175 #
176 # var api = new GithubAPI(get_github_oauth)
177 # var user = api.load_user("Morriar")
178 # print user or else "null"
179 # assert user.login == "Morriar"
180 fun load_user(login: String): nullable User do
181 return load_from_github("/users/{login}").as(nullable User)
182 end
183
184 # Get the Github repo with `full_name`.
185 #
186 # Loads the `Repo` from the API or returns `null` if the repo cannot be found.
187 #
188 # var api = new GithubAPI(get_github_oauth)
189 # var repo = api.load_repo("nitlang/nit")
190 # assert repo.name == "nit"
191 # assert repo.owner.login == "nitlang"
192 # assert repo.default_branch == "master"
193 fun load_repo(full_name: String): nullable Repo do
194 return load_from_github("/repos/{full_name}").as(nullable Repo)
195 end
196
197 # List of branches associated with their names.
198 fun load_repo_branches(repo: Repo): Array[Branch] do
199 message(1, "Get branches for {repo.full_name}")
200 var array = get("/repos/{repo.full_name}/branches")
201 var res = new Array[Branch]
202 if not array isa JsonArray then return res
203 var deser = deserialize(array.to_json)
204 if deser isa Array[Object] then return res # empty array
205 return deser.as(Array[Branch])
206 end
207
208 # List of issues associated with their ids.
209 fun load_repo_issues(repo: Repo): Array[Issue] do
210 message(1, "Get issues for {repo.full_name}")
211 var res = new Array[Issue]
212 var issue = load_repo_last_issue(repo)
213 if issue == null then return res
214 res.add issue
215 while issue != null and issue.number > 1 do
216 issue = load_issue(repo, issue.number - 1)
217 if issue == null then continue
218 res.add issue
219 end
220 return res
221 end
222
223 # Search issues in this repo form an advanced query.
224 #
225 # Example:
226 #
227 # ~~~nitish
228 # var issues = repo.search_issues("is:open label:need_review")
229 # ~~~
230 #
231 # See <https://developer.github.com/v3/search/#search-issues>.
232 fun search_repo_issues(repo: Repo, query: String): Array[Issue] do
233 query = "/search/issues?q={query} repo:{repo.full_name}"
234 var res = new Array[Issue]
235 var response = get(query)
236 if was_error then return res
237 var arr = response.as(JsonObject)["items"].as(JsonArray)
238 return deserialize(arr.to_json).as(Array[Issue])
239 end
240
241 # Get the last published issue.
242 fun load_repo_last_issue(repo: Repo): nullable Issue do
243 var array = get("/repos/{repo.full_name}/issues")
244 if not array isa JsonArray then return null
245 if array.is_empty then return null
246 var obj = array.first
247 if not obj isa JsonObject then return null
248 return deserialize(obj.to_json).as(nullable Issue)
249 end
250
251 # List of labels associated with their names.
252 fun load_repo_labels(repo: Repo): Array[Label] do
253 message(1, "Get labels for {repo.full_name}")
254 var array = get("repos/{repo.full_name}/labels")
255 if not array isa JsonArray then return new Array[Label]
256 return deserialize(array.to_json).as(Array[Label])
257 end
258
259 # List of milestones associated with their ids.
260 fun load_repo_milestones(repo: Repo): Array[Milestone] do
261 message(1, "Get milestones for {repo.full_name}")
262 var array = get("/repos/{repo.full_name}/milestones")
263 if not array isa JsonArray then return new Array[Milestone]
264 return deserialize(array.to_json).as(Array[Milestone])
265 end
266
267 # List of pull-requests associated with their ids.
268 #
269 # Implementation notes: because PR numbers are not consecutive,
270 # PR are loaded from pages.
271 # See: https://developer.github.com/v3/pulls/#list-pull-requests
272 fun load_repo_pulls(repo: Repo): Array[PullRequest] do
273 message(1, "Get pulls for {repo.full_name}")
274 var key = "/repos/{repo.full_name}"
275 var res = new Array[PullRequest]
276 var page = 1
277 loop
278 var array = get("{key}/pulls?page={page}").as(JsonArray)
279 if array.is_empty then break
280 for obj in array do
281 if not obj isa JsonObject then continue
282 var pr = deserialize(array.to_json).as(nullable PullRequest)
283 if pr == null then continue
284 res.add pr
285 end
286 page += 1
287 end
288 return res
289 end
290
291 # List of contributor related statistics.
292 fun load_repo_contrib_stats(repo: Repo): Array[ContributorStats] do
293 message(1, "Get contributor stats for {repo.full_name}")
294 var res = new Array[ContributorStats]
295 var array = get("/repos/{repo.full_name}/stats/contributors")
296 if not array isa JsonArray then return res
297 return deserialize(array.to_json).as(Array[ContributorStats])
298 end
299
300 # Get the Github branch with `name`.
301 #
302 # Returns `null` if the branch cannot be found.
303 #
304 # var api = new GithubAPI(get_github_oauth)
305 # var repo = api.load_repo("nitlang/nit")
306 # assert repo != null
307 # var branch = api.load_branch(repo, "master")
308 # assert branch.name == "master"
309 # assert branch.commit isa Commit
310 fun load_branch(repo: Repo, name: String): nullable Branch do
311 return load_from_github("/repos/{repo.full_name}/branches/{name}").as(nullable Branch)
312 end
313
314 # List all commits in `self`.
315 #
316 # This can be long depending on the branch size.
317 # Commit are returned in an unspecified order.
318 fun load_branch_commits(branch: Branch): Array[Commit] do
319 var res = new Array[Commit]
320 var done = new HashSet[String]
321 var todos = new Array[Commit]
322 todos.add branch.commit
323 loop
324 if todos.is_empty then break
325 var commit = todos.pop
326 if done.has(commit.sha) then continue
327 done.add commit.sha
328 res.add commit
329 var parents = commit.parents
330 if parents == null then continue
331 for parent in parents do
332 todos.add parent
333 end
334 end
335 return res
336 end
337
338 # Get the Github commit with `sha`.
339 #
340 # Returns `null` if the commit cannot be found.
341 #
342 # var api = new GithubAPI(get_github_oauth)
343 # var repo = api.load_repo("nitlang/nit")
344 # assert repo != null
345 # var commit = api.load_commit(repo, "64ce1f")
346 # assert commit isa Commit
347 fun load_commit(repo: Repo, sha: String): nullable Commit do
348 return load_from_github("/repos/{repo.full_name}/commits/{sha}").as(nullable Commit)
349 end
350
351 # Get the Github issue #`number`.
352 #
353 # Returns `null` if the issue cannot be found.
354 #
355 # var api = new GithubAPI(get_github_oauth)
356 # var repo = api.load_repo("nitlang/nit")
357 # assert repo != null
358 # var issue = api.load_issue(repo, 1)
359 # assert issue.title == "Doc"
360 fun load_issue(repo: Repo, number: Int): nullable Issue do
361 return load_from_github("/repos/{repo.full_name}/issues/{number}").as(nullable Issue)
362 end
363
364 # List of event on this issue.
365 fun load_issue_comments(repo: Repo, issue: Issue): Array[IssueComment] do
366 var res = new Array[IssueComment]
367 var count = issue.comments or else 0
368 var page = 1
369 loop
370 var array = get("/repos/{repo.full_name}/issues/{issue.number}/comments?page={page}")
371 if not array isa JsonArray then break
372 if array.is_empty then break
373 for obj in array do
374 if not obj isa JsonObject then continue
375 var id = obj["id"].as(Int)
376 var comment = load_issue_comment(repo, id)
377 if comment == null then continue
378 res.add(comment)
379 end
380 if res.length >= count then break
381 page += 1
382 end
383 return res
384 end
385
386 # List of events on this issue.
387 fun load_issue_events(repo: Repo, issue: Issue): Array[IssueEvent] do
388 var res = new Array[IssueEvent]
389 var key = "/repos/{repo.full_name}/issues/{issue.number}"
390 var page = 1
391 loop
392 var array = get("{key}/events?page={page}")
393 if not array isa JsonArray or array.is_empty then break
394 for obj in array do
395 if not obj isa JsonObject then continue
396 var event = deserialize(obj.to_json).as(nullable IssueEvent)
397 if event == null then continue
398 res.add event
399 end
400 page += 1
401 end
402 return res
403 end
404
405 # Get the Github pull request #`number`.
406 #
407 # Returns `null` if the pull request cannot be found.
408 #
409 # var api = new GithubAPI(get_github_oauth)
410 # var repo = api.load_repo("nitlang/nit")
411 # assert repo != null
412 # var pull = api.load_pull(repo, 1)
413 # assert pull.title == "Doc"
414 # assert pull.user.login == "Morriar"
415 fun load_pull(repo: Repo, number: Int): nullable PullRequest do
416 return load_from_github("/repos/{repo.full_name}/pulls/{number}").as(nullable PullRequest)
417 end
418
419 # Get the Github label with `name`.
420 #
421 # Returns `null` if the label cannot be found.
422 #
423 # var api = new GithubAPI(get_github_oauth)
424 # var repo = api.load_repo("nitlang/nit")
425 # assert repo != null
426 # var labl = api.load_label(repo, "ok_will_merge")
427 # assert labl != null
428 fun load_label(repo: Repo, name: String): nullable Label do
429 return load_from_github("/repos/{repo.full_name}/labels/{name}").as(nullable Label)
430 end
431
432 # Get the Github milestone with `id`.
433 #
434 # Returns `null` if the milestone cannot be found.
435 #
436 # var api = new GithubAPI(get_github_oauth)
437 # var repo = api.load_repo("nitlang/nit")
438 # assert repo != null
439 # var stone = api.load_milestone(repo, 4)
440 # assert stone.title == "v1.0prealpha"
441 fun load_milestone(repo: Repo, id: Int): nullable Milestone do
442 return load_from_github("/repos/{repo.full_name}/milestones/{id}").as(nullable Milestone)
443 end
444
445 # Get the Github issue event with `id`.
446 #
447 # Returns `null` if the event cannot be found.
448 #
449 # var api = new GithubAPI(get_github_oauth)
450 # var repo = api.load_repo("nitlang/nit")
451 # assert repo isa Repo
452 # var event = api.load_issue_event(repo, 199674194)
453 # assert event isa IssueEvent
454 # assert event.actor.login == "privat"
455 # assert event.event == "labeled"
456 # assert event.labl isa Label
457 # assert event.labl.name == "need_review"
458 fun load_issue_event(repo: Repo, id: Int): nullable IssueEvent do
459 return load_from_github("/repos/{repo.full_name}/issues/events/{id}").as(nullable IssueEvent)
460 end
461
462 # Get the Github commit comment with `id`.
463 #
464 # Returns `null` if the comment cannot be found.
465 #
466 # var api = new GithubAPI(get_github_oauth)
467 # var repo = api.load_repo("nitlang/nit")
468 # assert repo != null
469 # var comment = api.load_commit_comment(repo, 8982707)
470 # assert comment.user.login == "Morriar"
471 # assert comment.body == "For testing purposes...\n"
472 # assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
473 fun load_commit_comment(repo: Repo, id: Int): nullable CommitComment do
474 return load_from_github("/repos/{repo.full_name}/comments/{id}").as(nullable CommitComment)
475 end
476
477 # Get the Github issue comment with `id`.
478 #
479 # Returns `null` if the comment cannot be found.
480 #
481 # var api = new GithubAPI(get_github_oauth)
482 # var repo = api.load_repo("nitlang/nit")
483 # assert repo != null
484 # var comment = api.load_issue_comment(repo, 6020149)
485 # assert comment.user.login == "privat"
486 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
487 # assert comment.issue_number == 10
488 fun load_issue_comment(repo: Repo, id: Int): nullable IssueComment do
489 return load_from_github("/repos/{repo.full_name}/issues/comments/{id}").as(nullable IssueComment)
490 end
491
492 # Get the Github diff comment with `id`.
493 #
494 # Returns `null` if the comment cannot be found.
495 #
496 # var api = new GithubAPI(get_github_oauth)
497 # var repo = api.load_repo("nitlang/nit")
498 # assert repo != null
499 # var comment = api.load_review_comment(repo, 21010363)
500 # assert comment.path == "src/modelize/modelize_property.nit"
501 # assert comment.original_position == 26
502 # assert comment.pull_number == 945
503 fun load_review_comment(repo: Repo, id: Int): nullable ReviewComment do
504 return load_from_github("/repos/{repo.full_name}/pulls/comments/{id}").as(nullable ReviewComment)
505 end
506 end
507
508 # Something returned by the Github API.
509 #
510 # Mainly a Nit wrapper around a JSON objet.
511 abstract class GithubEntity
512 super Jsonable
513 serialize
514
515 # Github page url.
516 var html_url: nullable String is writable
517 end
518
519 # A Github user
520 #
521 # Provides access to [Github user data](https://developer.github.com/v3/users/).
522 # Should be accessed from `GithubAPI::load_user`.
523 class User
524 super GitUser
525 serialize
526
527 # Github login.
528 var login: String is writable
529
530 # Avatar image url for this user.
531 var avatar_url: nullable String is writable
532
533 # User public name if any.
534 var name: nullable String is writable
535
536 # User public email if any.
537 var email: nullable String is writable
538
539 # User public blog if any.
540 var blog: nullable String is writable
541 end
542
543 # A Github repository.
544 #
545 # Provides access to [Github repo data](https://developer.github.com/v3/repos/).
546 # Should be accessed from `GithubAPI::load_repo`.
547 class Repo
548 super GithubEntity
549 serialize
550
551 # Repo full name on Github.
552 var full_name: String is writable
553
554 # Repo short name on Github.
555 var name: String is writable
556
557 # Get the repo owner.
558 var owner: User is writable
559
560 # Repo default branch name.
561 var default_branch: String is writable
562 end
563
564 # A Github branch.
565 #
566 # Should be accessed from `GithubAPI::load_branch`.
567 #
568 # See <https://developer.github.com/v3/repos/#list-branches>.
569 class Branch
570 super GithubEntity
571 serialize
572
573 # Branch name.
574 var name: String is writable
575
576 # Get the last commit of `self`.
577 var commit: Commit is writable
578 end
579
580 # A Github commit.
581 #
582 # Should be accessed from `GithubAPI::load_commit`.
583 #
584 # See <https://developer.github.com/v3/repos/commits/>.
585 class Commit
586 super GithubEntity
587 serialize
588
589 # Commit SHA.
590 var sha: String is writable
591
592 # Parent commits of `self`.
593 var parents: nullable Array[Commit] = null is writable
594
595 # Author of the commit.
596 var author: nullable GitUser is writable
597
598 # Committer of the commit.
599 var committer: nullable GitUser is writable
600
601 # Authoring date as String.
602 var author_date: nullable String is writable
603
604 # Authoring date as ISODate.
605 fun iso_author_date: nullable ISODate do
606 var author_date = self.author_date
607 if author_date == null then return null
608 return new ISODate.from_string(author_date)
609 end
610
611 # Commit date as String.
612 var commit_date: nullable String is writable
613
614 # Commit date as ISODate.
615 fun iso_commit_date: nullable ISODate do
616 var commit_date = self.commit_date
617 if commit_date == null then return null
618 return new ISODate.from_string(commit_date)
619 end
620
621 # List files staged in this commit.
622 var files: nullable Array[GithubFile] = null is optional, writable
623
624 # Commit message.
625 var message: nullable String is writable
626
627 # Git commit representation linked to this commit.
628 var commit: nullable GitCommit
629 end
630
631 # A Git Commit representation
632 class GitCommit
633 super GithubEntity
634 serialize
635
636 # Commit SHA.
637 var sha: nullable String is writable
638
639 # Parent commits of `self`.
640 var parents: nullable Array[GitCommit] = null is writable
641
642 # Author of the commit.
643 var author: nullable GitUser is writable
644
645 # Committer of the commit.
646 var committer: nullable GitUser is writable
647
648 # Commit message.
649 var message: nullable String is writable
650 end
651
652 # Git user authoring data
653 class GitUser
654 super GithubEntity
655 serialize
656
657 # Authoring date.
658 var date: nullable String = null is writable
659
660 # Authoring date as ISODate.
661 fun iso_date: nullable ISODate do
662 var date = self.date
663 if date == null then return null
664 return new ISODate.from_string(date)
665 end
666 end
667
668 # A Github issue.
669 #
670 # Should be accessed from `GithubAPI::load_issue`.
671 #
672 # See <https://developer.github.com/v3/issues/>.
673 class Issue
674 super GithubEntity
675 serialize
676
677 # Issue Github ID.
678 var number: Int is writable
679
680 # Issue id.
681 var id: nullable Int is writable
682
683 # Issue title.
684 var title: String is writable
685
686 # User that created this issue.
687 var user: nullable User is writable
688
689 # List of labels on this issue associated to their names.
690 var labels: nullable Array[Label] is writable
691
692 # State of the issue on Github.
693 var state: String is writable
694
695 # Is the issue locked?
696 var locked: nullable Bool is writable
697
698 # Assigned `User` (if any).
699 var assignee: nullable User is writable
700
701 # `Milestone` (if any).
702 var milestone: nullable Milestone is writable
703
704 # Number of comments on this issue.
705 var comments: nullable Int is writable
706
707 # Creation time as String.
708 var created_at: String is writable
709
710 # Creation time as ISODate.
711 fun iso_created_at: ISODate do
712 return new ISODate.from_string(created_at)
713 end
714
715 # Last update time as String (if any).
716 var updated_at: nullable String is writable
717
718 # Last update date as ISODate.
719 fun iso_updated_at: nullable ISODate do
720 var updated_at = self.updated_at
721 if updated_at == null then return null
722 return new ISODate.from_string(updated_at)
723 end
724
725 # Close time as String (if any).
726 var closed_at: nullable String is writable
727
728 # Close time as ISODate.
729 fun iso_closed_at: nullable ISODate do
730 var closed_at = self.closed_at
731 if closed_at == null then return null
732 return new ISODate.from_string(closed_at)
733 end
734
735 # Full description of the issue.
736 var body: nullable String is writable
737
738 # User that closed this issue (if any).
739 var closed_by: nullable User is writable
740
741 # Is this issue linked to a pull request?
742 var is_pull_request: Bool = false is writable
743 end
744
745 # A Github pull request.
746 #
747 # Should be accessed from `GithubAPI::load_pull`.
748 #
749 # PullRequest are basically Issues with more data.
750 # See <https://developer.github.com/v3/pulls/>.
751 class PullRequest
752 super Issue
753 serialize
754
755 # Merge time as String (if any).
756 var merged_at: nullable String is writable
757
758 # Merge time as ISODate.
759 fun iso_merged_at: nullable ISODate do
760 var merged_at = self.merged_at
761 if merged_at == null then return null
762 return new ISODate.from_string(merged_at)
763 end
764
765 # Merge commit SHA.
766 var merge_commit_sha: nullable String is writable
767
768 # Count of comments made on the pull request diff.
769 var review_comments: Int is writable
770
771 # Pull request head (can be a commit SHA or a branch name).
772 var head: PullRef is writable
773
774 # Pull request base (can be a commit SHA or a branch name).
775 var base: PullRef is writable
776
777 # Is this pull request merged?
778 var merged: Bool is writable
779
780 # Is this pull request mergeable?
781 var mergeable: nullable Bool is writable
782
783 # Mergeable state of this pull request.
784 #
785 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
786 var mergeable_state: String is writable
787
788 # User that merged this pull request (if any).
789 var merged_by: nullable User is writable
790
791 # Count of commits in this pull request.
792 var commits: Int is writable
793
794 # Added line count.
795 var additions: Int is writable
796
797 # Deleted line count.
798 var deletions: Int is writable
799
800 # Changed files count.
801 var changed_files: Int is writable
802
803 # URL to patch file
804 var patch_url: nullable String is writable
805 end
806
807 # A pull request reference (used for head and base).
808 class PullRef
809 serialize
810
811 # Label pointed by `self`.
812 var labl: String is writable, serialize_as("label")
813
814 # Reference pointed by `self`.
815 var ref: String is writable
816
817 # Commit SHA pointed by `self`.
818 var sha: String is writable
819
820 # User pointed by `self`.
821 var user: User is writable
822
823 # Repo pointed by `self` (if any).
824 #
825 # A `null` value means the `repo` was deleted.
826 var repo: nullable Repo is writable
827 end
828
829 # A Github label.
830 #
831 # Should be accessed from `GithubAPI::load_label`.
832 #
833 # See <https://developer.github.com/v3/issues/labels/>.
834 class Label
835 super GithubEntity
836 serialize
837
838 # Label name.
839 var name: String is writable
840
841 # Label color code.
842 var color: String is writable
843 end
844
845 # A Github milestone.
846 #
847 # Should be accessed from `GithubAPI::load_milestone`.
848 #
849 # See <https://developer.github.com/v3/issues/milestones/>.
850 class Milestone
851 super GithubEntity
852 serialize
853
854 # The milestone id on Github.
855 var number: nullable Int = null is writable
856
857 # Milestone title.
858 var title: String is writable
859
860 # Milestone long description.
861 var description: nullable String is writable
862
863 # Count of opened issues linked to this milestone.
864 var open_issues: nullable Int = null is writable
865
866 # Count of closed issues linked to this milestone.
867 var closed_issues: nullable Int = null is writable
868
869 # Milestone state.
870 var state: nullable String is writable
871
872 # Creation time as String.
873 var created_at: nullable String is writable
874
875 # Creation time as ISODate.
876 fun iso_created_at: nullable ISODate do
877 var created_at = self.created_at
878 if created_at == null then return null
879 return new ISODate.from_string(created_at)
880 end
881
882 # User that created this milestone.
883 var creator: nullable User is writable
884
885 # Due time as String (if any).
886 var due_on: nullable String is writable
887
888 # Due time in ISODate format (if any).
889 fun iso_due_on: nullable ISODate do
890 var due_on = self.due_on
891 if due_on == null then return null
892 return new ISODate.from_string(due_on)
893 end
894
895 # Last update time as String (if any).
896 var updated_at: nullable String is writable
897
898 # Last update date as ISODate.
899 fun iso_updated_at: nullable ISODate do
900 var updated_at = self.updated_at
901 if updated_at == null then return null
902 return new ISODate.from_string(updated_at)
903 end
904
905 # Close time as String (if any).
906 var closed_at: nullable String is writable
907
908 # Close time as ISODate.
909 fun iso_closed_at: nullable ISODate do
910 var closed_at = self.closed_at
911 if closed_at == null then return null
912 return new ISODate.from_string(closed_at)
913 end
914 end
915
916 # A Github comment
917 #
918 # There is two kinds of comments:
919 #
920 # * `CommitComment` are made on a commit page.
921 # * `IssueComment` are made on an issue or pull request page.
922 # * `ReviewComment` are made on the diff associated to a pull request.
923 abstract class Comment
924 super GithubEntity
925 serialize
926
927 # Identifier of this comment.
928 var id: Int is writable
929
930 # User that made this comment.
931 var user: User is writable
932
933 # Creation time as String.
934 var created_at: String is writable
935
936 # Creation time as ISODate.
937 fun iso_created_at: nullable ISODate do
938 return new ISODate.from_string(created_at)
939 end
940
941 # Last update time as String (if any).
942 var updated_at: nullable String is writable
943
944 # Last update date as ISODate.
945 fun iso_updated_at: nullable ISODate do
946 var updated_at = self.updated_at
947 if updated_at == null then return null
948 return new ISODate.from_string(updated_at)
949 end
950
951 # Comment body text.
952 var body: String is writable
953
954 # Does the comment contain an acknowledgement (+1)
955 fun is_ack: Bool do
956 return body.has("\\+1\\b".to_re) or body.has(":+1:") or body.has(":shipit:")
957 end
958 end
959
960 # A comment made on a commit.
961 class CommitComment
962 super Comment
963 serialize
964
965 # Commented commit.
966 var commit_id: String is writable
967
968 # Position of the comment on the line.
969 var position: nullable Int is writable
970
971 # Line of the comment.
972 var line: nullable Int is writable
973
974 # Path of the commented file.
975 var path: nullable String is writable
976 end
977
978 # Comments made on Github issue and pull request pages.
979 #
980 # Should be accessed from `GithubAPI::load_issue_comment`.
981 #
982 # See <https://developer.github.com/v3/issues/comments/>.
983 class IssueComment
984 super Comment
985 serialize
986
987 # Issue that contains `self`.
988 fun issue_number: Int do return issue_url.split("/").last.to_i
989
990 # Link to the issue document on API.
991 var issue_url: String is writable
992 end
993
994 # Comments made on Github pull request diffs.
995 #
996 # Should be accessed from `GithubAPI::load_diff_comment`.
997 #
998 # See <https://developer.github.com/v3/pulls/comments/>.
999 class ReviewComment
1000 super Comment
1001 serialize
1002
1003 # Pull request that contains `self`.
1004 fun pull_number: Int do return pull_request_url.split("/").last.to_i
1005
1006 # Link to the pull request on API.
1007 var pull_request_url: String is writable
1008
1009 # Diff hunk.
1010 var diff_hunk: String is writable
1011
1012 # Path of commented file.
1013 var path: String is writable
1014
1015 # Position of the comment on the file.
1016 var position: nullable Int is writable
1017
1018 # Original position in the diff.
1019 var original_position: Int is writable
1020
1021 # Commit referenced by this comment.
1022 var commit_id: String is writable
1023
1024 # Original commit id.
1025 var original_commit_id: String is writable
1026 end
1027
1028 # An event that occurs on a Github `Issue`.
1029 #
1030 # Should be accessed from `GithubAPI::load_issue_event`.
1031 #
1032 # See <https://developer.github.com/v3/issues/events/>.
1033 class IssueEvent
1034 super GithubEntity
1035 serialize
1036
1037 # Event id on Github.
1038 var id: Int is writable
1039
1040 # User that initiated the event.
1041 var actor: User is writable
1042
1043 # Creation time as String.
1044 var created_at: String is writable
1045
1046 # Creation time as ISODate.
1047 fun iso_created_at: nullable ISODate do
1048 return new ISODate.from_string(created_at)
1049 end
1050
1051 # Event descriptor.
1052 var event: String is writable
1053
1054 # Commit linked to this event (if any).
1055 var commit_id: nullable String is writable
1056
1057 # Label linked to this event (if any).
1058 var labl: nullable Label is writable, serialize_as("label")
1059
1060 # User linked to this event (if any).
1061 var assignee: nullable User is writable
1062
1063 # Milestone linked to this event (if any).
1064 var milestone: nullable Milestone is writable
1065
1066 # Rename linked to this event (if any).
1067 var rename: nullable RenameAction is writable
1068 end
1069
1070 # A rename action maintains the name before and after a renaming action.
1071 class RenameAction
1072 serialize
1073
1074 # Name before renaming.
1075 var from: String is writable
1076
1077 # Name after renaming.
1078 var to: String is writable
1079 end
1080
1081 #
1082 # Should be accessed from `Repo::contrib_stats`.
1083 #
1084 # See <https://developer.github.com/v3/repos/statistics/>.
1085 class ContributorStats
1086 super Comparable
1087 serialize
1088
1089 redef type OTHER: ContributorStats
1090
1091 # Github API client.
1092 var api: GithubAPI is writable
1093
1094 # User these statistics are about.
1095 var author: User is writable
1096
1097 # Total number of commit.
1098 var total: Int is writable
1099
1100 # Are of weeks of activity with detailed statistics.
1101 var weeks: JsonArray is writable
1102
1103 # ContributorStats can be compared on the total amount of commits.
1104 redef fun <(o) do return total < o.total
1105 end
1106
1107 # A Github file representation.
1108 #
1109 # Mostly a wrapper around a json object.
1110 class GithubFile
1111 serialize
1112
1113 # File name.
1114 var filename: String is writable
1115 end
1116
1117 # Make ISO Datew serilizable
1118 redef class ISODate
1119 super Jsonable
1120 serialize
1121 end
1122
1123 # JsonDeserializer specific for Github objects.
1124 class GithubDeserializer
1125 super JsonDeserializer
1126
1127 redef fun class_name_heuristic(json_object) do
1128 if json_object.has_key("login") then
1129 return "User"
1130 else if json_object.has_key("full_name") then
1131 return "Repo"
1132 else if json_object.has_key("name") and json_object.has_key("commit") then
1133 return "Branch"
1134 else if json_object.has_key("sha") and json_object.has_key("ref") then
1135 return "PullRef"
1136 else if (json_object.has_key("sha") and json_object.has_key("commit")) or (json_object.has_key("id") and json_object.has_key("tree_id")) then
1137 return "Commit"
1138 else if json_object.has_key("sha") and json_object.has_key("tree") then
1139 return "GitCommit"
1140 else if json_object.has_key("name") and json_object.has_key("date") then
1141 return "GitUser"
1142 else if json_object.has_key("number") and json_object.has_key("patch_url") then
1143 return "PullRequest"
1144 else if json_object.has_key("open_issues") and json_object.has_key("closed_issues") then
1145 return "Milestone"
1146 else if json_object.has_key("number") and json_object.has_key("title") then
1147 return "Issue"
1148 else if json_object.has_key("color") then
1149 return "Label"
1150 else if json_object.has_key("event") then
1151 return "IssueEvent"
1152 else if json_object.has_key("original_commit_id") then
1153 return "ReviewComment"
1154 else if json_object.has_key("commit_id") then
1155 return "CommitComment"
1156 else if json_object.has_key("issue_url") then
1157 return "IssueComment"
1158 end
1159 return null
1160 end
1161
1162 redef fun deserialize_class(name) do
1163 if name == "Issue" then
1164 var issue = super.as(Issue)
1165 if path.last.has_key("pull_request") then
1166 issue.is_pull_request = true
1167 end
1168 return issue
1169 else if name == "Commit" then
1170 var commit = super.as(Commit)
1171 var git_commit = commit.commit
1172 if git_commit != null then commit.message = git_commit.message
1173 return commit
1174 end
1175 return super
1176 end
1177 end