b24b4f
#!/bin/bash
b24b4f
#set -e
b24b4f
rm -f /tmp/pyxis*.json
b24b4f
TOTAL=`curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=1" | jq .total`
b24b4f
if [ "$TOTAL" == "null" ]; then
b24b4f
  echo "Error comunicating with Pyxis API."
b24b4f
  exit 1
b24b4f
fi
b24b4f
PAGES=$(($TOTAL/500))
b24b4f
for P in `seq 0 $PAGES`; do
b24b4f
  curl -s --negotiate -u: -H 'Content-Type: application/json' -H 'Accept: application/json' -X GET "https://pyxis.engineering.redhat.com/v1/repositories?page_size=500&page=$P" > /tmp/pyxis$P.json
b24b4f
done
b24b4f
cat /tmp/pyxis*.json > /tmp/pyx.json
b24b4f
rm -f /tmp/rhel-shortnames.conf
b24b4f
while read -r LINE; do
b24b4f
  if [[ "$LINE" == *\"_id\":* ]] || [[ "$LINE" == *\"total\":* ]]; then
b24b4f
    if [ -z $REGISTRY ] ||
b24b4f
       [ -z $PUBLISHED ] ||
b24b4f
       [ -z $REPOSITORY ] ||
b24b4f
       [  $REPOSITORY == \"\" ] ||
b24b4f
       [ "$AVAILABLE" != "Generally Available" ] ||
b24b4f
       [[ $REPOSITORY == *[@:]* ]] ||
b24b4f
       [[ $REPOSITORY == *[* ]] ||
b24b4f
       [[ "$REGISTRY" == *non_registry* ]] ||
b24b4f
       [[ $REGISTRY != *.* ]]
b24b4f
     then
b24b4f
      continue
b24b4f
    fi
b24b4f
    if [[ $REGISTRY == *quay.io* ]] ||
b24b4f
       [[ $REGISTRY == *redhat.com* ]]; then
b24b4f
      if [ "$REQUIRES_TERMS" == "1" ]; then
b24b4f
        REGISTRY=registry.redhat.io
b24b4f
      fi
b24b4f
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\""
b24b4f
      echo "\"$REPOSITORY\" = \"$REGISTRY/$REPOSITORY\"" >> /tmp/rhel-shortnames.conf
b24b4f
    fi
b24b4f
    REGISTRY=""
b24b4f
    PUBLISHED=""
b24b4f
    AVAILABLE=""
b24b4f
    REPOSITORY=""
b24b4f
    REQUIRES_TERMS=""
b24b4f
    continue
b24b4f
  fi
b24b4f
  if [[ "$LINE" == *\"published\":\ true,* ]]; then
b24b4f
    PUBLISHED=1
b24b4f
  fi
b24b4f
  if [[ "$LINE" == *\"requires_terms\":\ true,* ]]; then
b24b4f
    REQUIRES_TERMS=1
b24b4f
  fi
b24b4f
  if [[ "$LINE" == *\"repository\":\ * ]]; then
b24b4f
    REPOSITORY=`echo $LINE | sed 's,^.* ",,' | sed 's;",$;;'`
b24b4f
  fi
b24b4f
  if [[ "$LINE" == *\"registry\":\ * ]]; then
b24b4f
    REGISTRY=`echo $LINE | sed -e 's,^.*:\ ",,' -e 's,".*,,'`
b24b4f
  fi
b24b4f
  if [[ "$LINE" == *\"release_categories\":\ * ]]; then
b24b4f
    read -r LINE
b24b4f
    AVAILABLE=`echo $LINE | sed 's,",,g'`
b24b4f
  fi
b24b4f
done < /tmp/pyx.json
b24b4f
b24b4f
cp /tmp/rhel-shortnames.conf /tmp/r.conf
b24b4f
for D in `cut -d\  -f1 /tmp/r.conf | sort | uniq -d`; do
b24b4f
  echo $D
b24b4f
  M=`grep ^$D /tmp/r.conf | grep 'redhat.com' | tail -n1`
b24b4f
  [ -z "$M" ] && M=`grep ^$D /tmp/r.conf | tail -n1`
b24b4f
  echo $M
b24b4f
  if [ ! -z "$M" ]; then
b24b4f
    echo "replacing $D with $M"
b24b4f
    grep -v "^$D.*" /tmp/r.conf > /tmp/r2.conf
b24b4f
    echo "$M" >> /tmp/r2.conf
b24b4f
    mv /tmp/r2.conf /tmp/r.conf
b24b4f
  fi
b24b4f
done
b24b4f
b24b4f
echo "[aliases]" > 001-rhel-shortnames-pyxis.conf
b24b4f
sort /tmp/r.conf >> 001-rhel-shortnames-pyxis.conf