blob: 7e71029aaab9498ea3e0b9dd565573ee1a3efb7c [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
Paul Sokolovsky6338f102024-03-01 16:26:15 +07009if [ "${GERRIT_PROJECT}" == "TF-A/trusted-firmware-a" ]; then
10 # For real production project, non-sandbox run goes to production RTD project,
11 # while for sandbox run to a separate RTD project.
12 if [ "${SANDBOX_RUN}" == "false" ]; then
Paul Sokolovskyfb2d0402024-03-04 20:20:55 +070013 RTD_PROJECT="trustedfirmware-a"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070014 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070015 RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET}
16 RTD_API_TOKEN=${RTD_API_TOKEN}
17 else
Paul Sokolovskyfb2d0402024-03-04 20:20:55 +070018 RTD_PROJECT="trustedfirmware-a-sandbox"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070019 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a-sandbox/263958/"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070020 RTD_WEBHOOK_SECRET_KEY=${TFA_SANDBOX_RTD_WEBHOOK_SECRET}
21 RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN}
22 fi
23elif [ "${GERRIT_PROJECT}" == "sandbox/pfalcon/trusted-firmware-a" ]; then
24 # For test project, both "production" and "sandbox" go to the same elsewhere project.
Paul Sokolovskyfb2d0402024-03-04 20:20:55 +070025 RTD_PROJECT="pfalcon-trustedfirmware-a-sandbox"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070026 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/pfalcon-trustedfirmware-a-sandbox/263459/"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070027 RTD_WEBHOOK_SECRET_KEY=${PFALCON_RTD_WEBHOOK_SECRET}
28 RTD_API_TOKEN=${PFALCON_RTD_API_TOKEN}
29else
30 echo "Unknown GERRIT_PROJECT: ${GERRIT_PROJECT}"
31 exit 1
32fi
33
Paul Sokolovskyfb2d0402024-03-04 20:20:55 +070034RTD_API="https://readthedocs.org/api/v3/projects/${RTD_PROJECT}"
Paul Sokolovsky6338f102024-03-01 16:26:15 +070035RTD_VER_API="${RTD_API}/versions"
Arthur She8c3dac22024-01-15 20:13:40 -080036
37new_tag=""
Paul Sokolovsky3cc00232024-03-04 20:43:23 +070038new_slug=""
Arthur She8c3dac22024-01-15 20:13:40 -080039refname=${GERRIT_REFNAME##*/}
40lts_branch=${refname}
Paul Sokolovsky07bd0952024-03-01 16:14:14 +070041if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then
42 new_tag=${GERRIT_REFNAME#refs/tags/}
Paul Sokolovsky3cc00232024-03-04 20:43:23 +070043 # Convert tag to ReadTheDocs version slug
44 new_slug=$(echo ${new_tag} | tr '[A-Z]/' '[a-z]-')
Paul Sokolovsky07bd0952024-03-01 16:14:14 +070045 lts_branch=${refname%.*}
46fi
Arthur She8c3dac22024-01-15 20:13:40 -080047
48function activate_version() {
49 version=$1
Arthur Shec4b4b5e2024-02-09 21:28:27 -080050 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080051 retry=0
52
Arthur Shec4b4b5e2024-02-09 21:28:27 -080053 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
54 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080055
Arthur Shec4b4b5e2024-02-09 21:28:27 -080056 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080057 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080058 [ ${retry} -gt ${max_retry_time} ] && break
59 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080060 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080061 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
62 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080063 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080064
65 if [ ${retry} -le ${max_retry_time} ]; then
66 echo "Active new version: ${version}"
67 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
68 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
69 else
70 echo "RTD can not find the version: ${version}"
71 exit 1
72 fi
Arthur She8c3dac22024-01-15 20:13:40 -080073}
74
Paul Sokolovskyf1f68932024-03-01 16:37:41 +070075function wait_for_build() {
76 version=$1
Paul Sokolovsky2cac9b82024-03-18 15:17:08 +070077 retry=0
Paul Sokolovskyf1f68932024-03-01 16:37:41 +070078 while true; do
79 status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_API}/builds/" | \
80 jq -r ".results | map(select(.version==\"$version\")) | .[0].state.code")
81 echo $status
82 if [ "$status" == "finished" ]; then
83 break
84 fi
Paul Sokolovsky2cac9b82024-03-18 15:17:08 +070085
86 retry=$((retry + 1))
87 if [ $retry -gt 40 ]; then
88 echo "Could not confirm that ReadTheDoc slug ${version} was built in the alloted time."
89 break
90 fi
91
Paul Sokolovskyae61e3e2024-03-18 19:45:10 +070092 sleep 30
Paul Sokolovskyf1f68932024-03-01 16:37:41 +070093 done
94}
95
Arthur She8c3dac22024-01-15 20:13:40 -080096echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
97build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
98if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070099 # 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 -0800100 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +0700101 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -0800102 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
103 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -0800104 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
105 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -0800106fi
107
108# Triggered by a new tag
109if [ -n "${new_tag}" ]; then
Paul Sokolovsky3cc00232024-03-04 20:43:23 +0700110 echo -e "\nNew release tag: ${new_tag}, slug: ${new_slug}"
Arthur She8c3dac22024-01-15 20:13:40 -0800111 # Hide the current active and unhidden tags
112 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 +0700113 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -0800114 for t in ${old_tags};
115 do
116 echo "Hide old tag: ${t}"
117 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +0700118 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -0800119 done
120 # Active the new version
Paul Sokolovsky3cc00232024-03-04 20:43:23 +0700121 echo "Active new version: ${new_slug}"
122 activate_version ${new_slug}
Paul Sokolovskyaf1e5402024-03-17 18:08:43 +0700123
124 wait_for_build ${new_slug}
125 echo "Docs for the new release are available at: https://${RTD_PROJECT}.readthedocs.io/en/${new_slug}/"
Arthur She8c3dac22024-01-15 20:13:40 -0800126fi
127