cc0bf6b58d63e20025c67cc91d03c9fc1b480765
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
29 # Client to Github API
31 # To access the API you need an instance of a `GithubAPI` client.
34 # # Get Github authentification token.
35 # var token = get_github_oauth
36 # assert not token.is_empty
39 # var api = new GithubAPI(token)
42 # The API client allows you to get Github API entities.
45 # var repo = api.get_repo("nitlang/nit")
47 # assert repo.name == "nit"
49 # var user = api.get_user("Morriar")
51 # assert user.login == "Morriar"
55 # Github API OAuth token
57 # To access your private ressources, you must
58 # [authenticate](https://developer.github.com/guides/basics-of-authentication/).
60 # For client applications, Github recommands to use the
61 # [OAuth tokens](https://developer.github.com/v3/oauth/) authentification method.
65 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
66 # associated to the key.
67 var auth
: nullable String = null is optional
69 # User agent used for HTTP requests.
71 # Default is `nit_github_api`.
73 # See <https://developer.github.com/v3/#user-agent-required>
74 var user_agent
: String = "nit_github_api" is optional
76 # Headers to use on all requests
77 fun new_headers
: HeaderMap do
78 var map
= new HeaderMap
81 map
["Authorization"] = "token {auth}"
83 map
["User-Agent"] = user_agent
84 # FIXME remove when projects and team are no more in beta
85 map
["Accept"] = "application/vnd.github.inertia-preview+json"
86 map
["Accept"] = "application/vnd.github.hellcat-preview+json"
90 # Github API base url.
92 # Default is `https://api.github.com` and should not be changed.
93 var api_url
= "https://api.github.com"
97 # * `0`: only errors (default)
99 var verbose_lvl
= 0 is public
writable
101 # Send a HTTPRequest to the Github API
102 fun send
(method
, path
: String, headers
: nullable HeaderMap, body
: nullable String): nullable String do
104 path
= sanitize_uri
(path
)
105 var uri
= "{api_url}{path}"
106 var request
= new CurlHTTPRequest(uri
)
107 request
.method
= method
108 request
.user_agent
= user_agent
109 request
.headers
= headers
or else self.new_headers
111 return check_response
(uri
, request
.execute
)
114 private fun check_response
(uri
: String, response
: CurlResponse): nullable String do
115 if response
isa CurlResponseSuccess then
117 return response
.body_str
118 else if response
isa CurlResponseFailed then
119 last_error
= new GithubAPIError(
129 # Deserialize an object
130 fun deserialize
(string
: nullable Serializable): nullable Object do
131 if string
== null then return null
132 var deserializer
= new GithubDeserializer(string
.to_s
)
133 var res
= deserializer
.deserialize
134 if deserializer
.errors
.not_empty
then
136 last_error
= new GithubDeserializerErrors("Deserialization failed", deserializer
.errors
)
138 else if res
isa GithubError then
147 # Display a message depending on `verbose_lvl`.
148 fun message
(lvl
: Int, message
: String) do
149 if lvl
<= verbose_lvl
then print message
152 # Escape `uri` in an acceptable format for Github.
153 private fun sanitize_uri
(uri
: String): String do
154 # TODO better URI escape.
155 return uri
.replace
(" ", "%20")
158 # Last error occured during Github API communications.
159 var last_error
: nullable Error = null is public
writable
161 # Does the last request provoqued an error?
162 var was_error
= false is protected writable
164 # Execute a GET request on Github API.
166 # This method returns a deserialized result.
168 # For raw data see `send`.
171 # var api = new GithubAPI(get_github_oauth)
172 # var obj = api.get("/repos/nitlang/nit")
173 # assert obj isa Repo
174 # assert obj.name == "nit"
177 # Returns `null` in case of `error`.
180 # obj = api.get("/foo/bar/baz")
182 # assert api.was_error
183 # assert api.last_error isa GithubError
185 fun get
(path
: String, headers
: nullable HeaderMap, data
: nullable String): nullable Object do
186 return deserialize
(send
("GET", path
, headers
, data
))
189 # Get the Github logged user from `auth` token.
191 # Loads the `User` from the API or returns `null` if the user cannot be found.
194 # var api = new GithubAPI(get_github_oauth)
195 # var user = api.get_auth_user
196 # assert user.login == "Morriar"
198 fun get_auth_user
: nullable User do
199 return get
("/user").as(nullable User)
202 # Get the Github user with `login`
204 # Loads the `User` from the API or returns `null` if the user cannot be found.
207 # var api = new GithubAPI(get_github_oauth)
208 # var user = api.get_user("Morriar")
209 # print user or else "null"
210 # assert user.login == "Morriar"
212 fun get_user
(login
: String): nullable User do
213 return get
("/users/{login}").as(nullable User)
216 # Get the Github repo with `full_name`.
218 # Loads the `Repo` from the API or returns `null` if the repo cannot be found.
221 # var api = new GithubAPI(get_github_oauth)
222 # var repo = api.get_repo("nitlang/nit")
223 # assert repo.name == "nit"
224 # assert repo.owner.login == "nitlang"
225 # assert repo.default_branch == "master"
227 fun get_repo
(repo_slug
: String): nullable Repo do
228 return get
("/repos/{repo_slug}").as(nullable Repo)
231 # List of repo branches.
234 # * `page`: page to fetch (default: 1)
235 # * `per_page`: number of branches by page (default: 30)
236 fun get_repo_branches
(repo_slug
: String, page
, per_page
: nullable Int): Array[Branch] do
237 return new GithubArray[Branch].from
(get
(
238 "/repos/{repo_slug}/branches?{pagination(page, per_page)}"))
241 # List of issues associated with their ids.
242 fun get_repo_issues
(repo_slug
: String, page
, per_page
: nullable Int): Array[Issue] do
243 return new GithubArray[Issue].from
(get
(
244 "/repos/{repo_slug}/issues?{pagination(page, per_page)}"))
247 # Search issues in this repo form an advanced query.
252 # var issues = repo.search_issues("is:open label:need_review")
255 # See <https://developer.github.com/v3/search/#search-issues>.
256 fun search_repo_issues
(repo_slug
: String, query
: String, page
, per_page
: nullable Int): nullable SearchResults do
257 return get
("/search/issues?q={query} repo:{repo_slug}&{pagination(page, per_page)}").as(nullable SearchResults)
260 # List of labels associated with their names.
261 fun get_repo_labels
(repo_slug
: String, page
, per_page
: nullable Int): Array[Label] do
262 return new GithubArray[Label].from
(get
(
263 "/repos/{repo_slug}/labels?{pagination(page, per_page)}"))
266 # List of milestones associated with their ids.
267 fun get_repo_milestones
(repo_slug
: String, page
, per_page
: nullable Int): Array[Milestone] do
268 return new GithubArray[Milestone].from
(get
(
269 "/repos/{repo_slug}/milestones?{pagination(page, per_page)}"))
272 # List of pull-requests associated with their ids.
274 # Implementation notes: because PR numbers are not consecutive,
275 # PR are loaded from pages.
276 # See: https://developer.github.com/v3/pulls/#list-pull-requests
277 fun get_repo_pulls
(repo_slug
: String, page
, per_page
: nullable Int): Array[PullRequest] do
278 return new GithubArray[PullRequest].from
(get
(
279 "/repos/{repo_slug}/pulls?{pagination(page, per_page)}"))
282 # List of contributor related statistics.
283 fun get_repo_contrib_stats
(repo_slug
: String): Array[ContributorStats] do
284 message
(1, "Get contributor stats for {repo_slug}")
285 var res
= new Array[ContributorStats]
286 var array
= get
("/repos/{repo_slug}/stats/contributors")
287 if not array
isa JsonArray then return res
288 return deserialize
(array
.to_json
).as(Array[ContributorStats])
291 # Get the Github branch with `name`.
293 # Returns `null` if the branch cannot be found.
296 # var api = new GithubAPI(get_github_oauth)
297 # var repo = api.get_repo("nitlang/nit")
298 # assert repo != null
299 # var branch = api.get_branch(repo, "master")
300 # assert branch.name == "master"
301 # assert branch.commit isa Commit
303 fun get_branch
(repo_slug
: String, name
: String): nullable Branch do
304 return get
("/repos/{repo_slug}/branches/{name}").as(nullable Branch)
307 # Get the Github commit with `sha`.
309 # Returns `null` if the commit cannot be found.
312 # var api = new GithubAPI(get_github_oauth)
313 # var repo = api.get_repo("nitlang/nit")
314 # assert repo != null
315 # var commit = api.get_commit(repo, "64ce1f")
316 # assert commit isa Commit
318 fun get_commit
(repo_slug
: String, sha
: String): nullable Commit do
319 return get
("/repos/{repo_slug}/commits/{sha}").as(nullable Commit)
322 # Get the status of a commit
324 # The status holds the result of each check ran on a commit like CI, reviews etc.
325 fun get_commit_status
(repo_slug
: String, sha
: String): nullable CommitStatus do
326 return get
("/repos/{repo_slug}/commits/{sha}/status").as(nullable CommitStatus)
329 # Get the Github issue #`number`.
331 # Returns `null` if the issue cannot be found.
334 # var api = new GithubAPI(get_github_oauth)
335 # var repo = api.get_repo("nitlang/nit")
336 # assert repo != null
337 # var issue = api.get_issue(repo, 1)
338 # assert issue.title == "Doc"
340 fun get_issue
(repo_slug
: String, number
: Int): nullable Issue do
341 return get
("/repos/{repo_slug}/issues/{number}").as(nullable Issue)
344 # List of event on this issue.
345 fun get_issue_comments
(repo_slug
: String, issue_number
: Int, page
, per_page
: nullable Int): Array[IssueComment] do
346 return new GithubArray[IssueComment].from
(get
(
347 "/repos/{repo_slug}/issues/{issue_number}/comments?{pagination(page, per_page)}"))
350 # List of events on this issue.
351 fun get_issue_events
(repo_slug
: String, issue_number
: Int, page
, per_page
: nullable Int): Array[IssueEvent] do
352 return new GithubArray[IssueEvent].from
(get
(
353 "/repos/{repo_slug}/issues/{issue_number}/events?{pagination(page, per_page)}"))
356 # Get the Github pull request #`number`.
358 # Returns `null` if the pull request cannot be found.
361 # var api = new GithubAPI(get_github_oauth)
362 # var repo = api.get_repo("nitlang/nit")
363 # assert repo != null
364 # var pull = api.get_pull(repo, 1)
365 # assert pull.title == "Doc"
366 # assert pull.user.login == "Morriar"
368 fun get_pull
(repo_slug
: String, number
: Int): nullable PullRequest do
369 return get
("/repos/{repo_slug}/pulls/{number}").as(nullable PullRequest)
372 # List of comments on a pull request
373 fun get_pull_comments
(repo_slug
: String, pull_number
: Int, page
, per_page
: nullable Int): Array[PullComment] do
374 return new GithubArray[PullComment].from
(get
(
375 "/repos/{repo_slug}/pulls/{pull_number}/comments?{pagination(page, per_page)}"))
378 # Get a specific pull request comment
379 fun get_pull_comment
(repo_slug
: String, id
: Int): nullable PullComment do
380 return get
("/repos/{repo_slug}/pulls/comments/{id}").as(nullable PullComment)
383 # Get the Github label with `name`.
385 # Returns `null` if the label cannot be found.
388 # var api = new GithubAPI(get_github_oauth)
389 # var repo = api.get_repo("nitlang/nit")
390 # assert repo != null
391 # var labl = api.get_label(repo, "ok_will_merge")
392 # assert labl != null
394 fun get_label
(repo_slug
: String, name
: String): nullable Label do
395 return get
("/repos/{repo_slug}/labels/{name}").as(nullable Label)
398 # Get the Github milestone with `id`.
400 # Returns `null` if the milestone cannot be found.
403 # var api = new GithubAPI(get_github_oauth)
404 # var repo = api.get_repo("nitlang/nit")
405 # assert repo != null
406 # var stone = api.get_milestone(repo, 4)
407 # assert stone.title == "v1.0prealpha"
409 fun get_milestone
(repo_slug
: String, id
: Int): nullable Milestone do
410 return get
("/repos/{repo_slug}/milestones/{id}").as(nullable Milestone)
413 # Get the Github issue event with `id`.
415 # Returns `null` if the event cannot be found.
418 # var api = new GithubAPI(get_github_oauth)
419 # var repo = api.get_repo("nitlang/nit")
420 # assert repo isa Repo
421 # var event = api.get_issue_event(repo, 199674194)
422 # assert event isa IssueEvent
423 # assert event.actor.login == "privat"
424 # assert event.event == "labeled"
425 # assert event.labl isa Label
426 # assert event.labl.name == "need_review"
428 fun get_issue_event
(repo_slug
: String, id
: Int): nullable IssueEvent do
429 return get
("/repos/{repo_slug}/issues/events/{id}").as(nullable IssueEvent)
432 # Get the Github commit comment with `id`.
434 # Returns `null` if the comment cannot be found.
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_commit_comment(repo, 8982707)
441 # assert comment.user.login == "Morriar"
442 # assert comment.body == "For testing purposes...\n"
443 # assert comment.commit_id == "7eacb86d1e24b7e72bc9ac869bf7182c0300ceca"
445 fun get_commit_comment
(repo_slug
: String, id
: Int): nullable CommitComment do
446 return get
("/repos/{repo_slug}/comments/{id}").as(nullable CommitComment)
449 # Get the Github issue comment with `id`.
451 # Returns `null` if the comment cannot be found.
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_issue_comment(repo, 6020149)
458 # assert comment.user.login == "privat"
459 # assert comment.created_at.to_s == "2012-05-30T20:16:54Z"
460 # assert comment.issue_number == 10
462 fun get_issue_comment
(repo_slug
: String, id
: Int): nullable IssueComment do
463 return get
("/repos/{repo_slug}/issues/comments/{id}").as(nullable IssueComment)
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}"
471 # Return deserialization as an array of E
473 # Non-subtypes will be ignored.
474 private class GithubArray[E
]
477 # Create `self` from an Array of objects
479 # Objects non-subtyping E will be ignored.
480 init from
(res
: nullable Object) do
481 if not res
isa Array[Object] then return
483 if obj
isa E
then add obj
488 # An Error returned by GithubAPI
493 # An Error returned by https://api.github.com
495 # Anything that can occurs when sending request to the API:
496 # * Can't connect to API
497 # * Ressource not found
503 # Status code obtained
506 # URI that returned the error
507 var requested_uri
: String
510 # An Error returned while deserializing objects from the API
511 class GithubDeserializerErrors
514 # Errors returned by the deserizalization process
515 var deserizalization_errors
: Array[Error]
520 # Provides access to [Github user data](https://developer.github.com/v3/users/).
521 # Should be accessed from `GithubAPI::get_user`.
527 var login
: String is writable
529 # Avatar image url for this user.
530 var avatar_url
: nullable String is writable
532 # User public name if any.
533 var name
: nullable String is writable
535 # User public email if any.
536 var email
: nullable String is writable
538 # User public blog if any.
539 var blog
: nullable String is writable
542 # A Github repository.
544 # Provides access to [Github repo data](https://developer.github.com/v3/repos/).
545 # Should be accessed from `GithubAPI::get_repo`.
549 # Repo full name on Github.
550 var full_name
: String is writable
552 # Repo short name on Github.
553 var name
: String is writable
555 # Get the repo owner.
556 var owner
: User is writable
558 # Repo default branch name.
559 var default_branch
: nullable String = null is optional
, writable
564 # Should be accessed from `GithubAPI::get_branch`.
566 # See <https://developer.github.com/v3/repos/#list-branches>.
571 var name
: String is writable
573 # Get the last commit of `self`.
574 var commit
: Commit is writable
579 # Should be accessed from `GithubAPI::get_commit`.
581 # See <https://developer.github.com/v3/repos/commits/>.
586 var sha
: String is writable
588 # Parent commits of `self`.
589 var parents
: nullable Array[Commit] = null is writable
591 # Author of the commit.
592 var author
: nullable GitUser is writable
594 # Committer of the commit.
595 var committer
: nullable GitUser is writable
597 # Authoring date as String.
598 var author_date
: nullable String is writable
600 # Commit date as String.
601 var commit_date
: nullable String is writable
603 # List files staged in this commit.
604 var files
: nullable Array[GithubFile] = null is optional
, writable
607 var message
: nullable String is writable
609 # Git commit representation linked to this commit.
610 var commit
: nullable GitCommit
613 # A Git Commit representation
618 var sha
: nullable String is writable
620 # Parent commits of `self`.
621 var parents
: nullable Array[GitCommit] = null is writable
623 # Author of the commit.
624 var author
: nullable GitUser is writable
626 # Committer of the commit.
627 var committer
: nullable GitUser is writable
630 var message
: nullable String is writable
633 # Git user authoring data
638 var date
: nullable String = null is writable
643 # Should be accessed from `GithubAPI::get_issue`.
645 # See <https://developer.github.com/v3/issues/>.
650 var number
: Int is writable
653 var id
: nullable Int is writable
656 var title
: String is writable
658 # User that created this issue.
659 var user
: nullable User is writable
661 # List of labels on this issue associated to their names.
662 var labels
: nullable Array[Label] is writable
664 # State of the issue on Github.
665 var state
: String is writable
667 # Is the issue locked?
668 var locked
: nullable Bool is writable
670 # Assigned `User` (if any).
671 var assignee
: nullable User is writable
673 # `Milestone` (if any).
674 var milestone
: nullable Milestone is writable
676 # Number of comments on this issue.
677 var comments
: nullable Int is writable
679 # Creation time as String.
680 var created_at
: String is writable
682 # Last update time as String (if any).
683 var updated_at
: nullable String is writable
685 # Close time as String (if any).
686 var closed_at
: nullable String is writable
688 # Full description of the issue.
689 var body
: nullable String is writable
691 # User that closed this issue (if any).
692 var closed_by
: nullable User is writable
694 # Is this issue linked to a pull request?
695 var is_pull_request
: Bool = false is writable
698 # A Github pull request.
700 # Should be accessed from `GithubAPI::get_pull`.
702 # PullRequest are basically Issues with more data.
703 # See <https://developer.github.com/v3/pulls/>.
708 # Merge time as String (if any).
709 var merged_at
: nullable String is writable
712 var merge_commit_sha
: nullable String is writable
714 # Count of comments made on the pull request diff.
715 var review_comments
: nullable Int is writable
717 # Pull request head (can be a commit SHA or a branch name).
718 var head
: PullRef is writable
720 # Pull request base (can be a commit SHA or a branch name).
721 var base
: PullRef is writable
723 # Is this pull request merged?
724 var merged
: nullable Bool is writable
726 # Is this pull request mergeable?
727 var mergeable
: nullable Bool is writable
729 # Mergeable state of this pull request.
731 # See <https://developer.github.com/v3/pulls/#list-pull-requests>.
732 var mergeable_state
: nullable String is writable
734 # User that merged this pull request (if any).
735 var merged_by
: nullable User is writable
737 # Count of commits in this pull request.
738 var commits
: nullable Int is writable
741 var additions
: nullable Int is writable
743 # Deleted line count.
744 var deletions
: nullable Int is writable
746 # Changed files count.
747 var changed_files
: nullable Int is writable
750 var patch_url
: nullable String is writable
753 # A pull request reference (used for head and base).
757 # Label pointed by `self`.
758 var labl
: String is writable, serialize_as
("label")
760 # Reference pointed by `self`.
761 var ref
: String is writable
763 # Commit SHA pointed by `self`.
764 var sha
: String is writable
766 # User pointed by `self`.
767 var user
: User is writable
769 # Repo pointed by `self` (if any).
771 # A `null` value means the `repo` was deleted.
772 var repo
: nullable Repo is writable
777 # Should be accessed from `GithubAPI::get_label`.
779 # See <https://developer.github.com/v3/issues/labels/>.
784 var name
: String is writable
787 var color
: String is writable
790 # A Github milestone.
792 # Should be accessed from `GithubAPI::get_milestone`.
794 # See <https://developer.github.com/v3/issues/milestones/>.
798 # The milestone id on Github.
799 var number
: nullable Int = null is writable
802 var title
: String is writable
804 # Milestone long description.
805 var description
: nullable String is writable
807 # Count of opened issues linked to this milestone.
808 var open_issues
: nullable Int = null is writable
810 # Count of closed issues linked to this milestone.
811 var closed_issues
: nullable Int = null is writable
814 var state
: nullable String is writable
816 # Creation time as String.
817 var created_at
: nullable String is writable
819 # User that created this milestone.
820 var creator
: nullable User is writable
822 # Due time as String (if any).
823 var due_on
: nullable String is writable
825 # Last update time as String (if any).
826 var updated_at
: nullable String is writable
828 # Close time as String (if any).
829 var closed_at
: nullable String is writable
834 # There is two kinds of comments:
836 # * `CommitComment` are made on a commit page.
837 # * `IssueComment` are made on an issue or pull request page.
838 # * `PullComment` are made on the diff associated to a pull request.
839 abstract class Comment
842 # Identifier of this comment.
843 var id
: Int is writable
845 # User that made this comment.
846 var user
: User is writable
848 # Creation time as String.
849 var created_at
: String is writable
851 # Last update time as String (if any).
852 var updated_at
: nullable String is writable
855 var body
: String is writable
857 # Does the comment contain an acknowledgement (+1)
859 return body
.has
("\\+1\\b".to_re
) or body
.has
(":+1:") or body
.has
(":shipit:")
863 # A comment made on a commit.
869 var commit_id
: String is writable
871 # Position of the comment on the line.
872 var position
: nullable Int is writable
874 # Line of the comment.
875 var line
: nullable Int is writable
877 # Path of the commented file.
878 var path
: nullable String is writable
883 # Can contain sub-status for reviews, CI etc.
887 # Global state of this commit
888 var state
: nullable String = null is optional
, writable
890 # Sha of the commit this status is for
891 var sha
: nullable String = null is optional
, writable
893 # Repository the commit belongs to
894 var repository
: nullable Repo = null is optional
, writable
896 # All sub statuses (one for each check)
897 var statuses
= new Array[RepoStatus] is optional
, writable
899 # Total count of sub statuses
900 var total_count
: nullable Int = null is optional
, writable
903 # Sub status of a CommitStatus
905 # Represents a check applied to a commit (reviews, CI, ...).
909 # State of this check
910 var state
: nullable String = null is optional
, writable
912 # Description of this check
913 var description
: nullable String = null is optional
, writable
916 var target_url
: nullable String = null is optional
, writable
918 # Context this status is related to
920 # Used to hold the name of the check applied.
921 var context
: nullable String = null is optional
, writable
923 # Date when this status was created
924 var created_at
: nullable String = null is optional
, writable
926 # Last date this status was updated
927 var updated_at
: nullable String = null is optional
, writable
930 # Comments made on Github issue and pull request pages.
932 # Should be accessed from `GithubAPI::get_issue_comment`.
934 # See <https://developer.github.com/v3/issues/comments/>.
939 # Issue that contains `self`.
940 fun issue_number
: Int do return issue_url
.split
("/").last
.to_i
942 # Link to the issue document on API.
943 var issue_url
: String is writable
946 # Comments made on Github pull request diffs.
948 # Should be accessed from `GithubAPI::get_diff_comment`.
950 # See <https://developer.github.com/v3/pulls/comments/>.
955 # Pull request that contains `self`.
956 fun pull_number
: Int do return pull_request_url
.split
("/").last
.to_i
958 # Link to the pull request on API.
959 var pull_request_url
: String is writable
962 var diff_hunk
: String is writable
964 # Path of commented file.
965 var path
: String is writable
967 # Position of the comment on the file.
968 var position
: nullable Int is writable
970 # Original position in the diff.
971 var original_position
: Int is writable
973 # Commit referenced by this comment.
974 var commit_id
: String is writable
976 # Original commit id.
977 var original_commit_id
: String is writable
980 # An event that occurs on a Github `Issue`.
982 # Should be accessed from `GithubAPI::get_issue_event`.
984 # See <https://developer.github.com/v3/issues/events/>.
988 # Event id on Github.
989 var id
: Int is writable
991 # User that initiated the event.
992 var actor
: User is writable
994 # Creation time as String.
995 var created_at
: String is writable
998 var event
: String is writable
1000 # Commit linked to this event (if any).
1001 var commit_id
: nullable String is writable
1003 # Label linked to this event (if any).
1004 var labl
: nullable Label is writable, serialize_as
("label")
1006 # User linked to this event (if any).
1007 var assignee
: nullable User is writable
1009 # Milestone linked to this event (if any).
1010 var milestone
: nullable Milestone is writable
1012 # Rename linked to this event (if any).
1013 var rename
: nullable RenameAction is writable
1016 # A rename action maintains the name before and after a renaming action.
1020 # Name before renaming.
1021 var from
: String is writable
1023 # Name after renaming.
1024 var to
: String is writable
1028 # Should be accessed from `Repo::contrib_stats`.
1030 # See <https://developer.github.com/v3/repos/statistics/>.
1031 class ContributorStats
1035 redef type OTHER: ContributorStats
1037 # Github API client.
1038 var api
: GithubAPI is writable
1040 # User these statistics are about.
1041 var author
: User is writable
1043 # Total number of commit.
1044 var total
: Int is writable
1046 # Are of weeks of activity with detailed statistics.
1047 var weeks
: JsonArray is writable
1049 # ContributorStats can be compared on the total amount of commits.
1050 redef fun <(o
) do return total
< o
.total
1053 # A Github file representation.
1055 # Mostly a wrapper around a json object.
1060 var filename
: String is writable
1063 # A list of results returned buy `/search`
1067 # Total count with other pages
1068 var total_count
: Int
1070 # Does this page contain all the results?
1071 var incomplete_results
: Bool
1073 # Results in this page
1074 var items
: Array[Object]
1077 # JsonDeserializer specific for Github objects.
1078 class GithubDeserializer
1079 super JsonDeserializer
1081 private var pattern_base
= "https://api.github.com"
1083 # Url patterns to class names
1084 var url_patterns
: Map[Regex, String] is lazy
do
1085 var map
= new HashMap[Regex, String]
1086 map
["{pattern_base}/users/[^/]*$".to_re
] = "User"
1087 map
["{pattern_base}/repos/[^/]*/[^/]*$".to_re
] = "Repo"
1088 map
["{pattern_base}/repos/[^/]*/[^/]*/labels/[^/]+$".to_re
] = "Label"
1089 map
["{pattern_base}/repos/[^/]*/[^/]*/milestones/[0-9]+$".to_re
] = "Milestone"
1090 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/[0-9]+$".to_re
] = "Issue"
1091 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/comments/[0-9]+$".to_re
] = "IssueComment"
1092 map
["{pattern_base}/repos/[^/]*/[^/]*/issues/events/[0-9]+$".to_re
] = "IssueEvent"
1093 map
["{pattern_base}/repos/[^/]*/[^/]*/pulls/[0-9]+$".to_re
] = "PullRequest"
1094 map
["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re
] = "PullComment"
1095 map
["{pattern_base}/repos/[^/]*/[^/]*/comments/[0-9]+$".to_re
] = "CommitComment"
1096 map
["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+$".to_re
] = "Commit"
1097 map
["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+/status$".to_re
] = "CommitStatus"
1098 map
["{pattern_base}/repos/[^/]*/[^/]*/statuses/[a-f0-9]+$".to_re
] = "RepoStatus"
1102 # Match `url` property in object to a class name
1103 fun url_heuristic
(raw
: Map[String, nullable Object]): nullable String do
1104 if not raw
.has_key
("url") then return null
1106 var url
= raw
["url"].as(String)
1107 for re
, class_name
in url_patterns
do
1108 if url
.has
(re
) then return class_name
1113 redef fun class_name_heuristic
(raw
) do
1115 var class_name
= url_heuristic
(raw
)
1116 if class_name
!= null then return class_name
1118 # print raw.serialize_to_json(true, true) # debug
1120 # Use properties heuristics
1121 if raw
.has_key
("name") and raw
.has_key
("commit") then
1123 else if raw
.has_key
("total_count") and raw
.has_key
("items") then
1124 return "SearchResults"
1129 redef fun deserialize_class
(name
) do
1130 if name
== "Issue" then
1131 var issue
= super.as(Issue)
1132 if path
.last
.has_key
("pull_request") then
1133 issue
.is_pull_request
= true
1136 else if name
== "Commit" then
1137 var commit
= super.as(Commit)
1138 var git_commit
= commit
.commit
1139 if git_commit
!= null then commit
.message
= git_commit
.message
1146 # Gets the Github token from `git` configuration
1148 # Return the value of `git config --get github.oauthtoken`
1149 # or `""` if no key exists.
1150 fun get_github_oauth
: String
1152 var p
= new ProcessReader("git", "config", "--get", "github.oauthtoken")
1153 var token
= p
.read_line