1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Nit object oriented interface to [Github api](https://developer.github.com/v3/).
17 # This modules reifies Github API elements as Nit classes.
19 # For most use-cases you need to use the `GithubAPI` client.
22 intrude import json
::serialization_read
28 # Client to Github API
30 # To access the API you need an instance of a `GithubAPI` client.
33 # # Get Github authentification token.
34 # var token = get_github_oauth
35 # assert not token.is_empty
38 # var api = new GithubAPI(token)
41 # The API client allows you to get Github API entities.
44 # var repo = api.get_repo("nitlang/nit")
46 # assert repo.name == "nit"
48 # var user = api.get_user("Morriar")
50 # assert user.login == "Morriar"
54 # Github API OAuth token
56 # To access your private ressources, you must
57 # [authenticate](https://developer.github.com/guides/basics-of-authentication/).
59 # For client applications, Github recommands to use the
60 # [OAuth tokens](https://developer.github.com/v3/oauth/) authentification method.
64 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
65 # associated to the key.
66 var auth
: nullable String = null is optional
68 # User agent used for HTTP requests.
70 # Default is `nit_github_api`.
72 # See <https://developer.github.com/v3/#user-agent-required>
73 var user_agent
: String = "nit_github_api" is optional
75 # Headers to use on all requests
76 fun new_headers
: HeaderMap do
77 var map
= new HeaderMap
80 map
["Authorization"] = "token {auth}"
82 map
["User-Agent"] = user_agent
83 # FIXME remove when projects and team are no more in beta
84 map
["Accept"] = "application/vnd.github.inertia-preview+json"
85 map
["Accept"] = "application/vnd.github.hellcat-preview+json"
89 # Github API base url.
91 # Default is `https://api.github.com` and should not be changed.
92 var api_url
= "https://api.github.com"
96 # * `0`: only errors (default)
98 var verbose_lvl
= 0 is public
writable
100 # Send a HTTPRequest to the Github API
101 fun send
(method
, path
: String, headers
: nullable HeaderMap, body
: nullable String): nullable String do
103 path
= sanitize_uri
(path
)
104 var uri
= "{api_url}{path}"
105 var request
= new CurlHTTPRequest(uri
)
106 request
.method
= method
107 request
.user_agent
= user_agent
108 request
.headers
= headers
or else self.new_headers
110 return check_response
(uri
, request
.execute
)
113 private fun check_response
(uri
: String, response
: CurlResponse): nullable String do
114 if response
isa CurlResponseSuccess then
116 return response
.body_str
117 else if response
isa CurlResponseFailed then
118 last_error
= new GithubAPIError(
128 # Deserialize an object
129 fun deserialize
(string
: nullable Serializable): nullable Object do
130 if string
== null then return null
131 var deserializer
= new GithubDeserializer(string
.to_s
)
132 var res
= deserializer
.deserialize
133 if deserializer
.errors
.not_empty
then
135 last_error
= new GithubDeserializerErrors("Deserialization failed", deserializer
.errors
)
137 else if res
isa GithubError then
146 # Display a message depending on `verbose_lvl`.
147 fun message
(lvl
: Int, message
: String) do
148 if lvl
<= verbose_lvl
then print message
151 # Escape `uri` in an acceptable format for Github.
152 private fun sanitize_uri
(uri
: String): String do
153 # TODO better URI escape.
154 return uri
.replace
(" ", "%20")
157 # Last error occured during Github API communications.
158 var last_error
: nullable Error = null is public
writable
160 # Does the last request provoqued an error?
161 var was_error
= false is protected writable
163 # Execute a GET request on Github API.
165 # This method returns a deserialized result.
167 # For raw data see `send`.
170 # var api = new GithubAPI(get_github_oauth)
171 # var obj = api.get("/repos/nitlang/nit")
172 # assert obj isa Repo
173 # assert obj.name == "nit"
176 # Returns `null` in case of `error`.
179 # obj = api.get("/foo/bar/baz")
181 # assert api.was_error
182 # assert api.last_error isa GithubError
184 fun get
(path
: String, headers
: nullable HeaderMap, data
: nullable String): nullable Object do
185 return deserialize
(send
("GET", path
, headers
, data
))
188 # Get the Github logged user from `auth` token.
190 # Loads the `User` from the API or returns `null` if the user cannot be found.
193 # var api = new GithubAPI(get_github_oauth)
194 # var user = api.get_auth_user
195 # assert user.login == "Morriar"
197 fun get_auth_user
: nullable User do
198 return get
("/user").as(nullable User)
201 # Get the Github user with `login`
203 # Loads the `User` from the API or returns `null` if the user cannot be found.
206 # var api = new GithubAPI(get_github_oauth)
207 # var user = api.get_user("Morriar")
208 # print user or else "null"
209 # assert user.login == "Morriar"
211 fun get_user
(login
: String): nullable User do
212 return get
("/users/{login}").as(nullable User)
215 # Get the Github repo with `full_name`.
217 # Loads the `Repo` from the API or returns `null` if the repo cannot be found.
220 # var api = new GithubAPI(get_github_oauth)
221 # var repo = api.get_repo("nitlang/nit")
222 # assert repo.name == "nit"
223 # assert repo.owner.login == "nitlang"
224 # assert repo.default_branch == "master"
226 fun get_repo
(repo_slug
: String): nullable Repo do
227 return get
("/repos/{repo_slug}").as(nullable Repo)
230 # List of repo branches.
233 # * `page`: page to fetch (default: 1)
234 # * `per_page`: number of branches by page (default: 30)
235 fun get_repo_branches
(repo_slug
: String, page
, per_page
: nullable Int): Array[Branch] do
236 return new GithubArray[Branch].from
(get
(
237 "/repos/{repo_slug}/branches?{pagination(page, per_page)}"))
240 # List of issues associated with their ids.
241 fun get_repo_issues
(repo_slug
: String, page
, per_page
: nullable Int): Array[Issue] do
242 return new GithubArray[Issue].from
(get
(
243 "/repos/{repo_slug}/issues?{pagination(page, per_page)}"))
246 # Search issues in this repo form an advanced query.
251 # var issues = repo.search_issues("is:open label:need_review")
254 # See <https://developer.github.com/v3/search/#search-issues>.
255 fun search_repo_issues
(repo_slug
: String, query
: String, page
, per_page
: nullable Int): nullable SearchResults do
256 return get
("/search/issues?q={query} repo:{repo_slug}&{pagination(page, per_page)}").as(nullable SearchResults)
259 # List of labels associated with their names.
260 fun get_repo_labels
(repo_slug
: String, page
, per_page
: nullable Int): Array[Label] do
261 return new GithubArray[Label].from
(get
(
262 "/repos/{repo_slug}/labels?{pagination(page, per_page)}"))
265 # List of milestones associated with their ids.
266 fun get_repo_milestones
(repo_slug
: String, page
, per_page
: nullable Int): Array[Milestone] do
267 return new GithubArray[Milestone].from
(get
(
268 "/repos/{repo_slug}/milestones?{pagination(page, per_page)}"))
271 # List of pull-requests associated with their ids.
273 # Implementation notes: because PR numbers are not consecutive,
274 # PR are loaded from pages.
275 # See: https://developer.github.com/v3/pulls/#list-pull-requests
276 fun get_repo_pulls
(repo_slug
: String, page
, per_page
: nullable Int): Array[PullRequest] do
277 return new GithubArray[PullRequest].from
(get
(
278 "/repos/{repo_slug}/pulls?{pagination(page, per_page)}"))
281 # List of contributor related statistics.
282 fun get_repo_contrib_stats
(repo_slug
: String): Array[ContributorStats] do
283 return new GithubArray[ContributorStats].from
(get
("/repos/{repo_slug}/stats/contributors"))
286 # Get the Github branch with `name`.
288 # Returns `null` if the branch cannot be found.
291 # var api = new GithubAPI(get_github_oauth)
292 # var repo = api.get_repo("nitlang/nit")
293 # assert repo != null
294 # var branch = api.get_branch(repo, "master")
295 # assert branch.name == "master"
296 # assert branch.commit isa Commit
298 fun get_branch
(repo_slug
: String, name
: String): nullable Branch do
299 return get
("/repos/{repo_slug}/branches/{name}").as(nullable Branch)
302 # Get the Github commit with `sha`.
304 # Returns `null` if the commit cannot be found.
307 # var api = new GithubAPI(get_github_oauth)
308 # var repo = api.get_repo("nitlang/nit")
309 # assert repo != null
310 # var commit = api.get_commit(repo, "64ce1f")
311 # assert commit isa Commit
313 fun get_commit
(repo_slug
: String, sha
: String): nullable Commit do
314 return get
("/repos/{repo_slug}/commits/{sha}").as(nullable Commit)
317 # Get the status of a commit
319 # The status holds the result of each check ran on a commit like CI, reviews etc.
320 fun get_commit_status
(repo_slug
: String, sha
: String): nullable CommitStatus do
321 return get
("/repos/{repo_slug}/commits/{sha}/status").as(nullable CommitStatus)
324 # Get the Github issue #`number`.
326 # Returns `null` if the issue cannot be found.
329 # var api = new GithubAPI(get_github_oauth)
330 # var repo = api.get_repo("nitlang/nit")
331 # assert repo != null
332 # var issue = api.get_issue(repo, 1)
333 # assert issue.title == "Doc"
335 fun get_issue
(repo_slug
: String, number
: Int): nullable Issue do
336 return get
("/repos/{repo_slug}/issues/{number}").as(nullable Issue)
339 # List of event on this issue.
340 fun get_issue_comments
(repo_slug
: String, issue_number
: Int, page
, per_page
: nullable Int): Array[IssueComment] do
341 return new GithubArray[IssueComment].from
(get
(
342 "/repos/{repo_slug}/issues/{issue_number}/comments?{pagination(page, per_page)}"))
345 # List of events on this issue.
346 fun get_issue_events
(repo_slug
: String, issue_number
: Int, page
, per_page
: nullable Int): Array[IssueEvent] do
347 return new GithubArray[IssueEvent].from
(get
(
348 "/repos/{repo_slug}/issues/{issue_number}/events?{pagination(page, per_page)}"))
351 # Get the Github pull request #`number`.
353 # Returns `null` if the pull request cannot be found.
356 # var api = new GithubAPI(get_github_oauth)
357 # var repo = api.get_repo("nitlang/nit")
358 # assert repo != null
359 # var pull = api.get_pull(repo, 1)
360 # assert pull.title == "Doc"
361 # assert pull.user.login == "Morriar"
363 fun get_pull
(repo_slug
: String, number
: Int): nullable PullRequest do
364 return get
("/repos/{repo_slug}/pulls/{number}").as(nullable PullRequest)
367 # List of comments on a pull request
368 fun get_pull_comments
(repo_slug
: String, pull_number
: Int, page
, per_page
: nullable Int): Array[PullComment] do
369 return new GithubArray[PullComment].from
(get
(
370 "/repos/{repo_slug}/pulls/{pull_number}/comments?{pagination(page, per_page)}"))
373 # Get a specific pull request comment
374 fun get_pull_comment
(repo_slug
: String, id
: Int): nullable PullComment do
375 return get
("/repos/{repo_slug}/pulls/comments/{id}").as(nullable PullComment)
378 # Get the Github label with `name`.
380 # Returns `null` if the label cannot be found.
383 # var api = new GithubAPI(get_github_oauth)
384 # var repo = api.get_repo("nitlang/nit")
385 # assert repo != null
386 # var labl = api.get_label(repo, "ok_will_merge")
387 # assert labl != null
389 fun get_label
(repo_slug
: String, name
: String): nullable Label do
390 return get
("/repos/{repo_slug}/labels/{name}").as(nullable Label)
393 # Get the Github milestone with `id`.
395 # Returns `null` if the milestone cannot be found.
398 # var api = new GithubAPI(get_github_oauth)
399 # var repo = api.get_repo("nitlang/nit")
400 # assert repo != null
401 # var stone = api.get_milestone(repo, 4)
402 # assert stone.title == "v1.0prealpha"
404 fun get_milestone
(repo_slug
: String, id
: Int): nullable Milestone do
405 return get
("/repos/{repo_slug}/milestones/{id}").as(nullable Milestone)
408 # Get the Github issue event with `id`.
410 # Returns `null` if the event cannot be found.
413 # var api = new GithubAPI(get_github_oauth)
414 # var repo = api.get_repo("nitlang/nit")
415 # assert repo isa Repo
416 # var event = api.get_issue_event(repo, 199674194)
417 # assert event isa IssueEvent
418 # assert event.actor.login == "privat"
419 # assert event.event == "labeled"
420 # assert event.labl isa Label
421 # assert event.labl.name == "need_review"
423 fun get_issue_event
(repo_slug
: String, id
: Int): nullable IssueEvent do
424 return get
("/repos/{repo_slug}/issues/events/{id}").as(nullable IssueEvent)
427 # Get the Github commit comment with `id`.
429 # Returns `null` if the comment cannot be found.
432 # var api = new GithubAPI(get_github_oauth)
433 # var repo = api.get_repo("nitlang/nit")
434 # assert repo != null
435 # var comment = api.get_commit_comment(repo, 8982707)
436 # assert comment.user.login == "Morriar"
437 # assert comment.body == "For testing purposes...\n"
438 # assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
440 fun get_commit_comment
(repo_slug
: String, id
: Int): nullable CommitComment do
441 return get
("/repos/{repo_slug}/comments/{id}").as(nullable CommitComment)
444 # Get the Github issue comment with `id`.
446 # Returns `null` if the comment cannot be found.
449 # var api = new GithubAPI(get_github_oauth)
450 # var repo = api.get_repo("nitlang/nit")
451 # assert repo != null
452 # var comment = api.get_issue_comment(repo, 6020149)
453 # assert comment.user.login == "privat"
454 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
455 # assert comment.issue_number == 10
457 fun get_issue_comment
(repo_slug
: String, id
: Int): nullable IssueComment do
458 return get
("/repos/{repo_slug}/issues/comments/{id}").as(nullable IssueComment)
461 private fun pagination
(page
, per_page
: nullable Int): String do
462 return "page={page or else 1}&per_page={per_page or else 30}"
466 # Return deserialization as an array of E
468 # Non-subtypes will be ignored.
469 private class GithubArray[E
]
472 # Create `self` from an Array of objects
474 # Objects non-subtyping E will be ignored.
475 init from
(res
: nullable Object) do
476 if not res
isa Array[Object] then return
478 if obj
isa E
then add obj
483 # An Error returned by GithubAPI
488 # An Error returned by https://api.github.com
490 # Anything that can occurs when sending request to the API:
491 # * Can't connect to API
492 # * Ressource not found
498 # Status code obtained
501 # URI that returned the error
502 var requested_uri
: String
505 # An Error returned while deserializing objects from the API
506 class GithubDeserializerErrors
509 # Errors returned by the deserizalization process
510 var deserizalization_errors
: Array[Error]
515 # Provides access to [Github user data](https://developer.github.com/v3/users/).
516 # Should be accessed from `GithubAPI::get_user`.
522 var login
: String is writable
524 # Avatar image url for this user.
525 var avatar_url
: nullable String is writable
527 # User public name if any.
528 var name
: nullable String is writable
530 # User public email if any.
531 var email
: nullable String is writable
533 # User public blog if any.
534 var blog
: nullable String is writable
537 # A Github repository.
539 # Provides access to [Github repo data](https://developer.github.com/v3/repos/).
540 # Should be accessed from `GithubAPI::get_repo`.
544 # Repo full name on Github.
545 var full_name
: String is writable
547 # Repo short name on Github.
548 var name
: String is writable
550 # Get the repo owner.
551 var owner
: User is writable
553 # Repo default branch name.
554 var default_branch
: nullable String = null is optional
, writable
559 # Should be accessed from `GithubAPI::get_branch`.
561 # See <https://developer.github.com/v3/repos/#list-branches>.
566 var name
: String is writable
568 # Get the last commit of `self`.
569 var commit
: Commit is writable
574 # Should be accessed from `GithubAPI::get_commit`.
576 # See <https://developer.github.com/v3/repos/commits/>.
581 var sha
: String is writable
583 # Parent commits of `self`.
584 var parents
: nullable Array[Commit] = null is writable
586 # Author of the commit.
587 var author
: nullable GitUser is writable
589 # Committer of the commit.
590 var committer
: nullable GitUser is writable
592 # Authoring date as String.
593 var author_date
: nullable String is writable
595 # Commit date as String.
596 var commit_date
: nullable String is writable
598 # List files staged in this commit.
599 var files
: nullable Array[GithubFile] = null is optional
, writable
602 var message
: nullable String is writable
604 # Git commit representation linked to this commit.
605 var commit
: nullable GitCommit
608 # A Git Commit representation
613 var sha
: nullable String is writable
615 # Parent commits of `self`.
616 var parents
: nullable Array[GitCommit] = null is writable
618 # Author of the commit.
619 var author
: nullable GitUser is writable
621 # Committer of the commit.
622 var committer
: nullable GitUser is writable
625 var message
: nullable String is writable
628 # Git user authoring data
633 var date
: nullable String = null is writable
638 # Should be accessed from `GithubAPI::get_issue`.
640 # See <https://developer.github.com/v3/issues/>.
645 var number
: Int is writable
648 var id
: nullable Int is writable
651 var title
: String is writable
653 # User that created this issue.
654 var user
: nullable User is writable
656 # List of labels on this issue associated to their names.
657 var labels
: nullable Array[Label] is writable
659 # State of the issue on Github.
660 var state
: String is writable
662 # Is the issue locked?
663 var locked
: nullable Bool is writable
665 # Assigned `User` (if any).
666 var assignee
: nullable User is writable
668 # `Milestone` (if any).
669 var milestone
: nullable Milestone is writable
671 # Number of comments on this issue.
672 var comments
: nullable Int is writable
674 # Creation time as String.
675 var created_at
: String is writable
677 # Last update time as String (if any).
678 var updated_at
: nullable String is writable
680 # Close time as String (if any).
681 var closed_at
: nullable String is writable
683 # Full description of the issue.
684 var body
: nullable String is writable
686 # User that closed this issue (if any).
687 var closed_by
: nullable User is writable
689 # Is this issue linked to a pull request?
690 var is_pull_request
: Bool = false is writable
693 # A Github pull request.
695 # Should be accessed from `GithubAPI::get_pull`.
697 # PullRequest are basically Issues with more data.
698 # See <https://developer.github.com/v3/pulls/>.
703 # Merge time as String (if any).
704 var merged_at
: nullable String is writable
707 var merge_commit_sha
: nullable String is writable
709 # Count of comments made on the pull request diff.
710 var review_comments
: nullable Int is writable
712 # Pull request head (can be a commit SHA or a branch name).
713 var head
: PullRef is writable
715 # Pull request base (can be a commit SHA or a branch name).
716 var base
: PullRef is writable
718 # Is this pull request merged?
719 var merged
: nullable Bool is writable
721 # Is this pull request mergeable?
722 var mergeable
: nullable Bool is writable
724 # Mergeable state of this pull request.
726 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
727 var mergeable_state
: nullable String is writable
729 # User that merged this pull request (if any).
730 var merged_by
: nullable User is writable
732 # Count of commits in this pull request.
733 var commits
: nullable Int is writable
736 var additions
: nullable Int is writable
738 # Deleted line count.
739 var deletions
: nullable Int is writable
741 # Changed files count.
742 var changed_files
: nullable Int is writable
745 var patch_url
: nullable String is writable
748 # A pull request reference (used for head and base).
752 # Label pointed by `self`.
753 var labl
: String is writable, serialize_as
("label")
755 # Reference pointed by `self`.
756 var ref
: String is writable
758 # Commit SHA pointed by `self`.
759 var sha
: String is writable
761 # User pointed by `self`.
762 var user
: User is writable
764 # Repo pointed by `self` (if any).
766 # A `null` value means the `repo` was deleted.
767 var repo
: nullable Repo is writable
772 # Should be accessed from `GithubAPI::get_label`.
774 # See <https://developer.github.com/v3/issues/labels/>.
779 var name
: String is writable
782 var color
: String is writable
785 # A Github milestone.
787 # Should be accessed from `GithubAPI::get_milestone`.
789 # See <https://developer.github.com/v3/issues/milestones/>.
793 # The milestone id on Github.
794 var number
: nullable Int = null is writable
797 var title
: String is writable
799 # Milestone long description.
800 var description
: nullable String is writable
802 # Count of opened issues linked to this milestone.
803 var open_issues
: nullable Int = null is writable
805 # Count of closed issues linked to this milestone.
806 var closed_issues
: nullable Int = null is writable
809 var state
: nullable String is writable
811 # Creation time as String.
812 var created_at
: nullable String is writable
814 # User that created this milestone.
815 var creator
: nullable User is writable
817 # Due time as String (if any).
818 var due_on
: nullable String is writable
820 # Last update time as String (if any).
821 var updated_at
: nullable String is writable
823 # Close time as String (if any).
824 var closed_at
: nullable String is writable
829 # There is two kinds of comments:
831 # * `CommitComment` are made on a commit page.
832 # * `IssueComment` are made on an issue or pull request page.
833 # * `PullComment` are made on the diff associated to a pull request.
834 abstract class Comment
837 # Identifier of this comment.
838 var id
: Int is writable
840 # User that made this comment.
841 var user
: User is writable
843 # Creation time as String.
844 var created_at
: String is writable
846 # Last update time as String (if any).
847 var updated_at
: nullable String is writable
850 var body
: String is writable
852 # Does the comment contain an acknowledgement (+1)
854 return body
.has
("\\+1\\b".to_re
) or body
.has
(":+1:") or body
.has
(":shipit:")
858 # A comment made on a commit.
864 var commit_id
: String is writable
866 # Position of the comment on the line.
867 var position
: nullable Int is writable
869 # Line of the comment.
870 var line
: nullable Int is writable
872 # Path of the commented file.
873 var path
: nullable String is writable
878 # Can contain sub-status for reviews, CI etc.
882 # Global state of this commit
883 var state
: nullable String = null is optional
, writable
885 # Sha of the commit this status is for
886 var sha
: nullable String = null is optional
, writable
888 # Repository the commit belongs to
889 var repository
: nullable Repo = null is optional
, writable
891 # All sub statuses (one for each check)
892 var statuses
= new Array[RepoStatus] is optional
, writable
894 # Total count of sub statuses
895 var total_count
: nullable Int = null is optional
, writable
898 # Sub status of a CommitStatus
900 # Represents a check applied to a commit (reviews, CI, ...).
904 # State of this check
905 var state
: nullable String = null is optional
, writable
907 # Description of this check
908 var description
: nullable String = null is optional
, writable
911 var target_url
: nullable String = null is optional
, writable
913 # Context this status is related to
915 # Used to hold the name of the check applied.
916 var context
: nullable String = null is optional
, writable
918 # Date when this status was created
919 var created_at
: nullable String = null is optional
, writable
921 # Last date this status was updated
922 var updated_at
: nullable String = null is optional
, writable
925 # Comments made on Github issue and pull request pages.
927 # Should be accessed from `GithubAPI::get_issue_comment`.
929 # See <https://developer.github.com/v3/issues/comments/>.
934 # Issue that contains `self`.
935 fun issue_number
: Int do return issue_url
.split
("/").last
.to_i
937 # Link to the issue document on API.
938 var issue_url
: String is writable
941 # Comments made on Github pull request diffs.
943 # Should be accessed from `GithubAPI::get_diff_comment`.
945 # See <https://developer.github.com/v3/pulls/comments/>.
950 # Pull request that contains `self`.
951 fun pull_number
: Int do return pull_request_url
.split
("/").last
.to_i
953 # Link to the pull request on API.
954 var pull_request_url
: String is writable
957 var diff_hunk
: String is writable
959 # Path of commented file.
960 var path
: String is writable
962 # Position of the comment on the file.
963 var position
: nullable Int is writable
965 # Original position in the diff.
966 var original_position
: Int is writable
968 # Commit referenced by this comment.
969 var commit_id
: String is writable
971 # Original commit id.
972 var original_commit_id
: String is writable
975 # An event that occurs on a Github `Issue`.
977 # Should be accessed from `GithubAPI::get_issue_event`.
979 # See <https://developer.github.com/v3/issues/events/>.
983 # Event id on Github.
984 var id
: Int is writable
986 # User that initiated the event.
987 var actor
: User is writable
989 # Creation time as String.
990 var created_at
: String is writable
993 var event
: String is writable
995 # Commit linked to this event (if any).
996 var commit_id
: nullable String is writable
998 # Label linked to this event (if any).
999 var labl
: nullable Label is writable, serialize_as
("label")
1001 # User linked to this event (if any).
1002 var assignee
: nullable User is writable
1004 # Milestone linked to this event (if any).
1005 var milestone
: nullable Milestone is writable
1007 # Rename linked to this event (if any).
1008 var rename
: nullable RenameAction is writable
1011 # A rename action maintains the name before and after a renaming action.
1015 # Name before renaming.
1016 var from
: String is writable
1018 # Name after renaming.
1019 var to
: String is writable
1023 # Should be accessed from `Repo::contrib_stats`.
1025 # See <https://developer.github.com/v3/repos/statistics/>.
1026 class ContributorStats
1030 redef type OTHER: ContributorStats
1032 # User these statistics are about.
1033 var author
: User is writable
1035 # Total number of commit.
1036 var total
: Int is writable
1038 # Array of weeks of activity with detailed statistics.
1039 var weeks
: Array[ContributorWeek] is writable
1041 # ContributorStats can be compared on the total amount of commits.
1042 redef fun <(o
) do return total
< o
.total
1045 # Contributor stats weekly hash
1046 class ContributorWeek
1049 # Start of week given a Unix timestamp
1052 # Number of additions
1055 # Number of deletions
1062 # A Github file representation.
1064 # Mostly a wrapper around a json object.
1069 var filename
: String is writable
1072 # A list of results returned buy `/search`
1076 # Total count with other pages
1077 var total_count
: Int
1079 # Does this page contain all the results?
1080 var incomplete_results
: Bool
1082 # Results in this page
1083 var items
: Array[Object]
1086 # JsonDeserializer specific for Github objects.
1087 class GithubDeserializer
1088 super JsonDeserializer
1090 private var pattern_base
= "https://api.github.com"
1092 # Url patterns to class names
1093 var url_patterns
: Map[Regex, String] is lazy
do
1094 var map
= new HashMap[Regex, String]
1095 map
["{pattern_base}/users/[^/]*$".to_re
] = "User"
1096 map
["{pattern_base}/repos/[^/]*/[^/]*$".to_re
] = "Repo"
1097 map
["{pattern_base}/repos/[^/]*/[^/]*/labels/[^/]+$".to_re
] = "Label"
1098 map
["{pattern_base}/repos/[^/]*/[^/]*/milestones/[0-9]+$".to_re
] = "Milestone"
1099 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/[0-9]+$".to_re
] = "Issue"
1100 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/comments/[0-9]+$".to_re
] = "IssueComment"
1101 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/events/[0-9]+$".to_re
] = "IssueEvent"
1102 map
["{pattern_base}/repos/[^/]*/[^/]*/pulls/[0-9]+$".to_re
] = "PullRequest"
1103 map
["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re
] = "PullComment"
1104 map
["{pattern_base}/repos/[^/]*/[^/]*/comments/[0-9]+$".to_re
] = "CommitComment"
1105 map
["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+$".to_re
] = "Commit"
1106 map
["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+/status$".to_re
] = "CommitStatus"
1107 map
["{pattern_base}/repos/[^/]*/[^/]*/statuses/[a-f0-9]+$".to_re
] = "RepoStatus"
1111 # Match `url` property in object to a class name
1112 fun url_heuristic
(raw
: Map[String, nullable Object]): nullable String do
1113 if not raw
.has_key
("url") then return null
1115 var url
= raw
["url"].as(String)
1116 for re
, class_name
in url_patterns
do
1117 if url
.has
(re
) then return class_name
1122 redef fun class_name_heuristic
(raw
) do
1124 var class_name
= url_heuristic
(raw
)
1125 if class_name
!= null then return class_name
1127 # print raw.serialize_to_json(true, true) # debug
1129 # Use properties heuristics
1130 if raw
.has_key
("name") and raw
.has_key
("commit") then
1132 else if raw
.has_key
("total_count") and raw
.has_key
("items") then
1133 return "SearchResults"
1134 else if raw
.has_key
("total") and raw
.has_key
("weeks") then
1135 return "ContributorStats"
1136 else if raw
.has_key
("a") and raw
.has_key
("d") and raw
.has_key
("c") then
1137 return "ContributorWeek"
1142 redef fun deserialize_class
(name
) do
1143 if name
== "Issue" then
1144 var issue
= super.as(Issue)
1145 if path
.last
.has_key
("pull_request") then
1146 issue
.is_pull_request
= true
1149 else if name
== "Commit" then
1150 var commit
= super.as(Commit)
1151 var git_commit
= commit
.commit
1152 if git_commit
!= null then commit
.message
= git_commit
.message
1159 # Gets the Github token from `git` configuration
1161 # Return the value of `git config --get github.oauthtoken`
1162 # or `""` if no key exists.
1163 fun get_github_oauth
: String
1165 var p
= new ProcessReader("git", "config", "--get", "github.oauthtoken")
1166 var token
= p
.read_line