Support the new cut-branch-jobs script
[docs.git] / build_update_version_RTD.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 enable_build_in_RTD_usage () {
13     echo "Usage: $0 <path_to_autorelease> <token> <version_name> <publish>"
14     echo ""
15     echo "    path_to_autorelease:  The path to the autorelease"
16     echo "    token:  Enter Token for RTD authentication"
17     echo "    version_name:  Name of the version to be updated e.g stable-sodium."
18     echo "    publish:  Set to true to publish"
19     echo ""
20 }
21
22 #Function build_update_version
23 #build_update_version <token> <project_name> <version_name>
24 #Using APIv3 ReadTheDocs for triggering build and updating version i.e setting version active status true
25 #APIv3 : https://docs.readthedocs.io/en/latest/api/v3.html
26 build_update_version() {
27     token=$1
28     project=$2
29     version=$3
30     project_name_RTD=$(curl -H "Authorization: Token $token" \
31     https://readthedocs.org/api/v3/projects/?slug__contains="$project");
32     slug_name=$(echo "$project_name_RTD" \
33     | python3 -c "import sys, json; print(json.load(sys.stdin)['results'][0]['slug'])");
34
35     echo "Triggering build of $version in $slug_name"
36         curl -d -X POST -H "Authorization: Token $token" \
37         https://readthedocs.org/api/v3/projects/"$slug_name"/versions/"$version"/builds/;
38
39     echo "Enabling RTD $version for $slug_name"
40         curl --request PATCH \
41         "https://readthedocs.org/api/v3/projects/$slug_name/versions/$version/" \
42         --header "Content-Type: application/json"  \
43         --header "Accept: application/json"  \
44         --header "Authorization: Token $token"  \
45         --data "{\"active\": true,\"privacy_level\":\"public\"}";
46 }
47
48 while getopts :h: opts; do
49     case "$opts" in
50         h)
51             enable_build_in_RTD_usage
52             exit 0
53             ;;
54         [?])
55             enable_build_in_RTD_usage
56             exit 1
57             ;;
58     esac
59 done
60
61 # User input
62 path_to_autorelease="$1"
63 token="$2"
64 version_name="$3"
65 publish="$4"
66
67 ################
68 # Start script #
69 ###############
70 cd "$path_to_autorelease"/ || exit
71 git submodule update
72 list_submodule=$(git submodule | awk '{ print $2 }')
73 # echo $list_submodule
74 if [ "$publish" == "true" ]
75 then
76         for submodule_name in $list_submodule
77             do
78                 submodule_name="$(cut -d'/' -f2 <<<"$submodule_name")"
79                 build_update_version "$token" "$submodule_name" "$version_name"
80             done
81 fi