blob: dc35b974bbb9846ff6092d07090dcb711ae5e186 [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/"
Arthur Shec4b4b5e2024-02-09 21:28:27 -080010RTD_VER_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a/versions"
Arthur She8c3dac22024-01-15 20:13:40 -080011RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET}
12RTD_API_TOKEN=${RTD_API_TOKEN}
13
14new_tag=""
15refname=${GERRIT_REFNAME##*/}
16lts_branch=${refname}
Paul Sokolovsky07bd0952024-03-01 16:14:14 +070017if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then
18 new_tag=${GERRIT_REFNAME#refs/tags/}
19 lts_branch=${refname%.*}
20fi
Arthur She8c3dac22024-01-15 20:13:40 -080021
22function activate_version() {
23 version=$1
Paul Sokolovsky980b3b82024-02-29 20:54:18 +070024 # Convert tag to ReadTheDocs version slug
25 version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
Arthur Shec4b4b5e2024-02-09 21:28:27 -080026 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080027 retry=0
28
Arthur Shec4b4b5e2024-02-09 21:28:27 -080029 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
30 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080031
Arthur Shec4b4b5e2024-02-09 21:28:27 -080032 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080033 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080034 [ ${retry} -gt ${max_retry_time} ] && break
35 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080036 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080037 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
38 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080039 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080040
41 if [ ${retry} -le ${max_retry_time} ]; then
42 echo "Active new version: ${version}"
43 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
44 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
45 else
46 echo "RTD can not find the version: ${version}"
47 exit 1
48 fi
Arthur She8c3dac22024-01-15 20:13:40 -080049}
50
51echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
52build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
53if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070054 # The branch might be new and hasn't been known by RTD, or hasn't been activated, or both
Arthur She4c341b22024-01-30 20:13:43 -080055 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070056 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080057 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
58 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080059 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
60 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080061fi
62
63# Triggered by a new tag
64if [ -n "${new_tag}" ]; then
65 echo -e "\nNew release tag: ${new_tag}"
66 # Hide the current active and unhidden tags
67 old_tags=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_VER_API}/?slug=${lts_branch}&type=tag&active=true" | \
Paul Sokolovskya3138302024-02-27 18:51:47 +070068 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -080069 for t in ${old_tags};
70 do
71 echo "Hide old tag: ${t}"
72 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070073 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -080074 done
75 # Active the new version
76 echo "Active new version: ${new_tag}"
77 activate_version ${new_tag}
78fi
79