Skip to content

Commit

Permalink
feat: implement deprecation flag on packages
Browse files Browse the repository at this point in the history
  • Loading branch information
woutersl committed Sep 10, 2024
1 parent 1e59623 commit 59aca50
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 70 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,26 +418,19 @@ impl Application {

/// Gets all the data about a crate
pub async fn get_crate_info(&self, auth_data: &AuthData, package: &str) -> Result<CrateInfo, ApiError> {
let (versions, targets) = self
let info = self
.db_transaction_read(|app| async move {
let _authentication = app.authenticate(auth_data).await?;
let versions = app
.database
.get_crate_versions(package, self.service_index.get_crate_data(package).await?)
.await?;
let targets = app.database.get_crate_targets(package).await?;
Ok::<_, ApiError>((versions, targets))
app.database
.get_crate_info(package, self.service_index.get_crate_data(package).await?)
.await
})
.await?;
let metadata = self
.service_storage
.download_crate_metadata(package, &versions.last().unwrap().index.vers)
.download_crate_metadata(package, &info.versions.last().unwrap().index.vers)
.await?;
Ok(CrateInfo {
metadata,
versions,
targets,
})
Ok(CrateInfo { metadata, ..info })
}

/// Downloads the last README for a crate
Expand Down Expand Up @@ -649,6 +642,16 @@ impl Application {
.await
}

/// Sets the deprecation status on a crate
pub async fn set_crate_deprecation(&self, auth_data: &AuthData, package: &str, deprecated: bool) -> Result<(), ApiError> {
self.db_transaction_write("set_crate_deprecation", |app| async move {
let authentication = app.authenticate(auth_data).await?;
app.check_can_manage_crate(&authentication, package).await?;
app.database.set_crate_deprecation(package, deprecated).await
})
.await
}

/// Gets the global statistics for the registry
pub async fn get_crates_stats(&self, auth_data: &AuthData) -> Result<GlobalStats, ApiError> {
self.db_transaction_read(|app| async move {
Expand All @@ -664,10 +667,11 @@ impl Application {
auth_data: &AuthData,
query: &str,
per_page: Option<usize>,
deprecated: Option<bool>,
) -> Result<SearchResults, ApiError> {
self.db_transaction_read(|app| async move {
let _authentication = app.authenticate(auth_data).await?;
app.database.search_crates(query, per_page).await
app.database.search_crates(query, per_page, deprecated).await
})
.await
}
Expand Down
Binary file modified src/empty.db
Binary file not shown.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ async fn main_serve_app(application: Arc<Application>, cookie_key: Key) -> Resul
.route("/:package/owners", put(routes::api_v1_cargo_add_crate_owners))
.route("/:package/owners", delete(routes::api_v1_cargo_remove_crate_owners))
.route("/:package/targets", get(routes::api_v1_get_crate_targets))
.route("/:package/targets", patch(routes::api_v1_set_crate_targets)),
.route("/:package/targets", patch(routes::api_v1_set_crate_targets))
.route("/:package/deprecated", patch(routes::api_v1_set_crate_deprecation)),
),
)
// fall back to serving the index
Expand Down
4 changes: 4 additions & 0 deletions src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const MIGRATIONS: &[Migration<'static>] = &[
target: "1.7.1",
content: MigrationContent::Sql(include_bytes!("v1.7.1.sql")),
},
Migration {
target: "1.8.0",
content: MigrationContent::Sql(include_bytes!("v1.8.0.sql")),
},
];

/// Gets the value for the metadata item
Expand Down
2 changes: 2 additions & 0 deletions src/migrations/v1.8.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE Package
ADD COLUMN isDeprecated BOOLEAN NOT NULL DEFAULT FALSE;
3 changes: 3 additions & 0 deletions src/model/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct SearchResultCrate {
pub name: String,
/// The highest version available
pub max_version: String,
/// Whether the entire package is deprecated
#[serde(rename = "isDeprecated")]
pub is_deprecated: bool,
/// Textual description of the crate
pub description: String,
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub struct CrateVersionDepsCheckState {
pub name: String,
/// The crate's version
pub version: String,
/// Whether the entire package is deprecated
#[serde(rename = "isDeprecated")]
pub is_deprecated: bool,
/// Whether the version has outdated dependencies
#[serde(rename = "depsHasOutdated")]
pub deps_has_outdated: bool,
Expand Down
3 changes: 3 additions & 0 deletions src/model/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use super::cargo::{CrateMetadata, IndexCrateMetadata, RegistryUser};
pub struct CrateInfo {
/// The last metadata, if any
pub metadata: Option<CrateMetadata>,
/// Whether the entire package is deprecated
#[serde(rename = "isDeprecated")]
pub is_deprecated: bool,
/// Gets the versions in the index
pub versions: Vec<CrateInfoVersion>,
/// The build targets to use (for docs generation and deps analysis)
Expand Down
18 changes: 17 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,20 @@ pub async fn api_v1_reactivate_user(
pub struct SearchForm {
q: String,
per_page: Option<usize>,
deprecated: Option<bool>,
}

pub async fn api_v1_cargo_search(
auth_data: AuthData,
State(state): State<Arc<AxumState>>,
form: Query<SearchForm>,
) -> ApiResult<SearchResults> {
response(state.application.search_crates(&auth_data, &form.q, form.per_page).await)
response(
state
.application
.search_crates(&auth_data, &form.q, form.per_page, form.deprecated)
.await,
)
}

/// Gets the global statistics for the registry
Expand Down Expand Up @@ -670,6 +676,16 @@ pub async fn api_v1_set_crate_targets(
response(state.application.set_crate_targets(&auth_data, &package, &input).await)
}

/// Sets the deprecation status on a crate
pub async fn api_v1_set_crate_deprecation(
auth_data: AuthData,
State(state): State<Arc<AxumState>>,
Path(PathInfoCrate { package }): Path<PathInfoCrate>,
input: Json<bool>,
) -> ApiResult<()> {
response(state.application.set_crate_deprecation(&auth_data, &package, input.0).await)
}

pub async fn index_serve_inner(
index: &(dyn Index + Send + Sync),
path: &str,
Expand Down
5 changes: 3 additions & 2 deletions src/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS SchemaMetadata (

CREATE INDEX IF NOT EXISTS SchemaMetadataIndex ON SchemaMetadata(name);

INSERT INTO SchemaMetadata VALUES ('version', '1.7.1');
INSERT INTO SchemaMetadata VALUES ('version', '1.8.0');

CREATE TABLE RegistryUser (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Expand Down Expand Up @@ -41,7 +41,8 @@ CREATE TABLE RegistryGlobalToken (
CREATE TABLE Package (
name TEXT NOT NULL PRIMARY KEY,
lowercase TEXT NOT NULL,
targets TEXT NOT NULL
targets TEXT NOT NULL,
isDeprecated BOOLEAN NOT NULL
);

CREATE INDEX IndexPackage ON Package (name);
Expand Down
Loading

0 comments on commit 59aca50

Please sign in to comment.