blob: ae50e9885a8c7014476d1d0c712559c2ac4413b6 [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}
17echo ${GERRIT_REFNAME} | grep -q "refs\/tags\/" && lts_branch=${refname%.*} && new_tag=${refname}
18
19function activate_version() {
20 version=$1
Paul Sokolovsky980b3b82024-02-29 20:54:18 +070021 # Convert tag to ReadTheDocs version slug
22 version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
Arthur Shec4b4b5e2024-02-09 21:28:27 -080023 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080024 retry=0
25
Arthur Shec4b4b5e2024-02-09 21:28:27 -080026 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
27 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080028
Arthur Shec4b4b5e2024-02-09 21:28:27 -080029 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080030 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080031 [ ${retry} -gt ${max_retry_time} ] && break
32 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080033 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080034 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
35 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080036 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080037
38 if [ ${retry} -le ${max_retry_time} ]; then
39 echo "Active new version: ${version}"
40 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
41 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
42 else
43 echo "RTD can not find the version: ${version}"
44 exit 1
45 fi
Arthur She8c3dac22024-01-15 20:13:40 -080046}
47
48echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
49build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
50if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070051 # 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 -080052 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070053 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080054 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
55 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080056 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
57 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080058fi
59
60# Triggered by a new tag
61if [ -n "${new_tag}" ]; then
62 echo -e "\nNew release tag: ${new_tag}"
63 # Hide the current active and unhidden tags
64 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 +070065 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -080066 for t in ${old_tags};
67 do
68 echo "Hide old tag: ${t}"
69 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070070 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -080071 done
72 # Active the new version
73 echo "Active new version: ${new_tag}"
74 activate_version ${new_tag}
75fi
76