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 | |
Paul Sokolovsky | 6338f10 | 2024-03-01 16:26:15 +0700 | [diff] [blame^] | 9 | if [ "${GERRIT_PROJECT}" == "TF-A/trusted-firmware-a" ]; then |
| 10 | # For real production project, non-sandbox run goes to production RTD project, |
| 11 | # while for sandbox run to a separate RTD project. |
| 12 | if [ "${SANDBOX_RUN}" == "false" ]; then |
| 13 | RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/" |
| 14 | RTD_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a" |
| 15 | RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET} |
| 16 | RTD_API_TOKEN=${RTD_API_TOKEN} |
| 17 | else |
| 18 | RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a-sandbox/263958/" |
| 19 | RTD_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a-sandbox" |
| 20 | RTD_WEBHOOK_SECRET_KEY=${TFA_SANDBOX_RTD_WEBHOOK_SECRET} |
| 21 | RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN} |
| 22 | fi |
| 23 | elif [ "${GERRIT_PROJECT}" == "sandbox/pfalcon/trusted-firmware-a" ]; then |
| 24 | # For test project, both "production" and "sandbox" go to the same elsewhere project. |
| 25 | RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/pfalcon-trustedfirmware-a-sandbox/263459/" |
| 26 | RTD_API="https://readthedocs.org/api/v3/projects/pfalcon-trustedfirmware-a-sandbox" |
| 27 | RTD_WEBHOOK_SECRET_KEY=${PFALCON_RTD_WEBHOOK_SECRET} |
| 28 | RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN} |
| 29 | else |
| 30 | echo "Unknown GERRIT_PROJECT: ${GERRIT_PROJECT}" |
| 31 | exit 1 |
| 32 | fi |
| 33 | |
| 34 | RTD_VER_API="${RTD_API}/versions" |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 35 | |
| 36 | new_tag="" |
| 37 | refname=${GERRIT_REFNAME##*/} |
| 38 | lts_branch=${refname} |
Paul Sokolovsky | 07bd095 | 2024-03-01 16:14:14 +0700 | [diff] [blame] | 39 | if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then |
| 40 | new_tag=${GERRIT_REFNAME#refs/tags/} |
| 41 | lts_branch=${refname%.*} |
| 42 | fi |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 43 | |
| 44 | function activate_version() { |
| 45 | version=$1 |
Paul Sokolovsky | 980b3b8 | 2024-02-29 20:54:18 +0700 | [diff] [blame] | 46 | # Convert tag to ReadTheDocs version slug |
| 47 | version=$(echo ${version} | tr '[A-Z]/' '[a-z]-') |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 48 | max_retry_time=20 |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 49 | retry=0 |
| 50 | |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 51 | ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \ |
| 52 | jq -r '.detail') |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 53 | |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 54 | while [ "${ver_status}" == "Not found." ]; |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 55 | do |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 56 | [ ${retry} -gt ${max_retry_time} ] && break |
| 57 | sleep 30 |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 58 | retry=$((retry+1)) |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 59 | ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \ |
| 60 | jq -r '.detail') |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 61 | done |
Arthur She | c4b4b5e | 2024-02-09 21:28:27 -0800 | [diff] [blame] | 62 | |
| 63 | if [ ${retry} -le ${max_retry_time} ]; then |
| 64 | echo "Active new version: ${version}" |
| 65 | curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
| 66 | -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/ |
| 67 | else |
| 68 | echo "RTD can not find the version: ${version}" |
| 69 | exit 1 |
| 70 | fi |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | echo "Notifying ReadTheDocs of changes on: ${lts_branch}" |
| 74 | build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered) |
| 75 | if [ "${build_trigger}" = "false" ]; then |
Paul Sokolovsky | 92151ad | 2024-02-26 12:38:32 +0700 | [diff] [blame] | 76 | # The branch might be new and hasn't been known by RTD, or hasn't been activated, or both |
Arthur She | 4c341b2 | 2024-01-30 20:13:43 -0800 | [diff] [blame] | 77 | # we can trigger a build for the master branch to update all branches |
Paul Sokolovsky | 92151ad | 2024-02-26 12:38:32 +0700 | [diff] [blame] | 78 | echo "The branch ${lts_branch} is now! Activate and hide it!" |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 79 | curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} |
| 80 | activate_version ${lts_branch} |
Arthur She | 4c341b2 | 2024-01-30 20:13:43 -0800 | [diff] [blame] | 81 | curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
| 82 | -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/ |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 83 | fi |
| 84 | |
| 85 | # Triggered by a new tag |
| 86 | if [ -n "${new_tag}" ]; then |
| 87 | echo -e "\nNew release tag: ${new_tag}" |
| 88 | # Hide the current active and unhidden tags |
| 89 | old_tags=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_VER_API}/?slug=${lts_branch}&type=tag&active=true" | \ |
Paul Sokolovsky | a313830 | 2024-02-27 18:51:47 +0700 | [diff] [blame] | 90 | jq -r '.results | map(select(.hidden == false) | .slug) | .[]') |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 91 | for t in ${old_tags}; |
| 92 | do |
| 93 | echo "Hide old tag: ${t}" |
| 94 | curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \ |
Paul Sokolovsky | 92151ad | 2024-02-26 12:38:32 +0700 | [diff] [blame] | 95 | -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/ |
Arthur She | 8c3dac2 | 2024-01-15 20:13:40 -0800 | [diff] [blame] | 96 | done |
| 97 | # Active the new version |
| 98 | echo "Active new version: ${new_tag}" |
| 99 | activate_version ${new_tag} |
| 100 | fi |
| 101 | |