Skip to content

Commit

Permalink
Merge pull request #178 from rylev/referrers
Browse files Browse the repository at this point in the history
  • Loading branch information
thomastaylor312 authored Oct 23, 2024
2 parents 40e3075 + f2250d2 commit 19f589e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,32 @@ impl Client {
ret
}

/// Pulls the referrers for the given image filtering by the optionally provided artifact type.
pub async fn pull_referrers(
&self,
image: &Reference,
artifact_type: Option<&str>,
) -> Result<OciImageIndex> {
let url = self.to_v2_referrers_url(image, artifact_type)?;
debug!("Pulling referrers from {}", url);

let res = RequestBuilderWrapper::from_client(self, |client| client.get(&url))
.apply_accept(MIME_TYPES_DISTRIBUTION_MANIFEST)?
.apply_auth(image, RegistryOperation::Pull)
.await?
.into_request_builder()
.send()
.await?;
let status = res.status();
let body = res.bytes().await?;

validate_registry_response(status, &body, &url)?;
let manifest = serde_json::from_slice(&body)
.map_err(|e| OciDistributionError::ManifestParsingError(e.to_string()))?;

Ok(manifest)
}

async fn extract_location_header(
&self,
image: &Reference,
Expand Down Expand Up @@ -1587,6 +1613,30 @@ impl Client {
.unwrap_or_default(),
)
}

/// Convert a Reference to a v2 manifest URL.
fn to_v2_referrers_url(
&self,
reference: &Reference,
artifact_type: Option<&str>,
) -> Result<String> {
let registry = reference.resolve_registry();
Ok(format!(
"{scheme}://{registry}/v2/{repository}/referrers/{reference}{at}",
scheme = self.config.protocol.scheme_for(registry),
repository = reference.repository(),
reference = if let Some(digest) = reference.digest() {
digest
} else {
return Err(OciDistributionError::GenericError(Some(
"Getting referrers for a tag is not supported".into(),
)));
},
at = artifact_type
.map(|at| format!("?artifactType={at}"))
.unwrap_or_default(),
))
}
}

/// The OCI spec technically does not allow any codes but 200, 500, 401, and 404.
Expand Down

0 comments on commit 19f589e

Please sign in to comment.