m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-20 19:03:43 -05:00

feat: take remote tags into account when excluding based on images.exclude from the config (#148)

This commit is contained in:
Daniel Rocha
2025-11-20 21:54:19 +01:00
committed by GitHub
parent c745a249f5
commit 5ad8c315a5
3 changed files with 49 additions and 4 deletions

View File

@@ -7,7 +7,10 @@ use crate::{
http::Client, http::Client,
registry::{check_auth, get_token}, registry::{check_auth, get_token},
structs::{image::Image, update::Update}, structs::{image::Image, update::Update},
utils::request::{get_response_body, parse_json}, utils::{
reference::split,
request::{get_response_body, parse_json},
},
Context, Context,
}; };
@@ -79,6 +82,21 @@ async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Ve
remote_images remote_images
} }
/// Returns a list of excluded tag prefixes for the given image.
fn get_excluded_tags(image: &Image, ctx: &Context) -> Vec<String> {
let image_name = image.reference.split(':').next().unwrap();
ctx.config
.images
.exclude
.iter()
.filter(|item| item.starts_with(image_name))
.filter_map(|excluded| {
let tag = split(excluded).2;
(tag != "latest").then_some(tag)
})
.collect()
}
/// Returns a list of updates for all images passed in. /// Returns a list of updates for all images passed in.
pub async fn get_updates( pub async fn get_updates(
references: &Option<Vec<String>>, // If a user requested _specific_ references to be checked, this will have a value references: &Option<Vec<String>>, // If a user requested _specific_ references to be checked, this will have a value
@@ -200,8 +218,9 @@ pub async fn get_updates(
.iter() .iter()
.any(|item| image.reference.starts_with(item)); .any(|item| image.reference.starts_with(item));
if !is_ignored { if !is_ignored {
let excluded_tags = get_excluded_tags(image, ctx);
let token = tokens.get(image.parts.registry.as_str()).unwrap(); let token = tokens.get(image.parts.registry.as_str()).unwrap();
let future = image.check(token.as_deref(), ctx, &client); let future = image.check(token.as_deref(), ctx, &client, excluded_tags);
handles.push(future); handles.push(future);
} }
} }

View File

@@ -122,6 +122,7 @@ pub async fn get_latest_tag(
token: Option<&str>, token: Option<&str>,
ctx: &Context, ctx: &Context,
client: &Client, client: &Client,
excluded_tags: Vec<String>,
) -> Image { ) -> Image {
ctx.logger ctx.logger
.debug(format!("Checking for tag update to {}", image.reference)); .debug(format!("Checking for tag update to {}", image.reference));
@@ -153,6 +154,7 @@ pub async fn get_latest_tag(
&image.version_info.as_ref().unwrap().format_str, &image.version_info.as_ref().unwrap().format_str,
ctx, ctx,
client, client,
&excluded_tags,
) )
.await .await
{ {
@@ -211,6 +213,20 @@ pub async fn get_latest_tag(
} }
} }
/// Checks if a tag matches any of the excluded tag prefixes.
fn is_excluded_tag(tag: &str, excluded_tags: &[String], ctx: &Context) -> bool {
for excluded in excluded_tags {
if tag.starts_with(excluded) {
ctx.logger.debug(format!(
"Ignoring tag \"{}\" as it matches excluded prefix \"{}\"",
tag, excluded
));
return true;
}
}
false
}
pub async fn get_extra_tags( pub async fn get_extra_tags(
url: &str, url: &str,
headers: &[(&str, Option<&str>)], headers: &[(&str, Option<&str>)],
@@ -218,6 +234,7 @@ pub async fn get_extra_tags(
format_str: &str, format_str: &str,
ctx: &Context, ctx: &Context,
client: &Client, client: &Client,
excluded_tags: &[String],
) -> Result<(Vec<Version>, Option<String>), String> { ) -> Result<(Vec<Version>, Option<String>), String> {
let response = client.get(url, headers, false).await; let response = client.get(url, headers, false).await;
@@ -232,6 +249,7 @@ pub async fn get_extra_tags(
.as_array() .as_array()
.unwrap() .unwrap()
.iter() .iter()
.filter(|tag| !is_excluded_tag(tag.as_str().unwrap(), excluded_tags, ctx))
.filter_map(|tag| Version::from_tag(tag.as_str().unwrap())) .filter_map(|tag| Version::from_tag(tag.as_str().unwrap()))
.filter(|(tag, format_string)| match (base.minor, tag.minor) { .filter(|(tag, format_string)| match (base.minor, tag.minor) {
(Some(_), Some(_)) | (None, None) => { (Some(_), Some(_)) | (None, None) => {

View File

@@ -228,9 +228,17 @@ impl Image {
} }
/// Checks if the image has an update /// Checks if the image has an update
pub async fn check(&self, token: Option<&str>, ctx: &Context, client: &Client) -> Self { pub async fn check(
&self,
token: Option<&str>,
ctx: &Context,
client: &Client,
excluded_tags: Vec<String>,
) -> Self {
match &self.version_info { match &self.version_info {
Some(data) => get_latest_tag(self, &data.current_tag, token, ctx, client).await, Some(data) => {
get_latest_tag(self, &data.current_tag, token, ctx, client, excluded_tags).await
}
None => match self.digest_info { None => match self.digest_info {
Some(_) => get_latest_digest(self, token, ctx, client).await, Some(_) => get_latest_digest(self, token, ctx, client).await,
None => unreachable!(), None => unreachable!(),