Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | set -ex |
| 3 | |
| 4 | echo "########################################################################" |
| 5 | echo " Gerrit Environment" |
| 6 | env |grep '^GERRIT' |
| 7 | echo "########################################################################" |
| 8 | |
| 9 | RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/" |
| 10 | RTD_VER_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a/versions/" |
| 11 | RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET} |
| 12 | RTD_API_TOKEN=${RTD_API_TOKEN} |
| 13 | |
| 14 | new_tag="" |
| 15 | refname=${GERRIT_REFNAME##*/} |
| 16 | lts_branch=${refname} |
| 17 | echo ${GERRIT_REFNAME} | grep -q "refs\/tags\/" && lts_branch=${refname%.*} && new_tag=${refname} |
| 18 | |
| 19 | function activate_version() { |
| 20 | version=$1 |
| 21 | retry=0 |
| 22 | |
| 23 | status=$(curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
| 24 | -d "{\"active\": true}" ${RTD_VER_API}/${version}/ | jq .detail) |
| 25 | |
| 26 | while [ "${status}" = "\"Not found.\"" ]; |
| 27 | do |
| 28 | [ ${retry} -gt 5 ] && echo "RTD can not find the version: ${new_tag}" && exit 1 |
| 29 | sleep 10 |
| 30 | status=$(curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
| 31 | -d "{\"active\": true}" ${RTD_VER_API}/${version}/ | jq .detail) |
| 32 | retry=$((retry+1)) |
| 33 | done |
| 34 | } |
| 35 | |
| 36 | echo "Notifying ReadTheDocs of changes on: ${lts_branch}" |
| 37 | build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered) |
| 38 | if [ "${build_trigger}" = "false" ]; then |
| 39 | # The branch might be new and hasn't been known by RTD, or hasn't been activated, or both |
| 40 | # we can trigger a build for the master branch to update all versions |
| 41 | echo "The branch ${lts_branch} has not been activated! Activate it!" |
| 42 | curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} |
| 43 | activate_version ${lts_branch} |
| 44 | fi |
| 45 | |
| 46 | # Triggered by a new tag |
| 47 | if [ -n "${new_tag}" ]; then |
| 48 | echo -e "\nNew release tag: ${new_tag}" |
| 49 | # Hide the current active and unhidden tags |
| 50 | old_tags=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_VER_API}/?slug=${lts_branch}&type=tag&active=true" | \ |
| 51 | jq -r '.results | map(select(.hidden == false) | .verbose_name) | .[]') |
| 52 | for t in ${old_tags}; |
| 53 | do |
| 54 | echo "Hide old tag: ${t}" |
| 55 | curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
| 56 | -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/ |
| 57 | done |
| 58 | # Active the new version |
| 59 | echo "Active new version: ${new_tag}" |
| 60 | activate_version ${new_tag} |
| 61 | fi |
| 62 | |