@@ -5,7 +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
+ use serde_json:: { json , Value } ;
9
9
use std:: io:: Read ;
10
10
11
11
#[ derive( Debug , Default , Serialize ) ]
@@ -151,7 +151,7 @@ const IGNORED_DIRECTORIES: &[&str] = &[
151
151
"debug" ,
152
152
] ;
153
153
154
- const ALLOWED_LICENCES : & [ & str ] = & [
154
+ const ALLOWED_LICENSES : & [ & str ] = & [
155
155
"0bsd" ,
156
156
"apache-2.0" ,
157
157
"bsd-2-clause" ,
@@ -160,13 +160,18 @@ const ALLOWED_LICENCES: &[&str] = &[
160
160
"bsd-4-clause" ,
161
161
"isc" ,
162
162
"mit" ,
163
- "mpl-2.0" ,
164
163
"unlicense" ,
165
164
"wtfpl" ,
166
165
"zlib" ,
167
166
] ;
168
167
169
- pub async fn is_indexing_allowed ( repository : & Repository ) -> Result < bool > {
168
+ #[ derive( Serialize , Debug , Default ) ]
169
+ pub struct LicenseFetchResponse {
170
+ pub permissible : bool ,
171
+ pub error : Option < Value > ,
172
+ }
173
+
174
+ pub async fn fetch_license_info ( repository : & Repository ) -> Result < LicenseFetchResponse > {
170
175
let Repository { owner, name, .. } = repository;
171
176
let url = format ! ( "https://api.github.com/repos/{owner}/{name}/license" ) ;
172
177
@@ -181,8 +186,22 @@ pub async fn is_indexing_allowed(repository: &Repository) -> Result<bool> {
181
186
Ok ( response) => {
182
187
let response_json = response. json :: < Value > ( ) . await ?;
183
188
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)
189
+ let permissible: bool = ALLOWED_LICENSES . iter ( ) . any ( |k| k. eq ( & license_key) ) ;
190
+
191
+ Ok ( LicenseFetchResponse {
192
+ permissible,
193
+ error : if permissible {
194
+ None
195
+ } else {
196
+ Some ( json ! { {
197
+ "message" : "Impermissible repository license" ,
198
+ "license" : {
199
+ "name" : response_json[ "license" ] [ "name" ] ,
200
+ "url" : response_json[ "html_url" ]
201
+ }
202
+ } } )
203
+ } ,
204
+ } )
186
205
}
187
206
Err ( _) => Err ( anyhow:: anyhow!( "Unable to fetch repository license" ) ) ,
188
207
}
@@ -265,8 +284,9 @@ mod tests {
265
284
branch : "beta" . to_string ( ) ,
266
285
} ;
267
286
268
- let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
269
- assert_eq ! ( is_allowed, true ) ;
287
+ let license_info = fetch_license_info ( & repository) . await . unwrap_or_default ( ) ;
288
+ dbg ! ( & license_info) ;
289
+ assert_eq ! ( license_info. permissible, true ) ;
270
290
271
291
//Permissible
272
292
let repository = Repository {
@@ -275,8 +295,8 @@ mod tests {
275
295
branch : "main" . to_string ( ) ,
276
296
} ;
277
297
278
- let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
279
- assert_eq ! ( is_allowed , true ) ;
298
+ let license_info = fetch_license_info ( & repository) . await . unwrap_or_default ( ) ;
299
+ assert_eq ! ( license_info . permissible , true ) ;
280
300
281
301
//Impermissible
282
302
let repository = Repository {
@@ -285,7 +305,7 @@ mod tests {
285
305
branch : "main" . to_string ( ) ,
286
306
} ;
287
307
288
- let is_allowed = is_indexing_allowed ( & repository) . await . unwrap_or_default ( ) ;
289
- assert_eq ! ( is_allowed , false ) ;
308
+ let license_info = fetch_license_info ( & repository) . await . unwrap_or_default ( ) ;
309
+ assert_eq ! ( license_info . permissible , false ) ;
290
310
}
291
311
}
0 commit comments