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