Merge: json::serialization: fallback to the static type when there is no metadata...
[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
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 return deserialize(array.to_json).as(Array[Branch])
204 end
205
206 # List of issues associated with their ids.
207 fun load_repo_issues(repo: Repo): Array[Issue] do
208 message(1, "Get issues for {repo.full_name}")
209 var res = new Array[Issue]
210 var issue = load_repo_last_issue(repo)
211 if issue == null then return res
212 res.add issue
213 while issue != null and issue.number > 1 do
214 issue = load_issue(repo, issue.number - 1)
215 if issue == null then continue
216 res.add issue
217 end
218 return res
219 end
220
221 # Search issues in this repo form an advanced query.
222 #
223 # Example:
224 #
225 # ~~~nitish
226 # var issues = repo.search_issues("is:open label:need_review")
227 # ~~~
228 #
229 # See <https://developer.github.com/v3/search/#search-issues>.
230 fun search_repo_issues(repo: Repo, query: String): Array[Issue] do
231 query = "/search/issues?q={query} repo:{repo.full_name}"
232 var res = new Array[Issue]
233 var response = get(query)
234 if was_error then return res
235 var arr = response.as(JsonObject)["items"].as(JsonArray)
236 return deserialize(arr.to_json).as(Array[Issue])
237 end
238
239 # Get the last published issue.
240 fun load_repo_last_issue(repo: Repo): nullable Issue do
241 var array = get("/repos/{repo.full_name}/issues")
242 if not array isa JsonArray then return null
243 if array.is_empty then return null
244 var obj = array.first
245 if not obj isa JsonObject then return null
246 return deserialize(obj.to_json).as(nullable Issue)
247 end
248
249 # List of labels associated with their names.
250 fun load_repo_labels(repo: Repo): Array[Label] do
251 message(1, "Get labels for {repo.full_name}")
252 var array = get("repos/{repo.full_name}/labels")
253 if not array isa JsonArray then return new Array[Label]
254 return deserialize(array.to_json).as(Array[Label])
255 end
256
257 # List of milestones associated with their ids.
258 fun load_repo_milestones(repo: Repo): Array[Milestone] do
259 message(1, "Get milestones for {repo.full_name}")
260 var array = get("/repos/{repo.full_name}/milestones")
261 if not array isa JsonArray then return new Array[Milestone]
262 return deserialize(array.to_json).as(Array[Milestone])
263 end
264
265 # List of pull-requests associated with their ids.
266 #
267 # Implementation notes: because PR numbers are not consecutive,
268 # PR are loaded from pages.
269 # See: https://developer.github.com/v3/pulls/#list-pull-requests
270 fun load_repo_pulls(repo: Repo): Array[PullRequest] do
271 message(1, "Get pulls for {repo.full_name}")
272 var key = "/repos/{repo.full_name}"
273 var res = new Array[PullRequest]
274 var page = 1
275 loop
276 var array = get("{key}/pulls?page={page}").as(JsonArray)
277 if array.is_empty then break
278 for obj in array do
279 if not obj isa JsonObject then continue
280 var pr = deserialize(array.to_json).as(nullable PullRequest)
281 if pr == null then continue
282 res.add pr
283 end
284 page += 1
285 end
286 return res
287 end
288
289 # List of contributor related statistics.
290 fun load_repo_contrib_stats(repo: Repo): Array[ContributorStats] do
291 message(1, "Get contributor stats for {repo.full_name}")
292 var res = new Array[ContributorStats]
293 var array = get("/repos/{repo.full_name}/stats/contributors")
294 if not array isa JsonArray then return res
295 return deserialize(array.to_json).as(Array[ContributorStats])
296 end
297
298 # Get the Github branch with `name`.
299 #
300 # Returns `null` if the branch cannot be found.
301 #
302 # var api = new GithubAPI(get_github_oauth)
303 # var repo = api.load_repo("nitlang/nit")
304 # assert repo != null
305 # var branch = api.load_branch(repo, "master")
306 # assert branch.name == "master"
307 # assert branch.commit isa Commit
308 fun load_branch(repo: Repo, name: String): nullable Branch do
309 return load_from_github("/repos/{repo.full_name}/branches/{name}").as(nullable Branch)
310 end
311
312 # List all commits in `self`.
313 #
314 # This can be long depending on the branch size.
315 # Commit are returned in an unspecified order.
316 fun load_branch_commits(branch: Branch): Array[Commit] do
317 var res = new Array[Commit]
318 var done = new HashSet[String]
319 var todos = new Array[Commit]
320 todos.add branch.commit
321 loop
322 if todos.is_empty then break
323 var commit = todos.pop
324 if done.has(commit.sha) then continue
325 done.add commit.sha
326 res.add commit
327 var parents = commit.parents
328 if parents == null then continue
329 for parent in parents do
330 todos.add parent
331 end
332 end
333 return res
334 end
335
336 # Get the Github commit with `sha`.
337 #
338 # Returns `null` if the commit cannot be found.
339 #
340 # var api = new GithubAPI(get_github_oauth)
341 # var repo = api.load_repo("nitlang/nit")
342 # assert repo != null
343 # var commit = api.load_commit(repo, "64ce1f")
344 # assert commit isa Commit
345 fun load_commit(repo: Repo, sha: String): nullable Commit do
346 return load_from_github("/repos/{repo.full_name}/commits/{sha}").as(nullable Commit)
347 end
348
349 # Get the Github issue #`number`.
350 #
351 # Returns `null` if the issue cannot be found.
352 #
353 # var api = new GithubAPI(get_github_oauth)
354 # var repo = api.load_repo("nitlang/nit")
355 # assert repo != null
356 # var issue = api.load_issue(repo, 1)
357 # assert issue.title == "Doc"
358 fun load_issue(repo: Repo, number: Int): nullable Issue do
359 return load_from_github("/repos/{repo.full_name}/issues/{number}").as(nullable Issue)
360 end
361
362 # List of event on this issue.
363 fun load_issue_comments(repo: Repo, issue: Issue): Array[IssueComment] do
364 var res = new Array[IssueComment]
365 var count = issue.comments or else 0
366 var page = 1
367 loop
368 var array = get("/repos/{repo.full_name}/comments?page={page}")
369 if not array isa JsonArray then break
370 if array.is_empty or res.length < count then break
371 for obj in array do
372 if not obj isa JsonObject then continue
373 var id = obj["id"].as(Int)
374 var comment = load_issue_comment(repo, id)
375 if comment == null then continue
376 res.add(comment)
377 end
378 page += 1
379 end
380 return res
381 end
382
383 # List of events on this issue.
384 fun load_issue_events(repo: Repo, issue: Issue): Array[IssueEvent] do
385 var res = new Array[IssueEvent]
386 var key = "/repos/{repo.full_name}/issues/{issue.number}"
387 var page = 1
388 loop
389 var array = get("{key}/events?page={page}")
390 if not array isa JsonArray or array.is_empty then break
391 for obj in array do
392 if not obj isa JsonObject then continue
393 var event = deserialize(obj.to_json).as(nullable IssueEvent)
394 if event == null then continue
395 res.add event
396 end
397 page += 1
398 end
399 return res
400 end
401
402 # Get the Github pull request #`number`.
403 #
404 # Returns `null` if the pull request cannot be found.
405 #
406 # var api = new GithubAPI(get_github_oauth)
407 # var repo = api.load_repo("nitlang/nit")
408 # assert repo != null
409 # var pull = api.load_pull(repo, 1)
410 # assert pull.title == "Doc"
411 # assert pull.user.login == "Morriar"
412 fun load_pull(repo: Repo, number: Int): nullable PullRequest do
413 return load_from_github("/repos/{repo.full_name}/pulls/{number}").as(nullable PullRequest)
414 end
415
416 # Get the Github label with `name`.
417 #
418 # Returns `null` if the label cannot be found.
419 #
420 # var api = new GithubAPI(get_github_oauth)
421 # var repo = api.load_repo("nitlang/nit")
422 # assert repo != null
423 # var labl = api.load_label(repo, "ok_will_merge")
424 # assert labl != null
425 fun load_label(repo: Repo, name: String): nullable Label do
426 return load_from_github("/repos/{repo.full_name}/labels/{name}").as(nullable Label)
427 end
428
429 # Get the Github milestone with `id`.
430 #
431 # Returns `null` if the milestone cannot be found.
432 #
433 # var api = new GithubAPI(get_github_oauth)
434 # var repo = api.load_repo("nitlang/nit")
435 # assert repo != null
436 # var stone = api.load_milestone(repo, 4)
437 # assert stone.title == "v1.0prealpha"
438 fun load_milestone(repo: Repo, id: Int): nullable Milestone do
439 return load_from_github("/repos/{repo.full_name}/milestones/{id}").as(nullable Milestone)
440 end
441
442 # Get the Github issue event with `id`.
443 #
444 # Returns `null` if the event cannot be found.
445 #
446 # var api = new GithubAPI(get_github_oauth)
447 # var repo = api.load_repo("nitlang/nit")
448 # assert repo isa Repo
449 # var event = api.load_issue_event(repo, 199674194)
450 # assert event isa IssueEvent
451 # assert event.actor.login == "privat"
452 # assert event.event == "labeled"
453 # assert event.labl isa Label
454 # assert event.labl.name == "need_review"
455 fun load_issue_event(repo: Repo, id: Int): nullable IssueEvent do
456 return load_from_github("/repos/{repo.full_name}/issues/events/{id}").as(nullable IssueEvent)
457 end
458
459 # Get the Github commit comment with `id`.
460 #
461 # Returns `null` if the comment cannot be found.
462 #
463 # var api = new GithubAPI(get_github_oauth)
464 # var repo = api.load_repo("nitlang/nit")
465 # assert repo != null
466 # var comment = api.load_commit_comment(repo, 8982707)
467 # assert comment.user.login == "Morriar"
468 # assert comment.body == "For testing purposes..."
469 # assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
470 fun load_commit_comment(repo: Repo, id: Int): nullable CommitComment do
471 return load_from_github("/repos/{repo.full_name}/comments/{id}").as(nullable CommitComment)
472 end
473
474 # Get the Github issue comment with `id`.
475 #
476 # Returns `null` if the comment cannot be found.
477 #
478 # var api = new GithubAPI(get_github_oauth)
479 # var repo = api.load_repo("nitlang/nit")
480 # assert repo != null
481 # var comment = api.load_issue_comment(repo, 6020149)
482 # assert comment.user.login == "privat"
483 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
484 # assert comment.issue_number == 10
485 fun load_issue_comment(repo: Repo, id: Int): nullable IssueComment do
486 return load_from_github("/repos/{repo.full_name}/issues/comments/{id}").as(nullable IssueComment)
487 end
488
489 # Get the Github diff comment with `id`.
490 #
491 # Returns `null` if the comment cannot be found.
492 #
493 # var api = new GithubAPI(get_github_oauth)
494 # var repo = api.load_repo("nitlang/nit")
495 # assert repo != null
496 # var comment = api.load_review_comment(repo, 21010363)
497 # assert comment.path == "src/modelize/modelize_property.nit"
498 # assert comment.original_position == 26
499 # assert comment.pull_number == 945
500 fun load_review_comment(repo: Repo, id: Int): nullable ReviewComment do
501 return load_from_github("/repos/{repo.full_name}/pulls/comments/{id}").as(nullable ReviewComment)
502 end
503 end
504
505 # Something returned by the Github API.
506 #
507 # Mainly a Nit wrapper around a JSON objet.
508 abstract class GithubEntity
509 super Jsonable
510 serialize
511
512 # Github page url.
513 var html_url: nullable String is writable
514
515 redef fun to_json do return serialize_to_json
516 end
517
518 # A Github user
519 #
520 # Provides access to [Github user data](https://developer.github.com/v3/users/).
521 # Should be accessed from `GithubAPI::load_user`.
522 class User
523 super GithubEntity
524 serialize
525
526 # Github login.
527 var login: String is writable
528
529 # Avatar image url for this user.
530 var avatar_url: nullable String is writable
531 end
532
533 # A Github repository.
534 #
535 # Provides access to [Github repo data](https://developer.github.com/v3/repos/).
536 # Should be accessed from `GithubAPI::load_repo`.
537 class Repo
538 super GithubEntity
539 serialize
540
541 # Repo full name on Github.
542 var full_name: String is writable
543
544 # Repo short name on Github.
545 var name: String is writable
546
547 # Get the repo owner.
548 var owner: User is writable
549
550 # Repo default branch name.
551 var default_branch: String is writable
552 end
553
554 # A Github branch.
555 #
556 # Should be accessed from `GithubAPI::load_branch`.
557 #
558 # See <https://developer.github.com/v3/repos/#list-branches>.
559 class Branch
560 super GithubEntity
561 serialize
562
563 # Branch name.
564 var name: String is writable
565
566 # Get the last commit of `self`.
567 var commit: Commit is writable
568 end
569
570 # A Github commit.
571 #
572 # Should be accessed from `GithubAPI::load_commit`.
573 #
574 # See <https://developer.github.com/v3/repos/commits/>.
575 class Commit
576 super GithubEntity
577 serialize
578
579 # Commit SHA.
580 var sha: String is writable
581
582 # Parent commits of `self`.
583 var parents: nullable Array[Commit] = null is writable
584
585 # Author of the commit.
586 var author: nullable User is writable
587
588 # Committer of the commit.
589 var committer: nullable User is writable
590
591 # Authoring date as String.
592 var author_date: nullable String is writable
593
594 # Authoring date as ISODate.
595 fun iso_author_date: nullable ISODate do
596 var author_date = self.author_date
597 if author_date == null then return null
598 return new ISODate.from_string(author_date)
599 end
600
601 # Commit date as String.
602 var commit_date: nullable String is writable
603
604 # Commit date as ISODate.
605 fun iso_commit_date: nullable ISODate do
606 var commit_date = self.commit_date
607 if commit_date == null then return null
608 return new ISODate.from_string(commit_date)
609 end
610
611 # List files staged in this commit.
612 var files: nullable Array[GithubFile] = null is optional, writable
613
614 # Commit message.
615 var message: nullable String is writable
616 end
617
618 # A Github issue.
619 #
620 # Should be accessed from `GithubAPI::load_issue`.
621 #
622 # See <https://developer.github.com/v3/issues/>.
623 class Issue
624 super GithubEntity
625 serialize
626
627 # Issue Github ID.
628 var number: Int is writable
629
630 # Issue id.
631 var id: nullable Int is writable
632
633 # Issue title.
634 var title: String is writable
635
636 # User that created this issue.
637 var user: nullable User is writable
638
639 # List of labels on this issue associated to their names.
640 var labels: nullable Array[Label] is writable
641
642 # State of the issue on Github.
643 var state: String is writable
644
645 # Is the issue locked?
646 var locked: nullable Bool is writable
647
648 # Assigned `User` (if any).
649 var assignee: nullable User is writable
650
651 # `Milestone` (if any).
652 var milestone: nullable Milestone is writable
653
654 # Number of comments on this issue.
655 var comments: nullable Int is writable
656
657 # Creation time as String.
658 var created_at: String is writable
659
660 # Creation time as ISODate.
661 fun iso_created_at: ISODate do
662 return new ISODate.from_string(created_at)
663 end
664
665 # Last update time as String (if any).
666 var updated_at: nullable String is writable
667
668 # Last update date as ISODate.
669 fun iso_updated_at: nullable ISODate do
670 var updated_at = self.updated_at
671 if updated_at == null then return null
672 return new ISODate.from_string(updated_at)
673 end
674
675 # Close time as String (if any).
676 var closed_at: nullable String is writable
677
678 # Close time as ISODate.
679 fun iso_closed_at: nullable ISODate do
680 var closed_at = self.closed_at
681 if closed_at == null then return null
682 return new ISODate.from_string(closed_at)
683 end
684
685 # Full description of the issue.
686 var body: nullable String is writable
687
688 # User that closed this issue (if any).
689 var closed_by: nullable User is writable
690
691 # Is this issue linked to a pull request?
692 var is_pull_request: Bool is noserialize, writable
693 end
694
695 # A Github pull request.
696 #
697 # Should be accessed from `GithubAPI::load_pull`.
698 #
699 # PullRequest are basically Issues with more data.
700 # See <https://developer.github.com/v3/pulls/>.
701 class PullRequest
702 super Issue
703 serialize
704
705 # Merge time as String (if any).
706 var merged_at: nullable String is writable
707
708 # Merge time as ISODate.
709 fun iso_merged_at: nullable ISODate do
710 var merged_at = self.merged_at
711 if merged_at == null then return null
712 return new ISODate.from_string(merged_at)
713 end
714
715 # Merge commit SHA.
716 var merge_commit_sha: nullable String is writable
717
718 # Count of comments made on the pull request diff.
719 var review_comments: Int is writable
720
721 # Pull request head (can be a commit SHA or a branch name).
722 var head: PullRef is writable
723
724 # Pull request base (can be a commit SHA or a branch name).
725 var base: PullRef is writable
726
727 # Is this pull request merged?
728 var merged: Bool is writable
729
730 # Is this pull request mergeable?
731 var mergeable: nullable Bool is writable
732
733 # Mergeable state of this pull request.
734 #
735 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
736 var mergeable_state: String is writable
737
738 # User that merged this pull request (if any).
739 var merged_by: nullable User is writable
740
741 # Count of commits in this pull request.
742 var commits: Int is writable
743
744 # Added line count.
745 var additions: Int is writable
746
747 # Deleted line count.
748 var deletions: Int is writable
749
750 # Changed files count.
751 var changed_files: Int is writable
752 end
753
754 # A pull request reference (used for head and base).
755 class PullRef
756 serialize
757
758 # Label pointed by `self`.
759 var labl: String is writable, serialize_as("label")
760
761 # Reference pointed by `self`.
762 var ref: String is writable
763
764 # Commit SHA pointed by `self`.
765 var sha: String is writable
766
767 # User pointed by `self`.
768 var user: User is writable
769
770 # Repo pointed by `self`.
771 var repo: Repo is writable
772 end
773
774 # A Github label.
775 #
776 # Should be accessed from `GithubAPI::load_label`.
777 #
778 # See <https://developer.github.com/v3/issues/labels/>.
779 class Label
780 super GithubEntity
781 serialize
782
783 # Label name.
784 var name: String is writable
785
786 # Label color code.
787 var color: String is writable
788 end
789
790 # A Github milestone.
791 #
792 # Should be accessed from `GithubAPI::load_milestone`.
793 #
794 # See <https://developer.github.com/v3/issues/milestones/>.
795 class Milestone
796 super GithubEntity
797 serialize
798
799 # The milestone id on Github.
800 var number: Int is writable
801
802 # Milestone title.
803 var title: String is writable
804
805 # Milestone long description.
806 var description: String is writable
807
808 # Count of opened issues linked to this milestone.
809 var open_issues: Int is writable
810
811 # Count of closed issues linked to this milestone.
812 var closed_issues: Int is writable
813
814 # Milestone state.
815 var state: String is writable
816
817 # Creation time as String.
818 var created_at: String is writable
819
820 # Creation time as ISODate.
821 fun iso_created_at: nullable ISODate do return new ISODate.from_string(created_at)
822
823 # User that created this milestone.
824 var creator: User is writable
825
826 # Due time as String (if any).
827 var due_on: nullable String is writable
828
829 # Due time in ISODate format (if any).
830 fun iso_due_on: nullable ISODate do
831 var due_on = self.due_on
832 if due_on == null then return null
833 return new ISODate.from_string(due_on)
834 end
835
836 # Last update time as String (if any).
837 var updated_at: nullable String is writable
838
839 # Last update date as ISODate.
840 fun iso_updated_at: nullable ISODate do
841 var updated_at = self.updated_at
842 if updated_at == null then return null
843 return new ISODate.from_string(updated_at)
844 end
845
846 # Close time as String (if any).
847 var closed_at: nullable String is writable
848
849 # Close time as ISODate.
850 fun iso_closed_at: nullable ISODate do
851 var closed_at = self.closed_at
852 if closed_at == null then return null
853 return new ISODate.from_string(closed_at)
854 end
855 end
856
857 # A Github comment
858 #
859 # There is two kinds of comments:
860 #
861 # * `CommitComment` are made on a commit page.
862 # * `IssueComment` are made on an issue or pull request page.
863 # * `ReviewComment` are made on the diff associated to a pull request.
864 abstract class Comment
865 super GithubEntity
866 serialize
867
868 # Identifier of this comment.
869 var id: Int is writable
870
871 # User that made this comment.
872 var user: User is writable
873
874 # Creation time as String.
875 var created_at: String is writable
876
877 # Creation time as ISODate.
878 fun iso_created_at: nullable ISODate do
879 return new ISODate.from_string(created_at)
880 end
881
882 # Last update time as String (if any).
883 var updated_at: nullable String is writable
884
885 # Last update date as ISODate.
886 fun iso_updated_at: nullable ISODate do
887 var updated_at = self.updated_at
888 if updated_at == null then return null
889 return new ISODate.from_string(updated_at)
890 end
891
892 # Comment body text.
893 var body: String is writable
894
895 # Does the comment contain an acknowledgement (+1)
896 fun is_ack: Bool do
897 return body.has("\\+1\\b".to_re) or body.has(":+1:") or body.has(":shipit:")
898 end
899 end
900
901 # A comment made on a commit.
902 class CommitComment
903 super Comment
904 serialize
905
906 # Commented commit.
907 var commit_id: String is writable
908
909 # Position of the comment on the line.
910 var position: nullable Int is writable
911
912 # Line of the comment.
913 var line: nullable Int is writable
914
915 # Path of the commented file.
916 var path: nullable String is writable
917 end
918
919 # Comments made on Github issue and pull request pages.
920 #
921 # Should be accessed from `GithubAPI::load_issue_comment`.
922 #
923 # See <https://developer.github.com/v3/issues/comments/>.
924 class IssueComment
925 super Comment
926 serialize
927
928 # Issue that contains `self`.
929 fun issue_number: Int do return issue_url.split("/").last.to_i
930
931 # Link to the issue document on API.
932 var issue_url: String is writable
933 end
934
935 # Comments made on Github pull request diffs.
936 #
937 # Should be accessed from `GithubAPI::load_diff_comment`.
938 #
939 # See <https://developer.github.com/v3/pulls/comments/>.
940 class ReviewComment
941 super Comment
942 serialize
943
944 # Pull request that contains `self`.
945 fun pull_number: Int do return pull_request_url.split("/").last.to_i
946
947 # Link to the pull request on API.
948 var pull_request_url: String is writable
949
950 # Diff hunk.
951 var diff_hunk: String is writable
952
953 # Path of commented file.
954 var path: String is writable
955
956 # Position of the comment on the file.
957 var position: nullable Int is writable
958
959 # Original position in the diff.
960 var original_position: Int is writable
961
962 # Commit referenced by this comment.
963 var commit_id: String is writable
964
965 # Original commit id.
966 var original_commit_id: String is writable
967 end
968
969 # An event that occurs on a Github `Issue`.
970 #
971 # Should be accessed from `GithubAPI::load_issue_event`.
972 #
973 # See <https://developer.github.com/v3/issues/events/>.
974 class IssueEvent
975 super GithubEntity
976 serialize
977
978 # Event id on Github.
979 var id: Int is writable
980
981 # User that initiated the event.
982 var actor: User is writable
983
984 # Creation time as String.
985 var created_at: String is writable
986
987 # Creation time as ISODate.
988 fun iso_created_at: nullable ISODate do
989 return new ISODate.from_string(created_at)
990 end
991
992 # Event descriptor.
993 var event: String is writable
994
995 # Commit linked to this event (if any).
996 var commit_id: nullable String is writable
997
998 # Label linked to this event (if any).
999 var labl: nullable Label is writable, serialize_as("label")
1000
1001 # User linked to this event (if any).
1002 var assignee: nullable User is writable
1003
1004 # Milestone linked to this event (if any).
1005 var milestone: nullable Milestone is writable
1006
1007 # Rename linked to this event (if any).
1008 var rename: nullable RenameAction is writable
1009 end
1010
1011 # A rename action maintains the name before and after a renaming action.
1012 class RenameAction
1013 serialize
1014
1015 # Name before renaming.
1016 var from: String is writable
1017
1018 # Name after renaming.
1019 var to: String is writable
1020 end
1021
1022 #
1023 # Should be accessed from `Repo::contrib_stats`.
1024 #
1025 # See <https://developer.github.com/v3/repos/statistics/>.
1026 class ContributorStats
1027 super Comparable
1028 serialize
1029
1030 redef type OTHER: ContributorStats
1031
1032 # Github API client.
1033 var api: GithubAPI is writable
1034
1035 # User these statistics are about.
1036 var author: User is writable
1037
1038 # Total number of commit.
1039 var total: Int is writable
1040
1041 # Are of weeks of activity with detailed statistics.
1042 var weeks: JsonArray is writable
1043
1044 # ContributorStats can be compared on the total amount of commits.
1045 redef fun <(o) do return total < o.total
1046 end
1047
1048 # A Github file representation.
1049 #
1050 # Mostly a wrapper around a json object.
1051 class GithubFile
1052 serialize
1053
1054 # File name.
1055 var filename: String is writable
1056 end
1057
1058 # Make ISO Datew serilizable
1059 redef class ISODate
1060 super Jsonable
1061 serialize
1062
1063 redef fun to_json do return serialize_to_json
1064 end
1065
1066 # JsonDeserializer specific for Github objects.
1067 class GithubDeserializer
1068 super JsonDeserializer
1069
1070 redef fun class_name_heuristic(json_object) do
1071 if json_object.has_key("login") or json_object.has_key("email") then
1072 return "User"
1073 else if json_object.has_key("full_name") then
1074 return "Repo"
1075 else if json_object.has_key("name") and json_object.has_key("commit") then
1076 return "Branch"
1077 else if json_object.has_key("sha") and json_object.has_key("ref") then
1078 return "PullRef"
1079 else if json_object.has_key("sha") or (json_object.has_key("id") and json_object.has_key("tree_id")) then
1080 return "Commit"
1081 else if json_object.has_key("number") and json_object.has_key("patch_url") then
1082 return "PullRequest"
1083 else if json_object.has_key("open_issues") and json_object.has_key("closed_issues") then
1084 return "Milestone"
1085 else if json_object.has_key("number") and json_object.has_key("title") then
1086 return "Issue"
1087 else if json_object.has_key("color") then
1088 return "Label"
1089 else if json_object.has_key("event") then
1090 return "IssueEvent"
1091 else if json_object.has_key("original_commit_id") then
1092 return "ReviewComment"
1093 else if json_object.has_key("commit_id") then
1094 return "CommitComment"
1095 else if json_object.has_key("issue_url") then
1096 return "IssueComment"
1097 end
1098 return null
1099 end
1100 end