blob: 29ef47bfa1f5e796476dfec3a6d6053b536c0fd2 [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 Sokolovskyf1f68932024-03-01 16:37:41 +070077 while true; do
78 status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_API}/builds/" | \
79 jq -r ".results | map(select(.version==\"$version\")) | .[0].state.code")
80 echo $status
81 if [ "$status" == "finished" ]; then
82 break
83 fi
84 sleep 10
85 done
86}
87
Arthur She8c3dac22024-01-15 20:13:40 -080088echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
89build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
90if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070091 # 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 -080092 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070093 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080094 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
95 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080096 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
97 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080098fi
99
100# Triggered by a new tag
101if [ -n "${new_tag}" ]; then
Paul Sokolovsky3cc00232024-03-04 20:43:23 +0700102 echo -e "\nNew release tag: ${new_tag}, slug: ${new_slug}"
Arthur She8c3dac22024-01-15 20:13:40 -0800103 # Hide the current active and unhidden tags
104 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 +0700105 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -0800106 for t in ${old_tags};
107 do
108 echo "Hide old tag: ${t}"
109 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +0700110 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -0800111 done
112 # Active the new version
Paul Sokolovsky3cc00232024-03-04 20:43:23 +0700113 echo "Active new version: ${new_slug}"
114 activate_version ${new_slug}
Arthur She8c3dac22024-01-15 20:13:40 -0800115fi
116