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