-
Notifications
You must be signed in to change notification settings - Fork 624
/
Copy pathkrate.rs
583 lines (520 loc) · 18.1 KB
/
krate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use chrono::{NaiveDate, NaiveDateTime};
use diesel;
use diesel::associations::Identifiable;
use diesel::pg::Pg;
use diesel::prelude::*;
use license_exprs;
use semver;
use url::Url;
use app::App;
use util::{human, CargoResult};
use models::{
Badge, Category, CrateOwner, Keyword, NewCrateOwnerInvitation, Owner, OwnerKind,
ReverseDependency, User, Version,
};
use views::{EncodableCrate, EncodableCrateLinks};
use models::helpers::with_count::*;
use schema::*;
/// Hosts in this blacklist are known to not be hosting documentation,
/// and are possibly of malicious intent e.g. ad tracking networks, etc.
const DOCUMENTATION_BLACKLIST: [&str; 1] = ["rust-ci.org"];
#[derive(Debug, Insertable, Queryable, Identifiable, Associations, AsChangeset, Clone, Copy)]
#[belongs_to(Crate)]
#[primary_key(crate_id, date)]
#[table_name = "crate_downloads"]
pub struct CrateDownload {
pub crate_id: i32,
pub downloads: i32,
pub date: NaiveDate,
}
#[derive(Debug, Clone, Queryable, Identifiable, Associations, AsChangeset, QueryableByName)]
#[table_name = "crates"]
pub struct Crate {
pub id: i32,
pub name: String,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub downloads: i32,
pub description: Option<String>,
pub homepage: Option<String>,
pub documentation: Option<String>,
pub readme: Option<String>,
pub readme_file: Option<String>,
pub license: Option<String>,
pub repository: Option<String>,
pub max_upload_size: Option<i32>,
}
/// We literally never want to select `textsearchable_index_col`
/// so we provide this type and constant to pass to `.select`
type AllColumns = (
crates::id,
crates::name,
crates::updated_at,
crates::created_at,
crates::downloads,
crates::description,
crates::homepage,
crates::documentation,
crates::readme,
crates::readme_file,
crates::license,
crates::repository,
crates::max_upload_size,
);
pub const ALL_COLUMNS: AllColumns = (
crates::id,
crates::name,
crates::updated_at,
crates::created_at,
crates::downloads,
crates::description,
crates::homepage,
crates::documentation,
crates::readme,
crates::readme_file,
crates::license,
crates::repository,
crates::max_upload_size,
);
pub const MAX_NAME_LENGTH: usize = 64;
type CanonCrateName<T> = self::canon_crate_name::HelperType<T>;
type All = diesel::dsl::Select<crates::table, AllColumns>;
type WithName<'a> = diesel::dsl::Eq<CanonCrateName<crates::name>, CanonCrateName<&'a str>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
#[derive(Insertable, AsChangeset, Default, Debug)]
#[table_name = "crates"]
#[changeset_options(treat_none_as_null = "true")]
#[primary_key(name, max_upload_size)] // This is actually just to skip updating them
pub struct NewCrate<'a> {
pub name: &'a str,
pub description: Option<&'a str>,
pub homepage: Option<&'a str>,
pub documentation: Option<&'a str>,
pub readme: Option<&'a str>,
pub readme_file: Option<&'a str>,
pub repository: Option<&'a str>,
pub max_upload_size: Option<i32>,
pub license: Option<&'a str>,
}
impl<'a> NewCrate<'a> {
pub fn create_or_update(
mut self,
conn: &PgConnection,
license_file: Option<&'a str>,
uploader: i32,
) -> CargoResult<Crate> {
use diesel::update;
self.validate(license_file)?;
self.ensure_name_not_reserved(conn)?;
conn.transaction(|| {
// To avoid race conditions, we try to insert
// first so we know whether to add an owner
if let Some(krate) = self.save_new_crate(conn, uploader)? {
return Ok(krate);
}
update(crates::table)
.filter(canon_crate_name(crates::name).eq(canon_crate_name(self.name)))
.set(&self)
.returning(ALL_COLUMNS)
.get_result(conn)
.map_err(Into::into)
})
}
fn validate(&mut self, license_file: Option<&'a str>) -> CargoResult<()> {
fn validate_url(url: Option<&str>, field: &str) -> CargoResult<()> {
let url = match url {
Some(s) => s,
None => return Ok(()),
};
let url = Url::parse(url)
.map_err(|_| human(&format_args!("`{}` is not a valid url: `{}`", field, url)))?;
match &url.scheme()[..] {
"http" | "https" => {}
s => {
return Err(human(&format_args!(
"`{}` has an invalid url \
scheme: `{}`",
field, s
)))
}
}
if url.cannot_be_a_base() {
return Err(human(&format_args!(
"`{}` must have relative scheme \
data: {}",
field, url
)));
}
Ok(())
}
validate_url(self.homepage, "homepage")?;
validate_url(self.documentation, "documentation")?;
validate_url(self.repository, "repository")?;
self.validate_license(license_file)?;
Ok(())
}
fn validate_license(&mut self, license_file: Option<&str>) -> CargoResult<()> {
if let Some(license) = self.license {
for part in license.split('/') {
license_exprs::validate_license_expr(part).map_err(|e| {
human(&format_args!(
"{}; see http://opensource.org/licenses \
for options, and http://spdx.org/licenses/ \
for their identifiers",
e
))
})?;
}
} else if license_file.is_some() {
// If no license is given, but a license file is given, flag this
// crate as having a nonstandard license. Note that we don't
// actually do anything else with license_file currently.
self.license = Some("non-standard");
}
Ok(())
}
fn ensure_name_not_reserved(&self, conn: &PgConnection) -> CargoResult<()> {
use diesel::dsl::exists;
use diesel::select;
use schema::reserved_crate_names::dsl::*;
let reserved_name = select(exists(
reserved_crate_names.filter(canon_crate_name(name).eq(canon_crate_name(self.name))),
)).get_result::<bool>(conn)?;
if reserved_name {
Err(human("cannot upload a crate with a reserved name"))
} else {
Ok(())
}
}
fn save_new_crate(&self, conn: &PgConnection, user_id: i32) -> QueryResult<Option<Crate>> {
use schema::crates::dsl::*;
conn.transaction(|| {
let maybe_inserted = diesel::insert_into(crates)
.values(self)
.on_conflict_do_nothing()
.returning(ALL_COLUMNS)
.get_result::<Crate>(conn)
.optional()?;
if let Some(ref krate) = maybe_inserted {
let owner = CrateOwner {
crate_id: krate.id,
owner_id: user_id,
created_by: user_id,
owner_kind: OwnerKind::User as i32,
};
diesel::insert_into(crate_owners::table)
.values(&owner)
.execute(conn)?;
}
Ok(maybe_inserted)
})
}
}
impl Crate {
pub fn with_name(name: &str) -> WithName<'_> {
canon_crate_name(crates::name).eq(canon_crate_name(name))
}
pub fn by_name(name: &str) -> ByName<'_> {
Crate::all().filter(Self::with_name(name))
}
pub fn all() -> All {
crates::table.select(ALL_COLUMNS)
}
pub fn valid_name(name: &str) -> bool {
let under_max_length = name.chars().take(MAX_NAME_LENGTH + 1).count() <= MAX_NAME_LENGTH;
Crate::valid_ident(name) && under_max_length
}
fn valid_ident(name: &str) -> bool {
Self::valid_feature_name(name)
&& name
.chars()
.nth(0)
.map(char::is_alphabetic)
.unwrap_or(false)
}
pub fn valid_feature_name(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
&& name.chars().all(|c| c.is_ascii())
}
pub fn valid_feature(name: &str) -> bool {
let mut parts = name.split('/');
match parts.next() {
Some(part) if !Crate::valid_feature_name(part) => return false,
None => return false,
_ => {}
}
match parts.next() {
Some(part) if !Crate::valid_feature_name(part) => return false,
_ => {}
}
parts.next().is_none()
}
pub fn minimal_encodable(
self,
max_version: &semver::Version,
badges: Option<Vec<Badge>>,
exact_match: bool,
recent_downloads: Option<i64>,
) -> EncodableCrate {
self.encodable(
max_version,
None,
None,
None,
badges,
exact_match,
recent_downloads,
)
}
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
pub fn encodable(
self,
max_version: &semver::Version,
versions: Option<Vec<i32>>,
keywords: Option<&[Keyword]>,
categories: Option<&[Category]>,
badges: Option<Vec<Badge>>,
exact_match: bool,
recent_downloads: Option<i64>,
) -> EncodableCrate {
let Crate {
name,
created_at,
updated_at,
downloads,
description,
homepage,
documentation,
repository,
..
} = self;
let versions_link = match versions {
Some(..) => None,
None => Some(format!("/api/v1/crates/{}/versions", name)),
};
let keyword_ids = keywords.map(|kws| kws.iter().map(|kw| kw.keyword.clone()).collect());
let category_ids = categories.map(|cats| cats.iter().map(|cat| cat.slug.clone()).collect());
let badges = badges.map(|bs| bs.into_iter().map(|b| b.encodable()).collect());
let documentation = Crate::remove_blacklisted_documentation_urls(documentation);
EncodableCrate {
id: name.clone(),
name: name.clone(),
updated_at,
created_at,
downloads,
recent_downloads,
versions,
keywords: keyword_ids,
categories: category_ids,
badges,
max_version: max_version.to_string(),
documentation,
homepage,
exact_match,
description,
repository,
links: EncodableCrateLinks {
version_downloads: format!("/api/v1/crates/{}/downloads", name),
versions: versions_link,
owners: Some(format!("/api/v1/crates/{}/owners", name)),
owner_team: Some(format!("/api/v1/crates/{}/owner_team", name)),
owner_user: Some(format!("/api/v1/crates/{}/owner_user", name)),
reverse_dependencies: format!("/api/v1/crates/{}/reverse_dependencies", name),
},
}
}
/// Return `None` if the documentation URL host matches a blacklisted host
fn remove_blacklisted_documentation_urls(url: Option<String>) -> Option<String> {
// Handles if documentation URL is None
let url = match url {
Some(url) => url,
None => return None,
};
// Handles unsuccessful parsing of documentation URL
let parsed_url = match Url::parse(&url) {
Ok(parsed_url) => parsed_url,
Err(_) => return None,
};
// Extract host string from documentation URL
let url_host = match parsed_url.host_str() {
Some(url_host) => url_host,
None => return None,
};
// Match documentation URL host against blacklisted host array elements
if DOCUMENTATION_BLACKLIST.contains(&url_host) {
None
} else {
Some(url)
}
}
pub fn max_version(&self, conn: &PgConnection) -> CargoResult<semver::Version> {
use schema::versions::dsl::*;
let vs = self
.versions()
.select(num)
.load::<String>(conn)?
.into_iter()
.map(|s| semver::Version::parse(&s).unwrap());
Ok(Version::max(vs))
}
pub fn owners(&self, conn: &PgConnection) -> CargoResult<Vec<Owner>> {
let base_query = CrateOwner::belonging_to(self).filter(crate_owners::deleted.eq(false));
let users = base_query
.inner_join(users::table)
.select(users::all_columns)
.filter(crate_owners::owner_kind.eq(OwnerKind::User as i32))
.load(conn)?
.into_iter()
.map(Owner::User);
let teams = base_query
.inner_join(teams::table)
.select(teams::all_columns)
.filter(crate_owners::owner_kind.eq(OwnerKind::Team as i32))
.load(conn)?
.into_iter()
.map(Owner::Team);
Ok(users.chain(teams).collect())
}
pub fn owner_add(
&self,
app: &App,
conn: &PgConnection,
req_user: &User,
login: &str,
) -> CargoResult<String> {
use diesel::insert_into;
let owner = Owner::find_or_create_by_login(app, conn, req_user, login)?;
match owner {
// Users are invited and must accept before being added
owner @ Owner::User(_) => {
insert_into(crate_owner_invitations::table)
.values(&NewCrateOwnerInvitation {
invited_user_id: owner.id(),
invited_by_user_id: req_user.id,
crate_id: self.id,
})
.on_conflict_do_nothing()
.execute(conn)?;
Ok(format!(
"user {} has been invited to be an owner of crate {}",
owner.login(),
self.name
))
}
// Teams are added as owners immediately
owner @ Owner::Team(_) => {
insert_into(crate_owners::table)
.values(&CrateOwner {
crate_id: self.id,
owner_id: owner.id(),
created_by: req_user.id,
owner_kind: OwnerKind::Team as i32,
})
.on_conflict(crate_owners::table.primary_key())
.do_update()
.set(crate_owners::deleted.eq(false))
.execute(conn)?;
Ok(format!(
"team {} has been added as an owner of crate {}",
owner.login(),
self.name
))
}
}
}
pub fn owner_remove(
&self,
app: &App,
conn: &PgConnection,
req_user: &User,
login: &str,
) -> CargoResult<()> {
let owner = Owner::find_or_create_by_login(app, conn, req_user, login)?;
let target = crate_owners::table.find((self.id(), owner.id(), owner.kind() as i32));
diesel::update(target)
.set(crate_owners::deleted.eq(true))
.execute(conn)?;
Ok(())
}
pub fn badges(&self, conn: &PgConnection) -> QueryResult<Vec<Badge>> {
badges::table
.filter(badges::crate_id.eq(self.id))
.load(conn)
}
/// Returns (dependency, dependent crate name, dependent crate downloads)
pub fn reverse_dependencies(
&self,
conn: &PgConnection,
offset: i64,
limit: i64,
) -> QueryResult<(Vec<ReverseDependency>, i64)> {
use diesel::sql_query;
use diesel::sql_types::{BigInt, Integer};
let rows = sql_query(include_str!("krate_reverse_dependencies.sql"))
.bind::<Integer, _>(self.id)
.bind::<BigInt, _>(offset)
.bind::<BigInt, _>(limit)
.load::<WithCount<ReverseDependency>>(conn)?;
Ok(rows.records_and_total())
}
}
use diesel::sql_types::{Date, Text};
sql_function!(fn canon_crate_name(x: Text) -> Text);
sql_function!(fn to_char(a: Date, b: Text) -> Text);
#[cfg(test)]
mod tests {
use models::Crate;
#[test]
fn documentation_blacklist_no_url_provided() {
assert_eq!(Crate::remove_blacklisted_documentation_urls(None), None);
}
#[test]
fn documentation_blacklist_invalid_url() {
assert_eq!(
Crate::remove_blacklisted_documentation_urls(Some(String::from("not a url"))),
None
);
}
#[test]
fn documentation_blacklist_url_contains_partial_match() {
assert_eq!(
Crate::remove_blacklisted_documentation_urls(Some(String::from(
"http://rust-ci.organists.com"
)),),
Some(String::from("http://rust-ci.organists.com"))
);
}
#[test]
fn documentation_blacklist_blacklisted_url() {
assert_eq!(
Crate::remove_blacklisted_documentation_urls(Some(String::from(
"http://rust-ci.org/crate/crate-0.1/doc/crate-0.1",
),),),
None
);
}
}
pub trait CrateVersions {
fn versions(&self) -> versions::BoxedQuery<'_, Pg> {
self.all_versions().filter(versions::yanked.eq(false))
}
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg>;
}
impl CrateVersions for Crate {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
Version::belonging_to(self).into_boxed()
}
}
impl CrateVersions for Vec<Crate> {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
self.as_slice().all_versions()
}
}
impl CrateVersions for [Crate] {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
Version::belonging_to(self).into_boxed()
}
}