Skip to content
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

chore(updatecli): restore ubi9 manifest (RedHat Container Registry's json file is now valid) #901

Merged
merged 10 commits into from
Oct 31, 2024
77 changes: 77 additions & 0 deletions updatecli/scripts/ubi9-tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# This script fetches all tags from the Red Hat Container Catalog API for UBI9 images.
# It ensures that `jq` and `curl` are installed, fetches the tags, and processes them to find all unique tags.

# Correct URL of the Red Hat Container Catalog API for UBI9
URL="https://catalog.redhat.com/api/containers/v1/repositories/registry/registry.access.redhat.com/repository/ubi9/images?page_size=100&page=0&sort_by=last_update_date%5Bdesc%5D"

# Check if jq and curl are installed
if ! command -v jq >/dev/null 2>&1 || ! command -v curl >/dev/null 2>&1; then
echo "jq and curl are required but not installed. Exiting with status 1." >&2
exit 1
fi

# Fetch the tags using curl
response=$(curl -s "$URL" -H 'accept: application/json')

# Check if the response is empty or null
if [ -z "$response" ] || [ "$response" == "null" ]; then
echo "Error: Failed to fetch tags from the Red Hat Container Catalog API."
exit 1
fi

# Parse the JSON response using jq to find all tags and their published dates
all_tags=$(echo "$response" | jq -c '.data[] | {published_date: .repositories[].published_date, tags: .repositories[].signatures[].tags}')

# Check if the all_tags is empty
if [ -z "$all_tags" ]; then
echo "Error: No valid tags found."
exit 1
fi

# Declare an associative array to store tags and their published dates
declare -A tag_dates

# Declare an array to maintain the original order of tags
ordered_tags=()

# Iterate over the parsed JSON to populate the associative array and ordered array
while IFS= read -r line; do
published_date=$(echo "$line" | jq -r '.published_date')
tags=$(echo "$line" | jq -r '.tags[]')
for tag in $tags; do
# Filter tags that contain a hyphen
if [[ $tag == *-* ]]; then
# Update the published_date if the current date is more recent or the same
if [[ -z "${tag_dates[$tag]}" || ! "$published_date" < "${tag_dates[$tag]}" ]]; then
tag_dates[$tag]=$published_date
fi
# Add or replace the tag in the ordered array
IFS='.' read -r part1 part2 _ <<< "$tag"
base_tag="${part1}.${part2}"
# echo "Base tag is $base_tag and tag is $tag"
replaced=false
for i in "${!ordered_tags[@]}"; do
if [[ "${ordered_tags[i]}" == "$base_tag" ]]; then
ordered_tags[i]="$tag"
replaced=true
break
fi
done
if ! $replaced; then
ordered_tags+=("$tag")
fi
fi
done
done <<< "$all_tags"

# Custom sort function with correct order
sort_tags() {
printf "%s\n" "${ordered_tags[@]}" | sort -t '-' -k1,1n -k2,2n -k2.1,2.3n -k2.5,2.12n
}

# Print the sorted array and their corresponding published dates
for tag in $(sort_tags | tac); do
echo "$tag"
done