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