blob: 46dd10efc7fbd8d70cf39388d1995d6dd47c67f8 [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
13 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a/87181/"
14 RTD_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a"
15 RTD_WEBHOOK_SECRET_KEY=${RTD_WEBHOOK_SECRET}
16 RTD_API_TOKEN=${RTD_API_TOKEN}
17 else
18 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/trustedfirmware-a-sandbox/263958/"
19 RTD_API="https://readthedocs.org/api/v3/projects/trustedfirmware-a-sandbox"
20 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.
25 RTD_WEBHOOK_URL="https://readthedocs.org/api/v2/webhook/pfalcon-trustedfirmware-a-sandbox/263459/"
26 RTD_API="https://readthedocs.org/api/v3/projects/pfalcon-trustedfirmware-a-sandbox"
27 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
34RTD_VER_API="${RTD_API}/versions"
Arthur She8c3dac22024-01-15 20:13:40 -080035
36new_tag=""
37refname=${GERRIT_REFNAME##*/}
38lts_branch=${refname}
Paul Sokolovsky07bd0952024-03-01 16:14:14 +070039if echo ${GERRIT_REFNAME} | grep -q "refs/tags/"; then
40 new_tag=${GERRIT_REFNAME#refs/tags/}
41 lts_branch=${refname%.*}
42fi
Arthur She8c3dac22024-01-15 20:13:40 -080043
44function activate_version() {
45 version=$1
Paul Sokolovsky980b3b82024-02-29 20:54:18 +070046 # Convert tag to ReadTheDocs version slug
47 version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
Arthur Shec4b4b5e2024-02-09 21:28:27 -080048 max_retry_time=20
Arthur She8c3dac22024-01-15 20:13:40 -080049 retry=0
50
Arthur Shec4b4b5e2024-02-09 21:28:27 -080051 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
52 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080053
Arthur Shec4b4b5e2024-02-09 21:28:27 -080054 while [ "${ver_status}" == "Not found." ];
Arthur She8c3dac22024-01-15 20:13:40 -080055 do
Arthur Shec4b4b5e2024-02-09 21:28:27 -080056 [ ${retry} -gt ${max_retry_time} ] && break
57 sleep 30
Arthur She8c3dac22024-01-15 20:13:40 -080058 retry=$((retry+1))
Arthur Shec4b4b5e2024-02-09 21:28:27 -080059 ver_status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" ${RTD_VER_API}/${version}/ | \
60 jq -r '.detail')
Arthur She8c3dac22024-01-15 20:13:40 -080061 done
Arthur Shec4b4b5e2024-02-09 21:28:27 -080062
63 if [ ${retry} -le ${max_retry_time} ]; then
64 echo "Active new version: ${version}"
65 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
66 -d "{\"active\": true, \"hidden\": false}" ${RTD_VER_API}/${version}/
67 else
68 echo "RTD can not find the version: ${version}"
69 exit 1
70 fi
Arthur She8c3dac22024-01-15 20:13:40 -080071}
72
73echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
74build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
75if [ "${build_trigger}" = "false" ]; then
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070076 # 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 -080077 # we can trigger a build for the master branch to update all branches
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070078 echo "The branch ${lts_branch} is now! Activate and hide it!"
Arthur She8c3dac22024-01-15 20:13:40 -080079 curl -s -X POST -d "branches=master" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL}
80 activate_version ${lts_branch}
Arthur She4c341b22024-01-30 20:13:43 -080081 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
82 -d "{\"hidden\": true}" ${RTD_VER_API}/${lts_branch}/
Arthur She8c3dac22024-01-15 20:13:40 -080083fi
84
85# Triggered by a new tag
86if [ -n "${new_tag}" ]; then
87 echo -e "\nNew release tag: ${new_tag}"
88 # Hide the current active and unhidden tags
89 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 +070090 jq -r '.results | map(select(.hidden == false) | .slug) | .[]')
Arthur She8c3dac22024-01-15 20:13:40 -080091 for t in ${old_tags};
92 do
93 echo "Hide old tag: ${t}"
94 curl -s -X PATCH -H "Content-Type: application/json" -H "Authorization: Token ${RTD_API_TOKEN}" \
Paul Sokolovsky92151ad2024-02-26 12:38:32 +070095 -d "{\"hidden\": true}" ${RTD_VER_API}/${t}/
Arthur She8c3dac22024-01-15 20:13:40 -080096 done
97 # Active the new version
98 echo "Active new version: ${new_tag}"
99 activate_version ${new_tag}
100fi
101