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