Skip to content

Extract cargo-registry-index package #4396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rustdoc-args = [
[dependencies]
anyhow = "=1.0.53"
base64 = "=0.13.0"
cargo-registry-index = { path = "cargo-registry-index" }
cargo-registry-markdown = { path = "cargo-registry-markdown" }
cargo-registry-s3 = { path = "cargo-registry-s3" }
chrono = { version = "=0.4.19", features = ["serde"] }
Expand All @@ -56,7 +57,6 @@ dotenv = "=0.15.0"
flate2 = "=1.0.22"
futures-channel = { version = "=0.3.19", default-features = false }
futures-util = "=0.3.19"
git2 = "=0.13.25"
hex = "=0.4.3"
http = "=0.2.6"
hyper = { version = "=0.14.16", features = ["client", "http1"] }
Expand Down Expand Up @@ -89,6 +89,7 @@ tracing-subscriber = { version = "=0.3.6", features = ["env-filter"] }
url = "=2.2.2"

[dev-dependencies]
cargo-registry-index = { path = "cargo-registry-index", features = ["testing"] }
claim = "=0.5.0"
conduit-test = "=0.10.0"
hyper-tls = "=0.5.0"
Expand Down
24 changes: 24 additions & 0 deletions cargo-registry-index/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "cargo-registry-index"
version = "0.0.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/crates.io"
description = "crates.io package index utilities"
edition = "2018"
resolver = "2"

[lib]
path = "lib.rs"

[features]
testing = ["serde_json"]

[dependencies]
anyhow = "=1.0.53"
base64 = "=0.13.0"
dotenv = "=0.15.0"
git2 = "=0.13.25"
serde = { version = "=1.0.135", features = ["derive"] }
tempfile = "=3.3.0"
url = "=2.2.2"
serde_json = { version = "=1.0.78", optional = true }
16 changes: 14 additions & 2 deletions src/git.rs → cargo-registry-index/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[macro_use]
extern crate serde;

#[cfg(feature = "testing")]
pub mod testing;

use anyhow::{anyhow, Context};
use std::collections::HashMap;
use std::io::Write;
Expand All @@ -7,8 +13,6 @@ use std::process::Command;
use tempfile::TempDir;
use url::Url;

use crate::models::DependencyKind;

static DEFAULT_GIT_SSH_USERNAME: &str = "git";

#[derive(Clone)]
Expand Down Expand Up @@ -148,6 +152,14 @@ pub struct Dependency {
pub package: Option<String>,
}

#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum DependencyKind {
Normal,
Build,
Dev,
}

