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.
17 # This modules reifies Github API elements as Nit classes.
19 # For most use-cases you need to use the `GithubAPI` client.
24 # Interface to Github REST API.
26 # Used by all `GithubEntity` to perform requests.
31 # # Get Github authentification token.
32 # var token = get_github_oauth
33 # assert not token.is_empty
36 # var api = new GithubAPI(token)
39 # The API client allows to get Github API entities:
42 # var repo = api.load_repo("privat/nit")
43 # assert repo isa Repo
44 # assert repo.name == "nit"
46 # var user = api.load_user("Morriar")
47 # assert user isa User
48 # assert user.login == "Morriar"
52 # Github API OAuth token.
54 # This token is used to authenticate the application on Github API.
55 # Be aware that there is [rate limits](https://developer.github.com/v3/rate_limit/)
56 # associated to the key.
59 # Github API base url.
61 # Default is `https://api.github.com` and should not be changed.
62 var api_url
= "https://api.github.com"
64 # User agent used for HTTP requests.
66 # Default is `nit_github_api`.
68 # See <https://developer.github.com/v3/#user-agent-required>
69 var user_agent
= "nit_github_api"
73 # Internal Curl instance used to perform API calls.
74 private var ghcurl
: GithubCurl is noinit
78 # * `0`: only errors (default)
80 var verbose_lvl
= 0 is public
writable
83 ghcurl
= new GithubCurl(auth
, user_agent
)
86 # Execute a GET request on Github API.
88 # This method returns raw json data.
89 # See other `load_*` methods to use more expressive types.
91 # var api = new GithubAPI(get_github_oauth)
92 # var obj = api.get("repos/privat/nit")
93 # assert obj isa JsonObject
94 # assert obj["name"] == "nit"
96 # Returns `null` in case of `error`.
98 # obj = api.get("foo/bar/baz")
100 # assert api.was_error
101 # var err = api.last_error
102 # assert err isa GithubError
103 # assert err.name == "GithubAPIError"
104 # assert err.message == "Not Found"
105 fun get
(path
: String): nullable Jsonable do
106 path
= sanitize_uri
(path
)
107 var res
= ghcurl
.get_and_parse
("{api_url}/{path}")
108 if res
isa Error then
117 # Display a message depending on `verbose_lvl`.
118 fun message
(lvl
: Int, message
: String) do
119 if lvl
<= verbose_lvl
then print message
122 # Escape `uri` in an acceptable format for Github.
123 private fun sanitize_uri
(uri
: String): String do
124 # TODO better URI escape.
125 return uri
.replace
(" ", "%20")
128 # Last error occured during Github API communications.
129 var last_error
: nullable Error = null is public
writable
131 # Does the last request provoqued an error?
132 var was_error
= false is protected writable
134 # Load the json object from Github.
135 # See `GithubEntity::load_from_github`.
136 private fun load_from_github
(key
: String): JsonObject do
137 message
(1, "Get {key} (github)")
139 if was_error
then return new JsonObject
140 return res
.as(JsonObject)
143 # Get the Github user with `login`.
145 # Returns `null` if the user cannot be found.
147 # var api = new GithubAPI(get_github_oauth)
148 # var user = api.load_user("Morriar")
149 # assert user.login == "Morriar"
150 fun load_user
(login
: String): nullable User do
151 var user
= new User(self, login
)
152 user
.load_from_github
153 if was_error
then return null
157 # Get the Github repo with `full_name`.
159 # Returns `null` if the repo cannot be found.
161 # var api = new GithubAPI(get_github_oauth)
162 # var repo = api.load_repo("privat/nit")
163 # assert repo.name == "nit"
164 # assert repo.owner.login == "privat"
165 # assert repo.default_branch.name == "master"
166 fun load_repo
(full_name
: String): nullable Repo do
167 var repo
= new Repo(self, full_name
)
168 repo
.load_from_github
169 if was_error
then return null
173 # Get the Github branch with `name`.
175 # Returns `null` if the branch cannot be found.
177 # var api = new GithubAPI(get_github_oauth)
178 # var repo = api.load_repo("privat/nit")
179 # assert repo isa Repo
180 # var branch = api.load_branch(repo, "master")
181 # assert branch.name == "master"
182 # assert branch.commit isa Commit
183 fun load_branch
(repo
: Repo, name
: String): nullable Branch do
184 var branch
= new Branch(self, repo
, name
)
185 branch
.load_from_github
186 if was_error
then return null
190 # Get the Github commit with `sha`.
192 # Returns `null` if the commit cannot be found.
194 # var api = new GithubAPI(get_github_oauth)
195 # var repo = api.load_repo("privat/nit")
196 # assert repo isa Repo
197 # var commit = api.load_commit(repo, "64ce1f")
198 # assert commit isa Commit
199 fun load_commit
(repo
: Repo, sha
: String): nullable Commit do
200 var commit
= new Commit(self, repo
, sha
)
201 commit
.load_from_github
202 if was_error
then return null
207 # Something returned by the Github API.
209 # Mainly a Nit wrapper around a JSON objet.
210 abstract class GithubEntity
212 # Github API instance.
215 # FIXME constructor should be private
217 # Key used to access this entity from Github api base.
218 fun key
: String is abstract
220 # JSON representation of `self`.
222 # This is the same json structure than used by Github API.
223 var json
: JsonObject is noinit
, protected writable
225 # Load `json` from Github API.
226 private fun load_from_github
do
227 json
= api
.load_from_github
(key
)
230 redef fun to_s
do return json
.to_json
235 # Should be accessed from `GithubAPI::load_user`.
237 # See <https://developer.github.com/v3/users/>.
241 redef var key
is lazy
do return "users/{login}"
246 # Init `self` from a `json` object.
247 init from_json
(api
: GithubAPI, json
: JsonObject) do
248 init(api
, json
["login"].to_s
)
252 # Github User page url.
253 fun html_url
: String do return json
["html_url"].to_s
255 # Avatar image url for this user.
256 fun avatar_url
: String do return json
["avatar_url"].to_s
259 # A Github repository.
261 # Should be accessed from `GithubAPI::load_repo`.
263 # See <https://developer.github.com/v3/repos/>.
267 redef var key
is lazy
do return "repos/{full_name}"
269 # Repo full name on Github.
270 var full_name
: String
272 # Init `self` from a `json` object.
273 init from_json
(api
: GithubAPI, json
: JsonObject) do
274 init(api
, json
["full_name"].to_s
)
278 # Repo short name on Github.
279 fun name
: String do return json
["name"].to_s
281 # Github User page url.
282 fun html_url
: String do return json
["html_url"].to_s
284 # Get the repo owner.
286 return new User.from_json
(api
, json
["owner"].as(JsonObject))
289 # List of branches associated with their names.
290 fun branches
: Map[String, Branch] do
291 api
.message
(1, "Get branches for {full_name}")
292 var array
= api
.get
("repos/{full_name}/branches")
293 var res
= new HashMap[String, Branch]
294 if not array
isa JsonArray then return res
296 if not obj
isa JsonObject then continue
297 var name
= obj
["name"].to_s
298 res
[name
] = new Branch.from_json
(api
, self, obj
)
303 # Repo default branch.
304 fun default_branch
: Branch do
305 var name
= json
["default_branch"].to_s
306 var branch
= api
.load_branch
(self, name
)
307 assert branch
isa Branch
312 # A `RepoEntity` is something contained in a `Repo`.
313 abstract class RepoEntity
316 # Repo that contains `self`.
319 # Init `self` from a `json` object.
320 init from_json
(api
: GithubAPI, repo
: Repo, json
: JsonObject) do
329 # Should be accessed from `GithubAPI::load_branch`.
331 # See <https://developer.github.com/v3/repos/#list-branches>.
335 redef var key
is lazy
do return "{repo.key}/branches/{name}"
340 redef init from_json
(api
, repo
, json
) do
341 self.name
= json
["name"].to_s
345 # Get the last commit of `self`.
346 fun commit
: Commit do
347 return new Commit.from_json
(api
, repo
, json
["commit"].as(JsonObject))
350 # List all commits in `self`.
352 # This can be long depending on the branch size.
353 # Commit are returned in an unspecified order.
354 fun commits
: Array[Commit] do
355 var res
= new Array[Commit]
356 var done
= new HashSet[String]
357 var todos
= new Array[Commit]
359 while not todos
.is_empty
do
360 var commit
= todos
.pop
361 if done
.has
(commit
.sha
) then continue
364 for parent
in commit
.parents
do
374 # Should be accessed from `GithubAPI::load_commit`.
376 # See <https://developer.github.com/v3/commits/>.
380 redef var key
is lazy
do return "{repo.key}/commits/{sha}"
385 redef init from_json
(api
, repo
, json
) do
386 self.sha
= json
["sha"].to_s
390 # Parent commits of `self`.
391 fun parents
: Array[Commit] do
392 var res
= new Array[Commit]
393 var parents
= json
["parents"]
394 if not parents
isa JsonArray then return res
395 for obj
in parents
do
396 if not obj
isa JsonObject then continue
397 res
.add
(api
.load_commit
(repo
, obj
["sha"].to_s
).as(not null))
402 # Author of the commit.
403 fun author
: nullable User do
404 if not json
.has_key
("author") then return null
405 var user
= json
["author"]
406 if not user
isa JsonObject then return null
407 return new User.from_json
(api
, user
)
410 # Committer of the commit.
411 fun committer
: nullable User do
412 if not json
.has_key
("committer") then return null
413 var user
= json
["author"]
414 if not user
isa JsonObject then return null
415 return new User.from_json
(api
, user
)
418 # Authoring date as ISODate.
419 fun author_date
: ISODate do
420 var commit
= json
["commit"].as(JsonObject)
421 var author
= commit
["author"].as(JsonObject)
422 return new ISODate.from_string
(author
["date"].to_s
)
425 # Commit date as ISODate.
426 fun commit_date
: ISODate do
427 var commit
= json
["commit"].as(JsonObject)
428 var author
= commit
["committer"].as(JsonObject)
429 return new ISODate.from_string
(author
["date"].to_s
)
433 fun message
: String do return json
["commit"].as(JsonObject)["message"].to_s