@@ -5,6 +5,7 @@ use crate::{
5
5
} ;
6
6
use rayon:: prelude:: { IntoParallelIterator , ParallelIterator } ;
7
7
use serde:: { Deserialize , Serialize } ;
8
+ use serde_json:: Value ;
8
9
use std:: io:: Read ;
9
10
10
11
#[ derive( Debug , Default , Serialize ) ]
@@ -150,6 +151,43 @@ const IGNORED_DIRECTORIES: &[&str] = &[
150
151
"debug" ,
151
152
] ;
152
153
154
+ const ALLOWED_LICENCES : & [ & str ] = & [
155
+ "0bsd" ,
156
+ "apache-2.0" ,
157
+ "bsd-2-clause" ,
158
+ "bsd-3-clause" ,
159
+ "bsd-3-clause-clear" ,
160
+ "bsd-4-clause" ,
161
+ "isc" ,
162
+ "mit" ,
163
+ "mpl-2.0" ,
164
+ "unlicense" ,
165
+ "wtfpl" ,
166
+ "zlib" ,
167
+ ] ;
168
+
169
+ pub async fn is_indexing_allowed ( repository : & Repository ) -> Result < bool > {
170
+ let Repository { owner, name, .. } = repository;
171
+ let url = format ! ( "https://api.github.com/repos/{owner}/{name}/license" ) ;
172
+
173
+ //User-agent reference: https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#user-agent-required
174
+ let client = reqwest:: Client :: builder ( )
175
+ . user_agent ( "open-sauced" )
176
+ . build ( )
177
+ . unwrap ( ) ;
178
+
179
+ let response = client. get ( url) . send ( ) . await ?;
180
+ match response. error_for_status ( ) {
181
+ Ok ( response) => {
182
+ let response_json = response. json :: < Value > ( ) . await ?;
183
+ let license_key = response_json[ "license" ] [ "key" ] . as_str ( ) . unwrap_or_default ( ) ;
184
+ let is_allowed: bool = ALLOWED_LICENCES . iter ( ) . any ( |k| k. eq ( & license_key) ) ;
185
+ Ok ( is_allowed)
186
+ }
187
+ Err ( _) => Err ( anyhow:: anyhow!( "Unable to fetch repository license" ) ) ,
188
+ }
189
+ }
190
+
153
191
pub fn should_index ( path : & str ) -> bool {
154
192
!( IGNORED_EXTENSIONS . iter ( ) . any ( |ext| path. ends_with ( ext) )
155
193
|| IGNORED_DIRECTORIES . iter ( ) . any ( |dir| path. contains ( dir) ) )
@@ -217,4 +255,37 @@ mod tests {
217
255
let path = "path/to/file.tsx" ;
218
256
assert ! ( should_index( path) ) ;
219
257
}
258
+
259
+ #[ tokio:: test]
260
+ async fn test_is_indexing_allowed ( ) {
261
+ // Permissible
262
+ let repository = Repository {
263
+ owner : "open-sauced" . to_string ( ) ,
264
+ name : "ai" . to_string ( ) ,
265
+ branch : "beta" . to_string ( ) ,
266
+ } ;
267
+
268
+ let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
269
+ assert_eq ! ( is_allowed, true ) ;
270
+
271
+ //Permissible
272
+ let repository = Repository {
273
+ owner : "facebook" . to_string ( ) ,
274
+ name : "react" . to_string ( ) ,
275
+ branch : "main" . to_string ( ) ,
276
+ } ;
277
+
278
+ let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
279
+ assert_eq ! ( is_allowed, true ) ;
280
+
281
+ //Imermissible
282
+ let repository = Repository {
283
+ owner : "open-sauced" . to_string ( ) ,
284
+ name : "guestbook" . to_string ( ) ,
285
+ branch : "main" . to_string ( ) ,
286
+ } ;
287
+
288
+ let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
289
+ assert_eq ! ( is_allowed, false ) ;
290
+ }
220
291
}
0 commit comments