eb806833ee5859c78fb6144dd87566b8b24b30c9
[releng/global-jjb.git] / shell / rtdv3.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 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 echo "---> rtdv3.sh"
12 set -euo pipefail
13
14 # shellcheck disable=SC1090
15 . ~/lf-env.sh
16
17 lf-activate-venv --python python3 lftools yq
18
19 watchbuild(){
20     echo "INFO: Running build against branch $1"
21     local buildid
22     local result
23     buildid=$(lftools rtd project-build-trigger "$rtdproject" "$1" | jq '.build.id')
24
25     result=null
26     while [[ "$result" == null ]]; do
27         sleep 10
28         result=$(lftools rtd project-build-details "$rtdproject" "$buildid"  | jq '.success')
29         echo "INFO Current result of running build $result"
30         if [[ $result == failed ]]; then
31             echo "INFO: read the docs build completed with status: $result"
32             exit 1
33         fi
34     done
35     echo "INFO: read the docs build completed with status: $result"
36 }
37
38 project_dashed="${PROJECT////-}"
39 umbrella=$(echo "$GERRIT_URL" | awk -F'.' '{print $2}')
40 if [[ "$SILO" == "sandbox" ]]; then
41     rtdproject="$umbrella-$project_dashed-test"
42 else
43     rtdproject="$umbrella-$project_dashed"
44 fi
45
46 #MASTER_RTD_PROJECT as a global jenkins cnt
47 masterproject="$umbrella-$MASTER_RTD_PROJECT"
48
49 #Exceptions needed for onap, due to importing their old docs.
50 if [[ $masterproject == "onap-doc" ]]; then
51     masterproject="onap"
52 fi
53 if [[ $rtdproject == "onap-doc" ]]; then
54     rtdproject="onap"
55 fi
56
57
58 echo "INFO:"
59 echo "INFO: Project: $PROJECT"
60 echo "INFO: Read the Docs Sub Project: https://$rtdproject.readthedocs.io"
61 echo "INFO: Read the Docs Master Project: https://$masterproject.readthedocs.io"
62
63
64 if [[ "$JOB_NAME" =~ "verify" ]]; then
65     if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == \
66         "No Project matches the given query." ]]; then
67         echo "INFO: Project not found, merge will create project https://$rtdproject.readthedocs.io"
68     fi
69     echo "INFO: Verify job completed"
70 fi
71
72 if [[ "$JOB_NAME" =~ "merge" ]]; then
73     echo "INFO: Performing merge action"
74
75     # This retuns null if project exists.
76     project_exists=false
77     project_created=false
78
79     declare -i cnt=0
80     while [[ $project_exists == "false" ]]; do
81         if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == \
82             "No Project matches the given query." ]]; then
83             echo "INFO: Project not found"
84             if [[ $project_created == "false" ]]; then
85                 echo "INFO: Creating project https://$rtdproject.readthedocs.io"
86                 lftools rtd project-create "$rtdproject" "$GERRIT_URL/$PROJECT" \
87                     git "https://$rtdproject.readthedocs.io" py en
88                 project_created="true"
89             fi
90             echo "INFO sleeping for 30 seconds $cnt times"
91             sleep 30
92             cnt=$((cnt+1))
93             if (( cnt >= 20 )); then
94                 echo "INFO: Job has timed out"
95                 exit 1
96             fi
97         else
98             echo "INFO: Project exists in read the docs as https://$rtdproject.readthedocs.io"
99             project_exists="true"
100         fi
101     done
102
103     if [[ "$rtdproject" != "$masterproject" ]]; then
104         subproject_exists=false
105         while read -r subproject; do
106             if [[ "$subproject" == "$rtdproject" ]]; then
107                 subproject_exists=true
108                 break
109             fi
110         done < <(lftools rtd subproject-list "$masterproject")
111
112         if $subproject_exists; then
113             echo "INFO: subproject $rtdproject relationship already created"
114         else
115             echo "INFO: Creating subproject relationship"
116             lftools rtd subproject-create "$masterproject" "$rtdproject"
117             echo "INFO sleeping for 10 seconds"
118             sleep 10
119         fi
120     fi
121
122     # api v3 method does not update /latest/ when master is triggered.
123     # Also, when we build anything other than master we want to trigger /stable/ as well.
124     # allow projects to change their landing page from latest to branch_name
125
126     current_version="$(lftools rtd project-details "$rtdproject" | yq -r .default_version)"
127     if [[ -z ${DEFAULT_VERSION:-} ]]; then
128         echo "DEFAULT_VERSION (default-version) value cannot be empty"
129         exit 1
130     fi
131     default_version="${DEFAULT_VERSION}"
132
133     echo "INFO: current default version $current_version"
134     if [[ $current_version != "$default_version" ]]; then
135         echo "INFO: Setting rtd landing page to $default_version"
136         lftools rtd project-update "$rtdproject" default_version="$default_version"
137     fi
138
139     if [[ $GERRIT_BRANCH == "master" ]]; then
140         echo "INFO: triggering $rtdproject latest"
141         watchbuild latest
142     else
143
144         #read the docs only understands lower case branch names
145         branch=$(echo "$GERRIT_BRANCH" | tr '[:upper:]' '[:lower:]')
146         echo "INFO: Checking if read the docs has seen branch $branch"
147
148         #if this is 200 "null" Then run discover branch
149         if [[ $(lftools rtd project-version-details "$rtdproject" "$branch" | jq '.active') == "null" ]]; then
150             echo "INFO: read the docs has not seen branch $branch for project $rtdproject"
151             echo "INFO: triggering $rtdproject latest to instantiate new branch discovery"
152             watchbuild latest
153         fi
154
155         echo "INFO: triggering $rtdproject $branch"
156         watchbuild "$branch"
157
158         #Make newly discovered branches visible in the u/i
159         isactive=$(lftools rtd project-version-details "$rtdproject" "$branch" | jq '.active')
160         if [[ "$isactive" == false ]]; then
161             echo "INFO: Marking $branch as active for project $rtdproject"
162             lftools rtd project-version-update "$rtdproject" "$branch" true
163         fi
164
165     fi
166 fi