Skip to content

Commit 5bcc19b

Browse files
authored
Merge pull request #7194 from Turbo87/desc-license-from-manifest
publish: Use `description`, `license` and `license_file` fields from embedded `Cargo.toml` file
2 parents 0be5672 + d48b5ad commit 5bcc19b

8 files changed

+53
-56
lines changed

src/controllers/krate/publish.rs

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,6 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
5151
request_log.add("crate_name", &*metadata.name);
5252
request_log.add("crate_version", &*metadata.vers);
5353

54-
// Make sure required fields are provided
55-
fn empty(s: Option<&String>) -> bool {
56-
s.map_or(true, String::is_empty)
57-
}
58-
59-
// It can have up to three elements per below conditions.
60-
let mut missing = Vec::with_capacity(3);
61-
62-
if empty(metadata.description.as_ref()) {
63-
missing.push("description");
64-
}
65-
if empty(metadata.license.as_ref()) && empty(metadata.license_file.as_ref()) {
66-
missing.push("license");
67-
}
68-
if !missing.is_empty() {
69-
let message = missing_metadata_error_message(&missing);
70-
return Err(cargo_err(&message));
71-
}
72-
7354
conduit_compat(move || {
7455
let conn = &mut *app.db_write()?;
7556

@@ -110,6 +91,51 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
11091
app.rate_limiter
11192
.check_rate_limit(user.id, rate_limit_action, conn)?;
11293

94+
let content_length = tarball_bytes.len() as u64;
95+
96+
let maximums = Maximums::new(
97+
existing_crate.as_ref().and_then(|c| c.max_upload_size),
98+
app.config.max_upload_size,
99+
app.config.max_unpack_size,
100+
);
101+
102+
if content_length > maximums.max_upload_size {
103+
return Err(cargo_err(&format_args!(
104+
"max upload size is: {}",
105+
maximums.max_upload_size
106+
)));
107+
}
108+
109+
let pkg_name = format!("{}-{}", &*metadata.name, &*metadata.vers);
110+
let tarball_info = process_tarball(&pkg_name, &*tarball_bytes, maximums.max_unpack_size)?;
111+
112+
// `unwrap()` is safe here since `process_tarball()` validates that
113+
// we only accept manifests with a `package` section and without
114+
// inheritance.
115+
let package = tarball_info.manifest.package.unwrap();
116+
117+
let description = package.description.map(|it| it.as_local().unwrap());
118+
let license = package.license.map(|it| it.as_local().unwrap());
119+
let license_file = package.license_file.map(|it| it.as_local().unwrap());
120+
121+
// Make sure required fields are provided
122+
fn empty(s: Option<&String>) -> bool {
123+
s.map_or(true, String::is_empty)
124+
}
125+
126+
// It can have up to three elements per below conditions.
127+
let mut missing = Vec::with_capacity(3);
128+
if empty(description.as_ref()) {
129+
missing.push("description");
130+
}
131+
if empty(license.as_ref()) && empty(license_file.as_ref()) {
132+
missing.push("license");
133+
}
134+
if !missing.is_empty() {
135+
let message = missing_metadata_error_message(&missing);
136+
return Err(cargo_err(&message));
137+
}
138+
113139
// Create a transaction on the database, if there are no errors,
114140
// commit the transactions to record a new or updated crate.
115141
conn.transaction(|conn| {
@@ -136,15 +162,15 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
136162
// Persist the new crate, if it doesn't already exist
137163
let persist = NewCrate {
138164
name: &name,
139-
description: metadata.description.as_deref(),
165+
description: description.as_deref(),
140166
homepage: metadata.homepage.as_deref(),
141167
documentation: metadata.documentation.as_deref(),
142168
readme: metadata.readme.as_deref(),
143169
repository: repo.as_deref(),
144170
max_upload_size: None,
145171
};
146172

147-
let license_file = metadata.license_file.as_deref();
173+
let license_file = license_file.as_deref();
148174

149175
validate_url(persist.homepage, "homepage")?;
150176
validate_url(persist.documentation, "documentation")?;
@@ -182,40 +208,17 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
182208
}
183209
}
184210

185-
let content_length = tarball_bytes.len() as u64;
186-
187-
let maximums = Maximums::new(
188-
krate.max_upload_size,
189-
app.config.max_upload_size,
190-
app.config.max_unpack_size,
191-
);
192-
193-
if content_length > maximums.max_upload_size {
194-
return Err(cargo_err(&format_args!(
195-
"max upload size is: {}",
196-
maximums.max_upload_size
197-
)));
198-
}
199-
200211
// Read tarball from request
201212
let hex_cksum: String = Sha256::digest(&tarball_bytes).encode_hex();
202213

203-
let pkg_name = format!("{}-{}", krate.name, vers);
204-
let tarball_info =
205-
process_tarball(&pkg_name, &*tarball_bytes, maximums.max_unpack_size)?;
206-
207-
// `unwrap()` is safe here since `process_tarball()` validates that
208-
// we only accept manifests with a `package` section and without
209-
// inheritance.
210-
let package = tarball_info.manifest.package.unwrap();
211214
let rust_version = package.rust_version.map(|rv| rv.as_local().unwrap());
212215

213216
// Persist the new version of this crate
214217
let version = NewVersion::new(
215218
krate.id,
216219
vers,
217220
&features,
218-
metadata.license,
221+
license,
219222
license_file,
220223
// Downcast is okay because the file length must be less than the max upload size
221224
// to get here, and max upload sizes are way less than i32 max

src/tests/builders/publish.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ impl PublishBuilder {
142142
vers: u::EncodableCrateVersion(self.version.clone()),
143143
features: self.features.clone(),
144144
deps: self.deps.clone(),
145-
description: self.desc.clone(),
146145
homepage: None,
147146
documentation: self.doc_url.clone(),
148147
readme: self.readme,
@@ -161,8 +160,6 @@ impl PublishBuilder {
161160
.map(u::EncodableCategory)
162161
.collect(),
163162
),
164-
license: self.license.clone(),
165-
license_file: self.license_file.clone(),
166163
repository: None,
167164
links: None,
168165
};

src/tests/krate/publish/snapshots/all__krate__publish__categories__too_many_categories.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid length 6, expected at most 5 categories per crate at line 1 column 219"
8+
"detail": "invalid upload request: invalid length 6, expected at most 5 categories per crate at line 1 column 191"
99
}
1010
]
1111
}

