Repalce PolicyDevelopment with PolicyAPI in docs
[policy/parent.git] / integration / src / release_scripts / getReleaseData.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 branch="master"
29
30 # Use the bash internal OSTYPE variable to check for MacOS
31 if [[ "$OSTYPE" == "darwin"* ]]
32 then
33     SED="gsed"
34 else
35     SED="sed"
36 fi
37
38 declare -a pf_repos=(
39     "policy/parent"
40     "policy/docker"
41     "policy/common"
42     "policy/models"
43     "policy/api"
44     "policy/pap"
45     "policy/apex-pdp"
46     "policy/drools-pdp"
47     "policy/xacml-pdp"
48     "policy/distribution"
49     "policy/clamp"
50     "policy/gui"
51     "policy/drools-applications"
52 )
53
54 usage()
55 {
56     echo ""
57     echo "$SCRIPT_NAME - gets information from the checked out Policy Framework repos for the release process"
58     echo ""
59     echo "       usage:  $SCRIPT_NAME [-options]"
60     echo ""
61     echo "       options"
62     echo "         -h           - this help message"
63     echo "         -b branch    - the branch to release on, defaults to '$branch'"
64     echo "         -d data_file - the policy release data file to create, defaults to '$release_data_file'"
65     echo "         -l location  - the location of the policy framework repos on the file system,"
66     echo "                        defaults to '$repo_location'"
67     exit 255;
68 }
69
70 while getopts "hb:d:l:" opt
71 do
72     case $opt in
73     h)
74         usage
75         ;;
76     b)
77         branch=$OPTARG
78         ;;
79     d)
80         release_data_file=$OPTARG
81         ;;
82     l)
83         repo_location=$OPTARG
84         ;;
85     \?)
86         usage
87         exit 1
88         ;;
89     esac
90 done
91
92 if [[ -z "$repo_location" ]]
93 then
94     echo "policy repo location not specified on -l flag"
95     exit 1
96 fi
97
98 if [[ -z "$branch" ]]
99 then
100     echo "policy branch not specified on -b flag"
101     exit 1
102 fi
103
104 if ! [ -d "$repo_location" ]
105 then
106     echo "policy repo location '$repo_location' not found"
107     exit 1
108 fi
109
110 if [[ -z "$release_data_file" ]]
111 then
112     echo "policy release data file not specified on -d flag"
113     exit 1
114 fi
115
116 update_repos() {
117     echo "updating the policy framework data from '$repo_location' to data file '$release_data_file' . . ."
118
119     for repo in "${pf_repos[@]}"
120     do
121         echo "updating data from repo $repo branch $branch to data file '$release_data_file' . . ."
122
123         if [ -d "$repo_location/$repo" ]
124         then
125             echo "updating repository '$repo' . . ."
126             git -C "$repo_location/$repo" checkout -- .
127             git -C "$repo_location/$repo" checkout "$branch"
128             git -C "$repo_location/$repo" pull
129             git -C "$repo_location/$repo" rebase
130             git -C "$repo_location/$repo" fetch --tags
131         else
132             echo "repo $repo does not exist"
133             exit 1
134         fi
135         echo "data from repo $repo updated to data file '$release_data_file' . . ."
136     done
137
138     echo "policy framework data from '$repo_location' updated to data file '$release_data_file' . . ."
139 }
140
141 get_tags() {
142     echo "Repo, Last Tag Version,Snapshot Version,Changed Files,Docker Images"
143     echo "Repo, Last Tag Version,Snapshot Version,Changed Files,Docker Images" > "$release_data_file"
144     for repo in "${pf_repos[@]}"
145     do
146         latest_snapshot_tag=$(mvn -f "$repo_location/$repo" clean | \
147             grep "SNAPSHOT" | \
148             tail -1 | \
149             $SED -r 's/^.* ([0-9]*\.[0-9]*\.[0-9]*-SNAPSHOT).*$/\1/')
150
151         if [[ $branch == *master ]]
152         then
153             latest_released_tag=$(git -C "$repo_location/$repo" tag | \
154                 grep '^[0-9]*\.[0-9]*\.[0-9]*$' | \
155                 sort -V | \
156                 tail -1)
157         else
158             # shellcheck disable=SC2001
159             latest_snapshot_major_minor=$(echo "$latest_snapshot_tag" | sed 's/\.[0-9]*-SNAPSHOT$//')
160             latest_released_tag=$(git -C "$repo_location/$repo" tag | \
161                 grep '^[0-9]*\.[0-9]*\.[0-9]*$' | \
162                 grep "$latest_snapshot_major_minor" | \
163                 sort -V | \
164                 tail -1)
165         fi
166
167         changed_files=$(git -C "$repo_location/$repo" diff --name-only "$latest_released_tag" "$branch" | \
168             grep -v 'pom.xml$' | \
169             grep -v '^version.properties$' | \
170             grep -v "^releases/$latest_released_tag.yaml$" | \
171             grep -cv "^releases/$latest_released_tag-container.yaml$" | \
172             $SED 's/^[[:space:]]*//g')
173
174         latest_container_yaml=$(find "$repo_location/$repo/releases" -name "*container.yaml" | sort | tail -1)
175         if [ "$latest_container_yaml" != "" ]
176         then
177             docker_images=$(grep '\- name:' "$latest_container_yaml" | \
178             $SED -e 's/\- //g' -e 's/\://g' -e "s/\'//g" -e 's/^[[:space:]]*//g' -e 's/^name //' | \
179             tr '\n' ':' | \
180             $SED 's/:$//')
181         else
182             docker_images=""
183         fi
184
185         echo "$repo,$latest_released_tag,$latest_snapshot_tag,$changed_files,$docker_images"
186         echo "$repo,$latest_released_tag,$latest_snapshot_tag,$changed_files,$docker_images" >> "$release_data_file"
187     done
188 }
189
190 update_repos
191 get_tags