Skip to content

Commit 7b55b13

Browse files
committed
models/version: Move license validation into publish endpoint
1 parent 0811a12 commit 7b55b13

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/controllers/krate/publish.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::models::{
1919

2020
use crate::middleware::log_request::RequestLogExt;
2121
use crate::models::token::EndpointScope;
22+
use crate::models::version::validate_license_expr;
2223
use crate::rate_limiter::LimitedAction;
2324
use crate::schema::*;
2425
use crate::sql::canon_crate_name;
@@ -115,7 +116,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
115116
let package = tarball_info.manifest.package.unwrap();
116117

117118
let description = package.description.map(|it| it.as_local().unwrap());
118-
let license = package.license.map(|it| it.as_local().unwrap());
119+
let mut license = package.license.map(|it| it.as_local().unwrap());
119120
let license_file = package.license_file.map(|it| it.as_local().unwrap());
120121

121122
// Make sure required fields are provided
@@ -136,6 +137,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
136137
return Err(cargo_err(&message));
137138
}
138139

140+
if let Some(ref license) = license {
141+
validate_license_expr(license)?;
142+
} else if license_file.is_some() {
143+
// If no license is given, but a license file is given, flag this
144+
// crate as having a nonstandard license. Note that we don't
145+
// actually do anything else with license_file currently.
146+
license = Some(String::from("non-standard"));
147+
}
148+
139149
// Create a transaction on the database, if there are no errors,
140150
// commit the transactions to record a new or updated crate.
141151
conn.transaction(|conn| {

src/models/version.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl NewVersion {
139139
num: &semver::Version,
140140
features: &BTreeMap<String, Vec<String>>,
141141
license: Option<String>,
142-
license_file: Option<&str>,
142+
#[allow(unused_variables)] license_file: Option<&str>,
143143
crate_size: i32,
144144
published_by: i32,
145145
checksum: String,
@@ -148,7 +148,7 @@ impl NewVersion {
148148
) -> AppResult<Self> {
149149
let features = serde_json::to_value(features)?;
150150

151-
let mut new_version = NewVersion {
151+
Ok(NewVersion {
152152
crate_id,
153153
num: num.to_string(),
154154
features,
@@ -158,11 +158,7 @@ impl NewVersion {
158158
checksum,
159159
links,
160160
rust_version,
161-
};
162-
163-
new_version.validate_license(license_file)?;
164-
165-
Ok(new_version)
161+
})
166162
}
167163

168164
pub fn save(&self, conn: &mut PgConnection, published_by_email: &str) -> AppResult<Version> {
@@ -195,18 +191,6 @@ impl NewVersion {
195191
Ok(version)
196192
})
197193
}
198-
199-
fn validate_license(&mut self, license_file: Option<&str>) -> AppResult<()> {
200-
if let Some(ref license) = self.license {
201-
validate_license_expr(license)?;
202-
} else if license_file.is_some() {
203-
// If no license is given, but a license file is given, flag this
204-
// crate as having a nonstandard license. Note that we don't
205-
// actually do anything else with license_file currently.
206-
self.license = Some(String::from("non-standard"));
207-
}
208-
Ok(())
209-
}
210194
}
211195

212196
pub fn validate_license_expr(s: &str) -> AppResult<()> {

0 commit comments

Comments
 (0)