blob: e3850ef3ccc726c9075af74ebe26ca5a7ccfa843 [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=""
38refname=${GERRIT_REFNAME##*/}
39lts_branch=${refname}
Paul Sokolovsky07bd0952024-03-01 16:14:14 +070040if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then
41 new_tag=${GERRIT_REFNAME#refs/tags/}
42 lts_branch=${refname%.*}
43fi
Arthur She8c3dac22024-01-15 20:13:40 -080044
45function activate_version() {
46 version=$1
Paul Sokolovsky980b3b82024-02-29 20:54:18 +070047 # Convert tag to ReadTheDocs version slug
48 version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
Arthur Shec4b4b5e2024-02-09 21:28:27 -080049 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080050 retry=0
51
Arthur Shec4b4b5e2024-02-09 21:28:27 -080052 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
53 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080054
Arthur Shec4b4b5e2024-02-09 21:28:27 -080055 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080056 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080057 [ ${retry} -gt ${max_retry_time} ] && break
58 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080059 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080060 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
61 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080062 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080063
64 if [ ${retry} -le ${max_retry_time} ]; then
65 echo "Active new version: ${version}"
66 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
67 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
68 else
69 echo "RTD can not find the version: ${version}"
70 exit 1
71 fi
Arthur She8c3dac22024-01-15 20:13:40 -080072}
73
Paul Sokolovskyf1f68932024-03-01 16:37:41 +070074function wait_for_build() {
75 version=$1
76 # Convert tag to ReadTheDocs version slug
77 version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
78 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
85 sleep 10
86 done
87}
88
Arthur She8c3dac22024-01-15 20:13:40 -080089echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
90build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
91if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070092 # 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 -080093 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070094 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080095 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
96 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080097 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
98 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080099fi
100
101# Triggered by a new tag
102if [ -n "${new_tag}" ]; then
103 echo -e "\nNew release tag: ${new_tag}"
104 # Hide the current active and unhidden tags
105 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 +0700106 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -0800107 for t in ${old_tags};
108 do
109 echo "Hide old tag: ${t}"
110 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +0700111 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -0800112 done
113 # Active the new version
114 echo "Active new version: ${new_tag}"
115 activate_version ${new_tag}
116fi
117