blob: 45f93f529c493901660cced8415324033963582d [file] [log] [blame]
Arthur She8c3dac22024-01-15 20:13:40 -08001#!/bin/bash
2set -ex
3
4echo "########################################################################"
5echo " Gerrit Environment"
6env |grep '^GERRIT'
7echo "########################################################################"
8
9RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/"
10RTD_VER_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a/versions/"
11RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET}
12RTD_API_TOKEN=${RTD_API_TOKEN}
13
14new_tag=""
15refname=${GERRIT_REFNAME##*/}
16lts_branch=${refname}
17echo ${GERRIT_REFNAME} | grep -q "refs\/tags\/" && lts_branch=${refname%.*} && new_tag=${refname}
18
19function 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
36echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
37build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
38if [ "${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}
44fi
45
46# Triggered by a new tag
47if [ -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}
61fi
62