blob: abdd44896f1d7d3f30d8305820781f85f963aa9a [file] [log] [blame]
#!/bin/bash
set -x
echo "########################################################################"
echo " Gerrit Environment"
env | grep '^GERRIT'
echo "########################################################################"
set -e
if [ "${GERRIT_PROJECT}" == "TF-A/trusted-firmware-a" ]; then
# For real production project, non-sandbox run goes to production RTD project,
# while for sandbox run to a separate RTD project.
if [ "${SANDBOX_RUN}" == "false" ]; then
RTD_PROJECT="trustedfirmware-a"
RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/"
RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET}
RTD_API_TOKEN=${RTD_API_TOKEN}
else
RTD_PROJECT="trustedfirmware-a-sandbox"
RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a-sandbox/263958/"
RTD_WEBHOOK_SECRET_KEY=${TFA_SANDBOX_RTD_WEBHOOK_SECRET}
RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN}
fi
elif [ "${GERRIT_PROJECT}" == "sandbox/pfalcon/trusted-firmware-a" ]; then
# For test project, both "production" and "sandbox" go to the same elsewhere project.
RTD_PROJECT="pfalcon-trustedfirmware-a-sandbox"
RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/pfalcon-trustedfirmware-a-sandbox/263459/"
RTD_WEBHOOK_SECRET_KEY=${PFALCON_RTD_WEBHOOK_SECRET}
RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN}
else
echo "Unknown GERRIT_PROJECT: ${GERRIT_PROJECT}"
exit 1
fi
RTD_API="https://readthedocs.org/api/v3/projects/${RTD_PROJECT}"
RTD_VER_API="${RTD_API}/versions"
new_tag=""
new_slug=""
refname=${GERRIT_REFNAME##*/}
lts_branch=${refname}
if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then
new_tag=${GERRIT_REFNAME#refs/tags/}
# Convert tag to ReadTheDocs version slug
new_slug=$(echo ${new_tag} | tr '[A-Z]/' '[a-z]-')
lts_branch=${refname%.*}
fi
function rtd_rest_api() {
uri="$1"
jqfilt="$2"
resp=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "$uri")
echo $resp 1>&2
echo $resp | jq -r "$jqfilt"
}
function activate_version() {
version=$1
max_retry_time=20
retry=0
# Check whether we need to activate a tag or a branch. If it's a tag, create a branch variable
echo ${version} | grep -q "lts-v[0-9].*\.[0-9].*\.[0-9].*" && branch=${version%.*}
ver_slug=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
jq -r '.slug')
while [ "${ver_slug}" != "${version}" ];
do
[ ${retry} -gt ${max_retry_time} ] && break
sleep 30
retry=$((retry+1))
ver_slug=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
jq -r '.slug')
done
# activate and hide the branch
if [ -n "${branch}" ]; then
curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
-d "{\"active\": true, \"hidden\": true}" ${RTD_VER_API}/${branch}/
fi
if [ ${retry} -le ${max_retry_time} ]; then
echo "Active new version: ${version}"
curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
-d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
else
echo "RTD can not find the version: ${version}"
exit 1
fi
}
function wait_for_build() {
version=$1
retry=0
while true; do
status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_API}/builds/" | \
jq -r ".results | map(select(.version==\"$version\")) | .[0].state.code")
echo $status
if [ "$status" == "finished" ]; then
break
fi
retry=$((retry + 1))
if [ $retry -gt 40 ]; then
echo "Could not confirm that ReadTheDoc slug ${version} was built in the alloted time."
break
fi
sleep 30
done
}
echo "Notifying ReadTheDocs of changes"
curl -s -X POST -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${lts_branch}/builds/
# Triggered by a new tag
if [ -n "${new_tag}" ]; then
echo -e "\nNew release tag: ${new_tag}, slug: ${new_slug}"
# Hide the current active and visible version
old_tags=$(rtd_rest_api "${RTD_VER_API}/?slug=${lts_branch}&type=tag&active=true" '.results | map(select(.hidden == false) | .slug) | .[]')
for t in ${old_tags};
do
echo "Hide old version: ${t}"
curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
-d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
done
# Active the new version
echo "Active new version: ${new_slug}"
activate_version ${new_slug}
wait_for_build ${new_slug}
echo "Docs for the new release are available at: https://${RTD_PROJECT}.readthedocs.io/en/${new_slug}/"
fi