Skip to content

Commit

Permalink
Add rust-version field to the index
Browse files Browse the repository at this point in the history
  • Loading branch information
cassaundra committed Apr 11, 2023
1 parent 6342381 commit 4039572
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cargo-registry-index/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub struct Crate {
pub yanked: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rust_version: Option<String>,
/// The schema version for this entry.
///
/// If this is None, it defaults to version 1. Entries with unknown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE versions DROP COLUMN rust_version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE versions ADD COLUMN rust_version VARCHAR;
3 changes: 3 additions & 0 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
let name = new_crate.name;
let vers = &*new_crate.vers;
let links = new_crate.links;
let rust_version = new_crate.rust_version.as_deref();
let repo = new_crate.repository;
let features = new_crate
.features
Expand Down Expand Up @@ -201,6 +202,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
user.id,
hex_cksum.clone(),
links.clone(),
rust_version.map(String::as_str),
)?
.save(conn, &verified_email_address)?;

Expand Down Expand Up @@ -269,6 +271,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
deps: git_deps,
yanked: Some(false),
links,
rust_version: rust_version.map(ToOwned::to_owned),
v,
};
worker::add_crate(git_crate).enqueue(conn)?;
Expand Down
1 change: 1 addition & 0 deletions src/downloads_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ mod tests {
self.user.id,
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
None,
None,
)
.expect("failed to create version")
.save(conn, "ghost@example.com")
Expand Down
4 changes: 4 additions & 0 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Version {
pub published_by: Option<i32>,
pub checksum: String,
pub links: Option<String>,
pub rust_version: Option<String>,
}

#[derive(Insertable, Debug)]
Expand All @@ -38,6 +39,7 @@ pub struct NewVersion {
published_by: i32,
checksum: String,
links: Option<String>,
rust_version: Option<String>,
}

