Python merge use git choosing strategy default
[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 project_dashed="${PROJECT////-}"
15 umbrella=$(echo "$GERRIT_URL" | awk -F'.' '{print $2}')
16 if [[ "$SILO" == "sandbox" ]]; then
17   rtdproject="$umbrella-$project_dashed-test"
18 else
19   rtdproject="$umbrella-$project_dashed"
20 fi
21
22 #MASTER_RTD_PROJECT as a global jenkins cnt
23 masterproject="$umbrella-$MASTER_RTD_PROJECT"
24
25 echo "INFO:"
26 echo "INFO: Project: $PROJECT"
27 echo "INFO: Read the Docs Sub Project: https://$rtdproject.readthedocs.io"
28 echo "INFO: Read the Docs Master Project: https://$masterproject.readthedocs.io"
29
30
31 if [[ "$JOB_NAME" =~ "verify" ]]; then
32   if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == "Not found." ]]; then
33     echo "INFO: Project not found, merge will create project https://$rtdproject.readthedocs.io"
34   fi
35
36 echo "INFO: Verify job completed"
37
38 fi
39
40 if [[ "$JOB_NAME" =~ "merge" ]]; then
41 echo "INFO: Performing merge action"
42
43   # This retuns null if project exists.
44   project_exists=false
45   project_created=false
46
47   declare -i cnt=0
48   while [[ $project_exists == "false" ]]; do
49     if [[ "$(lftools rtd project-details "$rtdproject" | yq -r '.detail')" == "Not found." ]]; then
50       echo "INFO: Project not found"
51         if [[ $project_created == "false" ]]; then
52           echo "INFO: Creating project https://$rtdproject.readthedocs.io"
53           lftools rtd project-create "$rtdproject" "$GERRIT_URL/$PROJECT" git "https://$rtdproject.readthedocs.io" py en
54           project_created="true"
55         fi
56         echo "INFO sleeping for 30 seconds $cnt times"
57       sleep 30
58       ((cnt+=1))
59       if (( cnt >= 20 )); then
60         echo "INFO: Job has timed out"
61         exit 1
62       fi
63     else
64       echo "INFO: Project exists in read the docs as https://$rtdproject.readthedocs.io"
65       project_exists="true"
66     fi
67   done
68
69   if [[ "$rtdproject" != "$masterproject" ]]; then
70     subproject_exists=false
71     while read -r subproject; do
72       if [[ "$subproject" == "$rtdproject" ]]; then
73         subproject_exists=true
74         break
75       fi
76     done < <(lftools rtd subproject-list "$masterproject")
77
78     if $subproject_exists; then
79       echo "INFO: subproject $rtdproject relationship already created"
80     else
81       echo "INFO: Creating subproject relationship"
82       lftools rtd subproject-create "$masterproject" "$rtdproject"
83       echo "INFO sleeping for 10 seconds"
84       sleep 10
85     fi
86   fi
87
88   # api v3 method does not update /latest/ when master is triggered.
89   # Also, when we build anything other than master we want to trigger /stable/ as well.
90   # allow projects to change their landing page from latest to branch_name
91
92   current_version="$(lftools rtd project-details "$rtdproject" | yq -r .default_version)"
93   if [[ -z ${DEFAULT_VERSION:-} ]]; then
94     echo "DEFAULT_VERSION (default-version) value cannot be empty"
95     exit 1
96   fi
97   default_version="${DEFAULT_VERSION}"
98
99   echo "INFO: current default version $current_version"
100   if [[ $current_version != "$default_version" ]]; then
101     echo "INFO: Setting rtd landing page to $default_version"
102     lftools rtd project-update "$rtdproject" default_version="$default_version"
103   fi
104
105   lftools rtd project-build-trigger "$rtdproject" "$GERRIT_BRANCH"
106   if [[ $GERRIT_BRANCH == "master" ]]; then
107     echo "INFO: triggering latest"
108     lftools rtd project-build-trigger "$rtdproject" latest
109   else
110     echo "INFO: triggering stable"
111     lftools rtd project-build-trigger "$rtdproject" stable
112   fi
113 fi