Update release scripts to support major releases
[policy/parent.git] / integration / src / main / scripts / release / newReleaseSnapshots.sh
1 #!/bin/bash
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2022-2023 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 - on release changes, generate commits to set the snapshot version and update"
57     echo "               references on any repos that reference other repos"
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 "         -m           - update snapshots for a major release, default is to update for a minor release"
67     echo "         -i issue-id  - issue ID in the format POLICY-nnnn"
68     echo ""
69     echo " examples:"
70     echo "  $SCRIPT_NAME -l /home/user/onap -d /home/user/data/pf_release_data.csv -i POLICY-1234"
71     echo "    set snapshots on the repos at location '/home/user/onap' using the release data"
72     echo "    in the file '/home/user/data/pf_release_data.csv'"
73     exit 255;
74 }
75
76 major_release=false
77
78 while getopts "hmd:l:i:" opt
79 do
80     case $opt in
81     h)
82         usage
83         ;;
84     d)
85         release_data_file=$OPTARG
86         ;;
87     l)
88         repo_location=$OPTARG
89         ;;
90     i)
91         issue_id=$OPTARG
92         ;;
93     m)
94         major_release=true
95         ;;
96     \?)
97         usage
98         exit 1
99         ;;
100     esac
101 done
102
103 if [ $OPTIND -eq 1 ]
104 then
105     echo "no arguments were specified"
106     usage
107 fi
108
109 if [[ -z "$repo_location" ]]
110 then
111     echo "policy repo location not specified on -l flag"
112     exit 1
113 fi
114
115 if ! [ -d "$repo_location" ]
116 then
117     echo "policy repo location '$repo_location' not found"
118     exit 1
119 fi
120
121 if [[ -z "$release_data_file" ]]
122 then
123     echo "policy release data file not specified on -d flag"
124     exit 1
125 fi
126
127 if ! [ -f "$release_data_file" ]
128 then
129     echo "policy release data file '$release_data_file' not found"
130     exit 1
131 fi
132
133 if [ -z "$issue_id" ]
134 then
135     echo "issue_id not specified on -i flag"
136     exit 1
137 fi
138
139 if ! echo "$issue_id" | grep -Eq '^POLICY-[0-9]*$'
140 then
141   echo "issue ID is invalid, it should be of form 'POLICY-nnnn'"
142   exit 1
143 fi
144
145 for specified_repo in "${pf_repos[@]}"
146 do
147     # shellcheck disable=SC2034
148     # shellcheck disable=SC2046
149     read -r repo \
150             latest_released_tag \
151             latest_snapshot_tag \
152             changed_files \
153             docker_images \
154         <<< $(grep "$specified_repo" "$release_data_file" | tr ',' ' ' )
155
156     if [ ! "$repo" = "$specified_repo" ]
157     then
158         echo "repo '$specified_repo' not found in file 'pf_release_data.csv'"
159         continue
160     fi
161
162     next_release_version=${latest_snapshot_tag%-*}
163
164     major_version=$(echo "$latest_released_tag" | $SED -E 's/^([0-9]*)\.[0-9]*\.[0-9]*$/\1/')
165     minor_version=$(echo "$latest_released_tag" | $SED -E 's/^[0-9]*\.([0-9]*)\.[0-9]*$/\1/')
166     patch_version=$(echo "$next_release_version" | $SED -E 's/^[0-9]*\.[0-9]*\.([0-9]*)$/\1/')
167     # shellcheck disable=SC2004
168
169     if $major_release
170     then
171         new_major_version=$(($major_version+1))
172         new_minor_version=0
173     else
174         new_major_version=$major_version
175         new_minor_version=$(($minor_version+1))
176     fi
177
178     new_snapshot_tag="$new_major_version"."$new_minor_version".0-SNAPSHOT
179
180     echo "updating snapshot version and references of repo $repo to $new_snapshot_tag . . ."
181     mvn -f "$repo_location/$repo" \
182         "-DnewVersion=$new_snapshot_tag" versions:set \
183         versions:update-child-modules versions:commit
184
185     temp_file=$(mktemp)
186
187     echo "set snapshot version of repo $repo in $repo_location/$repo/version.properties"
188     $SED \
189         -e "s/major=$major_version/minor=$new_major_version/" \
190         -e "s/minor=$minor_version/minor=$new_minor_version/" \
191         -e "s/patch=$patch_version/patch=0/" \
192         "$repo_location/$repo/version.properties" \
193         > "$temp_file"
194     mv "$temp_file" "$repo_location/$repo/version.properties"
195
196     updateRefs.sh -pcmoxs -d "$release_data_file" -l "$repo_location" -r "$repo"
197
198     generateCommit.sh \
199        -l "$repo_location" \
200         -r "$repo" \
201         -i "$issue_id" \
202         -e "Set snapshot and/or references of $repo for new release" \
203         -m "$repo updated to its latest own and reference snapshots"
204
205     echo "commit to set snapshot version and/or references of repo $repo generated"
206 done
207