tf-a-lts-docs-watcher.sh: Add wait_for_build() function

Currently, the script just ensures that a new release tag is activated
on the RTD side, which schedules a build of the docs. But it finished
otherwise, while the actual build may take a while (10 mins or more).
So, after the job completes, the new docs may be not available for
some time, which may lead to confusion from the users. To be able to
address this concern, add a function to inventory to be able to wait
for a build of a particular doc version to finish. With this change,
this function isn't actually called, that's left for future updates.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
Change-Id: I5a9154026a64d2ee4591bfbcd9a69c31531901bc
diff --git a/scripts/tf-a-lts-docs-watcher.sh b/scripts/tf-a-lts-docs-watcher.sh
index 46dd10e..c56ebf5 100755
--- a/scripts/tf-a-lts-docs-watcher.sh
+++ b/scripts/tf-a-lts-docs-watcher.sh
@@ -70,6 +70,21 @@
     fi
 }
 
+function wait_for_build() {
+    version=$1
+    # Convert tag to ReadTheDocs version slug
+    version=$(echo ${version} | tr '[A-Z]/' '[a-z]-')
+    while true; do
+        status=$(curl -s -H "Authorization: Token ${RTD_API_TOKEN}" "${RTD_API}/builds/" | \
+                   jq -r ".results | map(select(.version==\"$version\")) | .[0].state.code")
+        echo $status
+        if [ "$status" == "finished" ]; then
+            break
+        fi
+        sleep 10
+    done
+}
+
 echo "Notifying ReadTheDocs of changes on: ${lts_branch}"
 build_trigger=$(curl -s -X POST -d "branches=${lts_branch}" -d "token=${RTD_WEBHOOK_SECRET_KEY}" ${RTD_WEBHOOK_URL} | jq .build_triggered)
 if [ "${build_trigger}" = "false" ]; then