blob: 654c03e3cbe4e8b485e5b43f09d9fd1776d446ec [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
Arthur Shec4b4b5e2024-02-09 21:28:27 -080021 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080022 retry=0
23
Arthur Shec4b4b5e2024-02-09 21:28:27 -080024 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
25 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080026
Arthur Shec4b4b5e2024-02-09 21:28:27 -080027 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080028 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080029 [ ${retry} -gt ${max_retry_time} ] && break
30 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080031 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080032 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
33 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080034 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080035
36 if [ ${retry} -le ${max_retry_time} ]; then
37 echo "Active new version: ${version}"
38 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
39 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
40 else
41 echo "RTD can not find the version: ${version}"
42 exit 1
43 fi
Arthur She8c3dac22024-01-15 20:13:40 -080044}
45
46echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
47build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
48if [ "${build_trigger}" = "false" ]; then
49 # 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 -080050 # we can trigger a build for the master branch to update all branches
51 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080052 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
53 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080054 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
55 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080056fi
57
58# Triggered by a new tag
59if [ -n "${new_tag}" ]; then
60 echo -e "\nNew release tag: ${new_tag}"
61 # Hide the current active and unhidden tags
62 old_tags=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_VER_API}/?slug=${lts_branch}&type=tag&active=true" | \
63 jq -r '.results | map(select(.hidden == false) | .verbose_name) | .[]')
64 for t in ${old_tags};
65 do
66 echo "Hide old tag: ${t}"
67 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
68 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
69 done
70 # Active the new version
71 echo "Active new version: ${new_tag}"
72 activate_version ${new_tag}
73fi
74