pub struct RepositoryConfig {
pub index_location: Url,
pub credentials: Credentials,
Expand Down
7 changes: 2 additions & 5 deletions src/tests/git.rs → cargo-registry-index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ impl UpstreamIndex {
}

/// Obtain a list of crates from the index HEAD
pub fn crates_from_index_head(
&self,
crate_name: &str,
) -> anyhow::Result<Vec<cargo_registry::git::Crate>> {
pub fn crates_from_index_head(&self, crate_name: &str) -> anyhow::Result<Vec<crate::Crate>> {
let repo = &self.repository;

let path = cargo_registry::git::Repository::relative_index_file(crate_name);
let path = crate::Repository::relative_index_file(crate_name);

let head = repo.head()?;
let tree = head.peel_to_tree()?;
Expand Down
2 changes: 1 addition & 1 deletion src/background_jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use swirl::PerformError;

use crate::db::{DieselPool, DieselPooledConn, PoolError};
use crate::git::Repository;
use crate::uploaders::Uploader;
use cargo_registry_index::Repository;

impl<'a> swirl::db::BorrowedConnection<'a> for DieselPool {
type Connection = DieselPooledConn<'a>;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/background-worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#![warn(clippy::all, rust_2018_idioms)]

use cargo_registry::config;
use cargo_registry::git::{Repository, RepositoryConfig};
use cargo_registry::{background_jobs::*, db};
use cargo_registry_index::{Repository, RepositoryConfig};
use diesel::r2d2;
use reqwest::blocking::Client;
use std::sync::{Arc, Mutex};
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::sync::Arc;
use swirl::Job;

use crate::controllers::cargo_prelude::*;
use crate::git;
use crate::models::{
insert_version_owner_action, Badge, Category, Crate, DependencyKind, Keyword, NewCrate,
NewVersion, Rights, VersionAction,
Expand Down Expand Up @@ -230,7 +229,7 @@ pub fn publish(req: &mut dyn RequestExt) -> EndpointResult {
};

// Register this crate in our local git repo.
let git_crate = git::Crate {
let git_crate = cargo_registry_index::Crate {
name: name.0,
vers: vers.to_string(),
cksum: hex_cksum,
Expand Down Expand Up @@ -313,7 +312,7 @@ pub fn add_dependencies(
conn: &PgConnection,
deps: &[EncodableCrateDependency],
target_version_id: i32,
) -> AppResult<Vec<git::Dependency>> {
) -> AppResult<Vec<cargo_registry_index::Dependency>> {
use self::dependencies::dsl::*;
use diesel::insert_into;

Expand Down Expand Up @@ -348,14 +347,14 @@ pub fn add_dependencies(
};

Ok((
git::Dependency {
cargo_registry_index::Dependency {
name,
req: dep.version_req.to_string(),
features: dep.features.iter().map(|s| s.0.to_string()).collect(),
optional: dep.optional,
default_features: dep.default_features,
target: dep.target.clone(),
kind: dep.kind.or(Some(DependencyKind::Normal)),
kind: dep.kind.or(Some(DependencyKind::Normal)).map(|dk| dk.into()),
package,
},
(
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub mod config;
pub mod db;
mod downloads_counter;
pub mod email;
pub mod git;
pub mod github;
pub mod metrics;
pub mod middleware;
Expand Down
11 changes: 11 additions & 0 deletions src/models/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use diesel::sql_types::Integer;

use crate::models::{Crate, Version};
use crate::schema::*;
use cargo_registry_index::DependencyKind as IndexDependencyKind;

#[derive(Identifiable, Associations, Debug, Queryable, QueryableByName)]
#[belongs_to(Version)]
Expand Down Expand Up @@ -42,6 +43,16 @@ pub enum DependencyKind {
// if you add a kind here, be sure to update `from_row` below.
}

impl From<DependencyKind> for IndexDependencyKind {
fn from(dk: DependencyKind) -> Self {
match dk {
DependencyKind::Normal => IndexDependencyKind::Normal,
DependencyKind::Build => IndexDependencyKind::Build,
DependencyKind::Dev => IndexDependencyKind::Dev,
}
}
}

impl FromSql<Integer, Pg> for DependencyKind {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
match <i32 as FromSql<Integer, Pg>>::from_sql(bytes)? {
Expand Down
1 change: 0 additions & 1 deletion src/tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ mod builders;
mod categories;
mod category;
mod dump_db;
mod git;
mod keyword;
mod krate;
mod metrics;
Expand Down
13 changes: 4 additions & 9 deletions src/tests/util/test_app.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use super::{MockAnonymousUser, MockCookieUser, MockTokenUser};
use crate::git::UpstreamIndex;
use crate::record;
use crate::util::{chaosproxy::ChaosProxy, fresh_schema::FreshSchema};
use cargo_registry::config;
use cargo_registry::{
background_jobs::Environment,
db::DieselPool,
git::{Credentials, RepositoryConfig},
App, Emails,
};
use cargo_registry::{background_jobs::Environment, db::DieselPool, App, Emails};
use cargo_registry_index::testing::UpstreamIndex;
use cargo_registry_index::{Credentials, Repository as WorkerRepository, RepositoryConfig};
use std::{rc::Rc, sync::Arc, time::Duration};

use cargo_registry::git::Repository as WorkerRepository;
use diesel::PgConnection;
use reqwest::{blocking::Client, Proxy};
use std::collections::HashSet;
Expand Down Expand Up @@ -133,7 +128,7 @@ impl TestApp {
}

/// Obtain a list of crates from the index HEAD
pub fn crates_from_index_head(&self, crate_name: &str) -> Vec<cargo_registry::git::Crate> {
pub fn crates_from_index_head(&self, crate_name: &str) -> Vec<cargo_registry_index::Crate> {
self.upstream_index()
.crates_from_index_head(crate_name)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/worker/git.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::background_jobs::Environment;
use crate::git::Crate;
use crate::schema;
use anyhow::Context;
use cargo_registry_index::Crate;
use chrono::Utc;
use diesel::prelude::*;
use std::fs::{self, OpenOptions};
Expand Down