Merge: fix ci nitunit some
[nit.git] / contrib / github_search_for_jni / src / github_search_for_jni.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Script to scan Github for repositories possibly using JNI.
18 module github_search_for_jni
19
20 import github::api
21 import json::static
22
23 # The proprieties introduced by this redef are to be used only on a JSON object
24 # representing a Github repository.
25 redef class JsonObject
26 # The repository has at least 50% Java code
27 fun has_lots_of_java: Bool
28 do
29 var java_count = 0
30 if keys.has("Java") then java_count = self["Java"].as(Int)
31
32 if java_count == 0 then return false
33
34 var total = 0
35 for k, v in self do
36 assert v isa Int
37 total += v
38 end
39
40 return java_count * 100 / total > 50
41 end
42
43 # The repository has at least 100 lines of C code
44 fun has_some_c: Bool
45 do
46 var c_count = 0
47 if keys.has("C") then c_count = self["C"].as(Int)
48 return c_count > 100
49 end
50 end
51
52 # Query sent to Github
53 var main_query = "language:java"
54
55 # API client instance use for all requests
56 var api = new GithubAPI("OAUTH TOKEN (replace with your own)", "JNI project finder (nitlanguage.org)")
57
58 if "NIT_TESTING".environ == "true" then exit 0
59
60 # Current requested page
61 var page = 0
62 var per_page = 100
63 loop
64 # Get a page of the main query
65 var uri = "/search/repositories?q={main_query}&page={page}&per_page={per_page}&sort=stars"
66 var obj = api.send("GET", uri).parse_json.as(JsonObject)
67
68 # Main object has "total_count" and "items"
69 var items = obj["items"].as(JsonArray)
70
71 # "items" is an array of Json objects
72 for item in items do
73 assert item isa JsonObject
74
75 # Each item has "name" and "languages_url"
76 assert item.keys.has("name")
77 assert item.keys.has("languages_url")
78
79 # Download the language list
80 var lang_url = item["languages_url"].as(String)
81 var langs = api.send("GET", lang_url).parse_json.as(JsonObject)
82
83 # The project is of interest if it has lots of Java and at least some C
84 var may_be_of_interest = langs.has_lots_of_java and langs.has_some_c
85 if may_be_of_interest then print "{item["name"].to_s}: {item["forks"].to_s}; {langs.keys.join(", ")}; {item["html_url"].to_s}"
86 end
87
88 # If we got less pages than asked for, we are done!
89 if items.length < per_page then break
90
91 page += 1
92 end