/// The highest version (semver order) and the most recently updated version.
Expand Down Expand Up @@ -139,6 +141,7 @@ impl NewVersion {
published_by: i32,
checksum: String,
links: Option<String>,
rust_version: Option<&str>,
) -> AppResult<Self> {
let features = serde_json::to_value(features)?;

Expand All @@ -151,6 +154,7 @@ impl NewVersion {
published_by,
checksum,
links,
rust_version: rust_version.map(ToOwned::to_owned),
};

new_version.validate_license(license_file)?;
Expand Down
6 changes: 6 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,12 @@ diesel::table! {
///
/// (Automatically generated by Diesel.)
links -> Nullable<Varchar>,
/// The `rust_version` column of the `versions` table.
///
/// Its SQL type is `Nullable<Varchar>`.
///
/// (Automatically generated by Diesel.)
rust_version -> Nullable<Varchar>,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/builders/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl PublishBuilder {
license_file: self.license_file,
repository: None,
links: None,
rust_version: None,
};

(serde_json::to_string(&new_crate).unwrap(), self.tarball)
Expand Down
9 changes: 9 additions & 0 deletions src/tests/builders/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct VersionBuilder<'a> {
yanked: bool,
checksum: String,
links: Option<String>,
rust_version: Option<String>,
}

impl<'a> VersionBuilder<'a> {
Expand All @@ -45,6 +46,7 @@ impl<'a> VersionBuilder<'a> {
yanked: false,
checksum: String::new(),
links: None,
rust_version: None,
}
}

Expand Down Expand Up @@ -83,6 +85,12 @@ impl<'a> VersionBuilder<'a> {
self
}

/// Sets the version's `rust_version` value.
pub fn rust_version(mut self, rust_version: &str) -> Self {
self.rust_version = Some(rust_version.to_owned());
self
}

pub fn build(
self,
crate_id: i32,
Expand All @@ -103,6 +111,7 @@ impl<'a> VersionBuilder<'a> {
published_by,
self.checksum,
self.links,
self.rust_version.as_deref(),
)?
.save(connection, "someone@example.com")?;

Expand Down
5 changes: 3 additions & 2 deletions src/tests/krate/versions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::builders::CrateBuilder;
use crate::builders::{CrateBuilder, VersionBuilder};
use crate::util::{RequestHelper, TestApp};
use cargo_registry::schema::versions;
use cargo_registry::views::EncodableVersion;
Expand All @@ -16,7 +16,7 @@ fn versions() {
app.db(|conn| {
CrateBuilder::new("foo_versions", user.id)
.version("0.5.1")
.version("1.0.0")
.version(VersionBuilder::new("1.0.0").rust_version("1.64"))
.version("0.5.0")
.expect_build(conn);
// Make version 1.0.0 mimic a version published before we started recording who published
Expand All @@ -33,6 +33,7 @@ fn versions() {

assert_eq!(json.versions.len(), 3);
assert_eq!(json.versions[0].num, "1.0.0");
assert_eq!(json.versions[0].rust_version, Some("1.64".to_owned()));
assert_eq!(json.versions[1].num, "0.5.1");
assert_eq!(json.versions[2].num, "0.5.0");
assert_none!(&json.versions[0].published_by);
Expand Down
1 change: 1 addition & 0 deletions src/tests/routes/crates/versions/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn show_by_crate_name_and_version() {
VersionBuilder::new("2.0.0")
.size(1234)
.checksum("c241cd77c3723ccf1aa453f169ee60c0a888344da504bee0142adb859092acb4")
.rust_version("1.64")
.expect_build(krate.id, user.id, conn)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ version:
num: 1.0.0
published_by: ~
readme_path: /api/v1/crates/foo_vers_show_no_pb/1.0.0/readme
rust_version: ~
updated_at: "[datetime]"
yanked: false

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: src/tests/routes/crates/versions/read.rs
assertion_line: 22
assertion_line: 23
expression: json
---
version:
Expand All @@ -26,6 +26,7 @@ version:
name: ~
url: "https://github.com/foo"
readme_path: /api/v1/crates/foo_vers_show/2.0.0/readme
rust_version: "1.64"
updated_at: "[datetime]"
yanked: false

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ versions:
name: ~
url: "https://github.com/foo"
readme_path: /api/v1/crates/foo_vers_index/2.0.0/readme
rust_version: ~
updated_at: "[datetime]"
yanked: false
- audit_actions: []
Expand All @@ -50,6 +51,7 @@ versions:
name: ~
url: "https://github.com/foo"
readme_path: /api/v1/crates/foo_vers_index/2.0.1/readme
rust_version: ~
updated_at: "[datetime]"
yanked: false

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ version:
name: ~
url: "https://github.com/foo"
readme_path: /api/v1/crates/foo_vers_show_id/2.0.0/readme
rust_version: ~
updated_at: "[datetime]"
yanked: false

4 changes: 4 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ pub struct EncodableVersion {
pub published_by: Option<EncodablePublicUser>,
pub audit_actions: Vec<EncodableAuditAction>,
pub checksum: String,
pub rust_version: Option<String>,
}

impl EncodableVersion {
Expand All @@ -633,6 +634,7 @@ impl EncodableVersion {
license,
crate_size,
checksum,
rust_version,
..
} = version;

Expand All @@ -657,6 +659,7 @@ impl EncodableVersion {
links,
crate_size,
checksum,
rust_version,
published_by: published_by.map(User::into),
audit_actions: audit_actions
.into_iter()
Expand Down Expand Up @@ -780,6 +783,7 @@ mod tests {
},
crate_size: Some(1234),
checksum: String::new(),
rust_version: None,
published_by: None,
audit_actions: vec![EncodableAuditAction {
action: "publish".to_string(),
Expand Down
30 changes: 30 additions & 0 deletions src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct EncodableCrateUpload {
pub repository: Option<String>,
#[serde(default)]
pub links: Option<String>,
#[serde(default)]
pub rust_version: Option<EncodableRustVersion>,
}

#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
Expand All @@ -42,6 +44,8 @@ pub struct EncodableDependencyName(pub String);
pub struct EncodableCrateVersion(pub semver::Version);
#[derive(Debug, Deref)]
pub struct EncodableCrateVersionReq(pub String);
#[derive(Debug, Deref, Clone)]
pub struct EncodableRustVersion(pub String);
#[derive(Serialize, Debug, Deref, Default)]
pub struct EncodableKeywordList(pub Vec<EncodableKeyword>);
#[derive(Serialize, Debug, Deref)]
Expand Down Expand Up @@ -177,6 +181,23 @@ impl<'de> Deserialize<'de> for EncodableCrateVersionReq {
}
}

impl<'de> Deserialize<'de> for EncodableRustVersion {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableRustVersion, D::Error> {
let s = String::deserialize(d)?;
match semver::VersionReq::parse(&s) {
// Exclude semver operators like `^` and pre-release identifiers
Ok(_) if s.chars().all(|c| c.is_ascii_digit() || c == '.') => {
Ok(EncodableRustVersion(s))
}
Ok(_) | Err(..) => {
let value = de::Unexpected::Str(&s);
let expected = "a valid rust-version";
Err(de::Error::invalid_value(value, &expected))
}
}
}
}

impl<'de> Deserialize<'de> for EncodableKeywordList {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableKeywordList, D::Error> {
let inner = <Vec<EncodableKeyword> as Deserialize<'de>>::deserialize(d)?;
Expand Down Expand Up @@ -224,6 +245,15 @@ impl Serialize for EncodableCrateVersionReq {
}
}

impl Serialize for EncodableRustVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&(**self).to_string())
}
}

use diesel::pg::Pg;
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::Text;
Expand Down
1 change: 1 addition & 0 deletions src/worker/dump_db/dump-db.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ crate_size = "public"
published_by = "public"
checksum = "public"
links = "public"
rust_version = "public"

[versions_published_by.columns]
version_id = "private"
Expand Down
1 change: 1 addition & 0 deletions src/worker/update_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod test {
user_id,
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
None,
None,
)
.unwrap();
let version = version.save(conn, "someone@example.com").unwrap();
Expand Down

0 comments on commit 4039572

Please sign in to comment.