1
1
2
- use Metadata ;
3
2
use utils:: MetadataPackage ;
4
3
use docbuilder:: BuildResult ;
5
4
use regex:: Regex ;
@@ -9,6 +8,7 @@ use std::io::BufReader;
9
8
use std:: path:: Path ;
10
9
use std:: fs;
11
10
11
+ use time:: Timespec ;
12
12
use rustc_serialize:: json:: { Json , ToJson } ;
13
13
use slug:: slugify;
14
14
use reqwest:: Client ;
@@ -28,6 +28,8 @@ pub(crate) fn add_package_into_database(conn: &Connection,
28
28
res : & BuildResult ,
29
29
files : Option < Json > ,
30
30
doc_targets : Vec < String > ,
31
+ default_target : & Option < String > ,
32
+ cratesio_data : & CratesIoData ,
31
33
has_docs : bool ,
32
34
has_examples : bool )
33
35
-> Result < i32 > {
@@ -36,9 +38,7 @@ pub(crate) fn add_package_into_database(conn: &Connection,
36
38
let dependencies = convert_dependencies ( metadata_pkg) ;
37
39
let rustdoc = get_rustdoc ( metadata_pkg, source_dir) . unwrap_or ( None ) ;
38
40
let readme = get_readme ( metadata_pkg, source_dir) . unwrap_or ( None ) ;
39
- let ( release_time, yanked, downloads) = get_release_time_yanked_downloads ( metadata_pkg) ?;
40
41
let is_library = metadata_pkg. is_library ( ) ;
41
- let metadata = Metadata :: from_source_dir ( source_dir) ?;
42
42
43
43
let release_id: i32 = {
44
44
let rows = conn. query ( "SELECT id FROM releases WHERE crate_id = $1 AND version = $2" ,
@@ -61,10 +61,10 @@ pub(crate) fn add_package_into_database(conn: &Connection,
61
61
RETURNING id" ,
62
62
& [ & crate_id,
63
63
& metadata_pkg. version ,
64
- & release_time,
64
+ & cratesio_data . release_time ,
65
65
& dependencies. to_json ( ) ,
66
66
& metadata_pkg. package_name ( ) ,
67
- & yanked,
67
+ & cratesio_data . yanked ,
68
68
& res. successful ,
69
69
& has_docs,
70
70
& false , // TODO: Add test status somehow
@@ -77,13 +77,13 @@ pub(crate) fn add_package_into_database(conn: &Connection,
77
77
& metadata_pkg. authors . to_json ( ) ,
78
78
& metadata_pkg. keywords . to_json ( ) ,
79
79
& has_examples,
80
- & downloads,
80
+ & cratesio_data . downloads ,
81
81
& files,
82
82
& doc_targets. to_json ( ) ,
83
83
& is_library,
84
84
& res. rustc_version ,
85
85
& metadata_pkg. documentation ,
86
- & metadata . default_target ] ) ?;
86
+ & default_target] ) ?;
87
87
// return id
88
88
rows. get ( 0 ) . get ( 0 )
89
89
@@ -115,10 +115,10 @@ pub(crate) fn add_package_into_database(conn: &Connection,
115
115
WHERE crate_id = $1 AND version = $2" ,
116
116
& [ & crate_id,
117
117
& format ! ( "{}" , metadata_pkg. version) ,
118
- & release_time,
118
+ & cratesio_data . release_time ,
119
119
& dependencies. to_json ( ) ,
120
120
& metadata_pkg. package_name ( ) ,
121
- & yanked,
121
+ & cratesio_data . yanked ,
122
122
& res. successful ,
123
123
& has_docs,
124
124
& false , // TODO: Add test status somehow
@@ -131,21 +131,21 @@ pub(crate) fn add_package_into_database(conn: &Connection,
131
131
& metadata_pkg. authors . to_json ( ) ,
132
132
& metadata_pkg. keywords . to_json ( ) ,
133
133
& has_examples,
134
- & downloads,
134
+ & cratesio_data . downloads ,
135
135
& files,
136
136
& doc_targets. to_json ( ) ,
137
137
& is_library,
138
138
& res. rustc_version ,
139
139
& metadata_pkg. documentation ,
140
- & metadata . default_target ] ) ?;
140
+ & default_target] ) ?;
141
141
rows. get ( 0 ) . get ( 0 )
142
142
}
143
143
} ;
144
144
145
145
146
146
add_keywords_into_database ( & conn, & metadata_pkg, & release_id) ?;
147
147
add_authors_into_database ( & conn, & metadata_pkg, & release_id) ?;
148
- add_owners_into_database ( & conn, & metadata_pkg , & crate_id) ?;
148
+ add_owners_into_database ( & conn, & cratesio_data . owners , & crate_id) ?;
149
149
150
150
151
151
// Update versions
@@ -284,6 +284,27 @@ fn read_rust_doc(file_path: &Path) -> Result<Option<String>> {
284
284
}
285
285
286
286
287
+ pub ( crate ) struct CratesIoData {
288
+ pub ( crate ) release_time : Timespec ,
289
+ pub ( crate ) yanked : bool ,
290
+ pub ( crate ) downloads : i32 ,
291
+ pub ( crate ) owners : Vec < CrateOwner > ,
292
+ }
293
+
294
+ impl CratesIoData {
295
+ pub ( crate ) fn get_from_network ( pkg : & MetadataPackage ) -> Result < Self > {
296
+ let ( release_time, yanked, downloads) = get_release_time_yanked_downloads ( pkg) ?;
297
+ let owners = get_owners ( pkg) ?;
298
+
299
+ Ok ( Self {
300
+ release_time,
301
+ yanked,
302
+ downloads,
303
+ owners,
304
+ } )
305
+ }
306
+ }
307
+
287
308
288
309
/// Get release_time, yanked and downloads from crates.io
289
310
fn get_release_time_yanked_downloads (
@@ -394,9 +415,15 @@ fn add_authors_into_database(conn: &Connection, pkg: &MetadataPackage, release_i
394
415
}
395
416
396
417
418
+ pub ( crate ) struct CrateOwner {
419
+ pub ( crate ) avatar : String ,
420
+ pub ( crate ) email : String ,
421
+ pub ( crate ) login : String ,
422
+ pub ( crate ) name : String ,
423
+ }
397
424
398
- /// Adds owners into database
399
- fn add_owners_into_database ( conn : & Connection , pkg : & MetadataPackage , crate_id : & i32 ) -> Result < ( ) > {
425
+ /// Fetch owners from crates.io
426
+ fn get_owners ( pkg : & MetadataPackage ) -> Result < Vec < CrateOwner > > {
400
427
// owners available in: https://crates.io/api/v1/crates/rand/owners
401
428
let owners_url = format ! ( "https://crates.io/api/v1/crates/{}/owners" , pkg. name) ;
402
429
let client = Client :: new ( ) ;
@@ -409,6 +436,7 @@ fn add_owners_into_database(conn: &Connection, pkg: &MetadataPackage, crate_id:
409
436
res. read_to_string ( & mut body) . unwrap ( ) ;
410
437
let json = Json :: from_str ( & body[ ..] ) ?;
411
438
439
+ let mut result = Vec :: new ( ) ;
412
440
if let Some ( owners) = json. as_object ( )
413
441
. and_then ( |j| j. get ( "users" ) )
414
442
. and_then ( |j| j. as_array ( ) ) {
@@ -435,25 +463,38 @@ fn add_owners_into_database(conn: &Connection, pkg: &MetadataPackage, crate_id:
435
463
continue ;
436
464
}
437
465
438
- let owner_id: i32 = {
439
- let rows = conn. query ( "SELECT id FROM owners WHERE login = $1" , & [ & login] ) ?;
440
- if rows. len ( ) > 0 {
441
- rows. get ( 0 ) . get ( 0 )
442
- } else {
443
- conn. query ( "INSERT INTO owners (login, avatar, name, email)
444
- VALUES ($1, $2, $3, $4)
445
- RETURNING id" ,
446
- & [ & login, & avatar, & name, & email] ) ?
447
- . get ( 0 )
448
- . get ( 0 )
449
- }
450
- } ;
451
-
452
- // add relationship
453
- let _ = conn. query ( "INSERT INTO owner_rels (cid, oid) VALUES ($1, $2)" ,
454
- & [ crate_id, & owner_id] ) ;
466
+ result. push ( CrateOwner {
467
+ avatar : avatar. to_string ( ) ,
468
+ email : email. to_string ( ) ,
469
+ login : login. to_string ( ) ,
470
+ name : name. to_string ( ) ,
471
+ } ) ;
455
472
}
473
+ }
474
+
475
+ Ok ( result)
476
+ }
477
+
478
+ /// Adds owners into database
479
+ fn add_owners_into_database ( conn : & Connection , owners : & [ CrateOwner ] , crate_id : & i32 ) -> Result < ( ) > {
480
+ for owner in owners {
481
+ let owner_id: i32 = {
482
+ let rows = conn. query ( "SELECT id FROM owners WHERE login = $1" , & [ & owner. login ] ) ?;
483
+ if rows. len ( ) > 0 {
484
+ rows. get ( 0 ) . get ( 0 )
485
+ } else {
486
+ conn. query ( "INSERT INTO owners (login, avatar, name, email)
487
+ VALUES ($1, $2, $3, $4)
488
+ RETURNING id" ,
489
+ & [ & owner. login , & owner. avatar , & owner. name , & owner. email ] ) ?
490
+ . get ( 0 )
491
+ . get ( 0 )
492
+ }
493
+ } ;
456
494
495
+ // add relationship
496
+ let _ = conn. query ( "INSERT INTO owner_rels (cid, oid) VALUES ($1, $2)" ,
497
+ & [ crate_id, & owner_id] ) ;
457
498
}
458
499
Ok ( ( ) )
459
500
}
0 commit comments