Fix logic for discovering new branches
[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 echo "INFO:"
45 echo "INFO: Project: $PROJECT"
46 echo "INFO: Read the Docs Sub Project: https://$rtdproject.readthedocs.io"
47 echo "INFO: Read the Docs Master Project: https://$masterproject.readthedocs.io"
48
49
50 if [[ "$JOB_NAME" =~ "verify" ]]; then
51   if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == "Not found." ]]; then
52     echo "INFO: Project not found, merge will create project https://$rtdproject.readthedocs.io"
53   fi
54
55 echo "INFO: Verify job completed"
56
57 fi
58
59 if [[ "$JOB_NAME" =~ "merge" ]]; then
60 echo "INFO: Performing merge action"
61
62   # This retuns null if project exists.
63   project_exists=false
64   project_created=false
65
66   declare -i cnt=0
67   while [[ $project_exists == "false" ]]; do
68     if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == "Not found." ]]; then
69       echo "INFO: Project not found"
70         if [[ $project_created == "false" ]]; then
71           echo "INFO: Creating project https://$rtdproject.readthedocs.io"
72           lftools rtd project-create "$rtdproject" "$GERRIT_URL/$PROJECT" git "https://$rtdproject.readthedocs.io" py en
73           project_created="true"
74         fi
75         echo "INFO sleeping for 30 seconds $cnt times"
76       sleep 30
77       ((cnt+=1))
78       if (( cnt >= 20 )); then
79         echo "INFO: Job has timed out"
80         exit 1
81       fi
82     else
83       echo "INFO: Project exists in read the docs as https://$rtdproject.readthedocs.io"
84       project_exists="true"
85     fi
86   done
87
88   if [[ "$rtdproject" != "$masterproject" ]]; then
89     subproject_exists=false
90     while read -r subproject; do
91       if [[ "$subproject" == "$rtdproject" ]]; then
92         subproject_exists=true
93         break
94       fi
95     done < <(lftools rtd subproject-list "$masterproject")
96
97     if $subproject_exists; then
98       echo "INFO: subproject $rtdproject relationship already created"
99     else
100       echo "INFO: Creating subproject relationship"
101       lftools rtd subproject-create "$masterproject" "$rtdproject"
102       echo "INFO sleeping for 10 seconds"
103       sleep 10
104     fi
105   fi
106
107   # api v3 method does not update /latest/ when master is triggered.
108   # Also, when we build anything other than master we want to trigger /stable/ as well.
109   # allow projects to change their landing page from latest to branch_name
110
111   current_version="$(lftools rtd project-details "$rtdproject" | yq -r .default_version)"
112   if [[ -z ${DEFAULT_VERSION:-} ]]; then
113     echo "DEFAULT_VERSION (default-version) value cannot be empty"
114     exit 1
115   fi
116   default_version="${DEFAULT_VERSION}"
117
118   echo "INFO: current default version $current_version"
119   if [[ $current_version != "$default_version" ]]; then
120     echo "INFO: Setting rtd landing page to $default_version"
121     lftools rtd project-update "$rtdproject" default_version="$default_version"
122   fi
123
124   if [[ $GERRIT_BRANCH == "master" ]]; then
125     echo "INFO: triggering $rtdproject latest"
126     watchbuild latest
127   else
128
129     #read the docs only understands lower case branch names
130     branch=$(echo "$GERRIT_BRANCH" | tr '[:upper:]' '[:lower:]')
131     echo "INFO: Checking if read the docs has seen branch $branch"
132
133     #if this is 200 "null" Then run discover branch
134     if [[ $(lftools rtd project-version-details "$rtdproject" "$branch" | jq '.active') == "null" ]]; then
135       echo "INFO: read the docs has not seen branch $branch for project $rtdproject"
136       echo "INFO: triggering $rtdproject latest to instantiate new branch discovery"
137       watchbuild latest
138     fi
139
140     echo "INFO: triggering $rtdproject $branch"
141     watchbuild "$branch"
142
143     #Make newly discovered branches visible in the u/i
144     isactive=$(lftools rtd project-version-details "$rtdproject" "$branch" | jq '.active')
145     if [[ "$isactive" == false ]]; then
146       echo "INFO: Marking $branch as active for project $rtdproject"
147       lftools rtd project-version-update "$rtdproject" "$branch" true
148     fi
149
150   fi
151 fi