🏡 index : ~doyle/gitlab-cargo-shim.git

author Alex Butler <alexheretic@gmail.com> 2024-07-05 14:34:25.0 +01:00:00
committer Alex Butler <alexheretic@gmail.com> 2024-07-05 14:34:25.0 +01:00:00
commit
a49414d1848c242bbeeace681d340b30bad2aae7 [patch]
tree
866277e1621f343bd7ef0f6e9fc154394c0ab9dc
parent
e0ead53e576758e2f2fc84e70dbf4685e6132a70
download
a49414d1848c242bbeeace681d340b30bad2aae7.tar.gz

Fix metadata::transform picking wrong package when multiple versions are in metadata



Diff

 src/main.rs     | 24 +++++++++++++++---------
 src/metadata.rs | 10 ++++++----
 2 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 2897836..81aa26b 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -12,8 +12,8 @@
pub mod providers;
pub mod util;

use crate::cache::{Cache, ConcreteCache, Yoked};
use crate::{
    cache::{Cache, ConcreteCache, Yoked},
    config::Args,
    metadata::{CargoConfig, CargoIndexCrateMetadata},
    providers::{gitlab::Gitlab, PackageProvider, Release, User, UserProvider},
@@ -30,8 +30,8 @@
    low_level::{HashOutput, PackFileEntry},
    PktLine,
};
use semver::Version;
use std::{
    borrow::Cow,
    fmt::Write,
    net::{SocketAddr, SocketAddrV6},
    pin::Pin,
@@ -243,15 +243,20 @@
        crate_version: &str,
        do_as: &Arc<User>,
    ) -> anyhow::Result<Yoked<CargoIndexCrateMetadata<'static>>> {
        let crate_v: Option<Version> = crate_version.parse().ok();
        let crate_v = crate_v.as_ref();

        if let Some(cache) = cache
            .get::<CargoIndexCrateMetadata<'static>>(checksum)
            .await?
        {
            debug!("Using metadata from cache");
            return Ok(cache);
            if crate_v.is_none() || crate_v == Some(&cache.get().vers) {
                debug!(crate_name, crate_version, "Using metadata from cache");
                return Ok(cache);
            }
        }

        debug!("Fetching metadata from GitLab");
        debug!(crate_name, crate_version, "Fetching metadata from GitLab");

        // fetch metadata from the provider
        let metadata = gitlab
@@ -261,7 +266,7 @@
        // transform the `cargo metadata` output to the cargo index
        // format
        let cksum = checksum.to_string();
        let metadata = metadata::transform(metadata, crate_name, cksum)
        let metadata = metadata::transform(metadata, crate_name, crate_v, cksum)
            .ok_or_else(|| anyhow!("the supplied metadata.json did contain the released crate"))?;

        // cache the transformed value so the next user to pull it
@@ -642,11 +647,4 @@
    }

    res
}

#[derive(Hash, Debug, PartialEq, Eq)]
struct MetadataCacheKey<'a> {
    checksum: Cow<'a, str>,
    crate_name: Cow<'a, str>,
    crate_version: Cow<'a, str>,
}
diff --git a/src/metadata.rs b/src/metadata.rs
index 4d4d140..8a2b7b1 100644
--- a/src/metadata.rs
+++ a/src/metadata.rs
@@ -15,12 +15,12 @@
pub fn transform(
    metadata: cargo_metadata::Metadata,
    crate_name: &str,
    crate_version: Option<&Version>,
    cksum: String,
) -> Option<CargoIndexCrateMetadata<'static>> {
    let package: Package = metadata
        .packages
        .into_iter()
        .find(|v| v.name == crate_name)?;
    let package: Package = metadata.packages.into_iter().find(|v| {
        v.name == crate_name && (crate_version.is_none() || Some(&v.version) == crate_version)
    })?;

    Some(CargoIndexCrateMetadata {
        name: Cow::Owned(package.name),
@@ -71,7 +71,7 @@
pub struct CargoIndexCrateMetadata<'a> {
    #[serde(borrow)]
    name: Cow<'a, str>,
    vers: Version,
    pub vers: Version,
    #[serde(borrow)]
    deps: Vec<CargoIndexCrateMetadataDependency<'a>>,
    cksum: String,