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