src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid value: string \"?@?%\", expected a valid keyword specifier at line 1 column 178"
8+
"detail": "invalid upload request: invalid value: string \"?@?%\", expected a valid keyword specifier at line 1 column 150"
99
}
1010
]
1111
}

src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords-3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid value: string \"áccênts\", expected a valid keyword specifier at line 1 column 183"
8+
"detail": "invalid upload request: invalid value: string \"áccênts\", expected a valid keyword specifier at line 1 column 155"
99
}
1010
]
1111
}

src/tests/krate/publish/snapshots/all__krate__publish__keywords__bad_keywords.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 203"
8+
"detail": "invalid upload request: invalid length 29, expected a keyword with less than 20 characters at line 1 column 175"
99
}
1010
]
1111
}

src/tests/krate/publish/snapshots/all__krate__publish__keywords__too_many_keywords.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ expression: response.into_json()
55
{
66
"errors": [
77
{
8-
"detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 203"
8+
"detail": "invalid upload request: invalid length 6, expected at most 5 keywords per crate at line 1 column 175"
99
}
1010
]
1111
}

src/views/krate_publish.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub struct PublishMetadata {
1818
pub vers: EncodableCrateVersion,
1919
pub deps: Vec<EncodableCrateDependency>,
2020
pub features: BTreeMap<EncodableFeatureName, Vec<EncodableFeature>>,
21-
pub description: Option<String>,
2221
pub homepage: Option<String>,
2322
pub documentation: Option<String>,
2423
pub readme: Option<String>,
@@ -27,8 +26,6 @@ pub struct PublishMetadata {
2726
pub keywords: EncodableKeywordList,
2827
#[serde(default)]
2928
pub categories: EncodableCategoryList,
30-
pub license: Option<String>,
31-
pub license_file: Option<String>,
3229
pub repository: Option<String>,
3330
#[serde(default)]
3431
pub links: Option<String>,

0 commit comments

Comments
 (0)