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