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