Update docker files base images to snapshots
[policy/parent.git] / integration / src / main / scripts / release / bumpSnapshots.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2021-2022 Nordix Foundation.
8 # =========================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END==================================================
21 #
22
23 set -e
24
25 SCRIPT_NAME=$(basename "$0")
26 repo_location="./"
27 release_data_file="./pf_release_data.csv"
28
29 # Use the bash internal OSTYPE variable to check for MacOS
30 if [[ "$OSTYPE" == "darwin"* ]]
31 then
32     SED="gsed"
33 else
34     SED="sed"
35 fi
36
37 declare -a pf_repos=(
38         "policy/parent"
39         "policy/docker"
40         "policy/common"
41         "policy/models"
42         "policy/api"
43         "policy/pap"
44         "policy/drools-pdp"
45         "policy/apex-pdp"
46         "policy/xacml-pdp"
47         "policy/distribution"
48         "policy/gui"
49         "policy/clamp"
50         "policy/drools-applications"
51 )
52
53 usage()
54 {
55     echo ""
56     echo "$SCRIPT_NAME - generate commits to bump the snapshot version and update references to snapshot references"
57     echo "               on any repos that need to be bumped or updated"
58     echo ""
59     echo "       usage:  $SCRIPT_NAME [-options]"
60     echo ""
61     echo "       options"
62     echo "         -h           - this help message"
63     echo "         -d data_file - the policy release data file to use, defaults to '$release_data_file'"
64     echo "         -l location  - the location of the policy framework repos on the file system,"
65     echo "                        defaults to '$repo_location'"
66     echo "         -i issue-id  - issue ID in the format POLICY-nnnn"
67     echo ""
68     echo " examples:"
69     echo "  $SCRIPT_NAME -l /home/user/onap -d /home/user/data/pf_release_data.csv -i POLICY-1234"
70     echo "    bump snapshots on the repos at location '/home/user/onap' using the release data"
71     echo "    in the file '/home/user/data/pf_release_data.csv'"
72     exit 255;
73 }
74
75 while getopts "hd:l:i:" opt
76 do
77     case $opt in
78     h)
79         usage
80         ;;
81     d)
82         release_data_file=$OPTARG
83         ;;
84     l)
85         repo_location=$OPTARG
86         ;;
87     i)
88         issue_id=$OPTARG
89         ;;
90     \?)
91         usage
92         exit 1
93         ;;
94     esac
95 done
96
97 if [ $OPTIND -eq 1 ]
98 then
99     echo "no arguments were specified"
100     usage
101 fi
102
103 if [[ -z "$repo_location" ]]
104 then
105     echo "policy repo location not specified on -l flag"
106     exit 1
107 fi
108
109 if ! [ -d "$repo_location" ]
110 then
111     echo "policy repo location '$repo_location' not found"
112     exit 1
113 fi
114
115 if [[ -z "$release_data_file" ]]
116 then
117     echo "policy release data file not specified on -d flag"
118     exit 1
119 fi
120
121 if ! [ -f "$release_data_file" ]
122 then
123     echo "policy release data file '$release_data_file' not found"
124     exit 1
125 fi
126
127 if [ -z "$issue_id" ]
128 then
129     echo "issue_id not specified on -i flag"
130     exit 1
131 fi
132
133 if ! echo "$issue_id" | grep -Eq '^POLICY-[0-9]*$'
134 then
135   echo "issue ID is invalid, it should be of form 'POLICY-nnnn'"
136   exit 1
137 fi
138
139 for specified_repo in "${pf_repos[@]}"
140 do
141     # shellcheck disable=SC2034
142     # shellcheck disable=SC2046
143     read -r repo \
144             latest_released_tag \
145             latest_snapshot_tag \
146             changed_files \
147             docker_images \
148         <<< $(grep "$specified_repo" "$release_data_file" | tr ',' ' ' )
149
150     if [ ! "$repo" = "$specified_repo" ]
151     then
152         echo "repo '$specified_repo' not found in file 'pf_release_data.csv'"
153         continue
154     fi
155
156     next_release_version=${latest_snapshot_tag%-*}
157
158     if [ "$latest_released_tag" = "$next_release_version" ]
159     then
160         major_version=$(echo "$next_release_version" | $SED -E 's/^([0-9]*)\.[0-9]*\.[0-9]*$/\1/')
161         minor_version=$(echo "$next_release_version" | $SED -E 's/^[0-9]*\.([0-9]*)\.[0-9]*$/\1/')
162         patch_version=$(echo "$next_release_version" | $SED -E 's/^[0-9]*\.[0-9]*\.([0-9]*)$/\1/')
163         # shellcheck disable=SC2004
164         new_patch_version=$(($patch_version+1))
165
166         new_snapshot_tag="$major_version"."$minor_version"."$new_patch_version"-SNAPSHOT
167
168         echo "updating snapshot version and references of repo $repo to $new_snapshot_tag . . ."
169         mvn -f "$repo_location/$repo" \
170             "-DnewVersion=$new_snapshot_tag" versions:set \
171             versions:update-child-modules versions:commit
172
173         temp_file=$(mktemp)
174
175         echo "updating snapshot version of repo $repo in $repo_location/$repo/version.properties"
176         $SED -e "s/patch=$patch_version/patch=$new_patch_version/" \
177             "$repo_location/$repo/version.properties" > "$temp_file"
178         mv "$temp_file" "$repo_location/$repo/version.properties"
179     fi
180
181     updateRefs.sh -pcmokxs -d "$release_data_file" -l "$repo_location" -r "$repo"
182
183     if [ "$(git -C "$repo_location/$specified_repo" status |
184         grep \
185             -e '^\s*modified:\s*pom.xml$' \
186             -e '^\s*modified:\s*.*Dockerfile$' \
187             > /dev/null 2>&1)" \
188         = 1 ]
189     then
190         references_updated=0
191     else
192         references_updated=1
193     fi
194
195     if [ "$latest_released_tag" != "$next_release_version" ] && [ $references_updated -ne 0 ]
196     then
197         continue
198     fi
199
200     echo "generating commit to update snapshot version and/or references of repo $repo . . ."
201
202     generateCommit.sh \
203         -l "$repo_location" \
204         -r "$repo" \
205         -i "$issue_id" \
206         -e "Update snapshot and/or references of $repo to latest snapshots" \
207         -m "$repo updated to its latest own and reference snapshots"
208
209     echo "commit to update snapshot version and/or references of repo $repo generated"
210 done
211