Add release script, fix sed for MacOS
[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
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/apex-pdp"
45     "policy/drools-pdp"
46     "policy/xacml-pdp"
47     "policy/distribution"
48     "policy/clamp"
49     "policy/gui"
50     "policy/drools-applications"
51 )
52
53 usage()
54 {
55     echo ""
56     echo "$SCRIPT_NAME - gets information from the checked out Policy Framework repos for the release process"
57     echo ""
58     echo "       usage:  $SCRIPT_NAME [-options]"
59     echo ""
60     echo "       options"
61     echo "         -h           - this help message"
62     echo "         -d data_file - the policy release data file to create, defaults to '$release_data_file'"
63     echo "         -l location  - the location of the policy framework repos on the file system,"
64     echo "                        defaults to '$repo_location'"
65     exit 255;
66 }
67
68 while getopts "hd:l:" opt
69 do
70     case $opt in
71     h)
72         usage
73         ;;
74     d)
75         release_data_file=$OPTARG
76         ;;
77     l)
78         repo_location=$OPTARG
79         ;;
80     \?)
81         usage
82         exit 1
83         ;;
84     esac
85 done
86
87 if [ $OPTIND -eq 1 ]
88 then
89     echo "no arguments were specified"
90     usage
91 fi
92
93 if [[ -z "$repo_location" ]]
94 then
95     echo "policy repo location not specified on -l flag"
96     exit 1
97 fi
98
99 if ! [ -d "$repo_location" ]
100 then
101     echo "policy repo location '$repo_location' not found"
102     exit 1
103 fi
104
105 if [[ -z "$release_data_file" ]]
106 then
107     echo "policy release data file not specified on -d flag"
108     exit 1
109 fi
110
111 update_repos() {
112     echo "updating the policy framework data from '$repo_location' to data file '$release_data_file' . . ."
113
114     for repo in "${pf_repos[@]}"
115     do
116         echo "updating data from repo $repo to data file '$release_data_file' . . ."
117
118         if [ -d $repo_location/$repo ]
119         then
120             echo "updating repository '$repo' . . ."
121             git -C $repo_location/$repo checkout master
122             git -C $repo_location/$repo pull
123             git -C $repo_location/$repo rebase
124             git -C $repo_location/$repo fetch --tags
125         else
126             echo "repo $repo does not exist"
127             exit 1
128         fi
129         echo "data from repo $repo updated to data file '$release_data_file' . . ."
130     done
131
132     echo "policy framework data from '$repo_location' updated to data file '$release_data_file' . . ."
133 }
134
135
136 get_tags() {
137     echo "Repo, Last Tag Version,Master Snapshot Version,Changed Files,Docker Images"
138     echo "repo, Last Tag Version,Master Snapshot Version,Changed Files,Docker Images" > $release_data_file
139     for repo in "${pf_repos[@]}"
140     do
141         latest_released_tag=`git -C $repo_location/$repo tag | \
142             grep '^[0-9]*\.[0-9]*\.[0-9]*$' | \
143             sort -V | \
144             tail -1`
145
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         changed_files=`git -C $repo_location/$repo diff --name-only $latest_released_tag origin/master | \
152             grep -v 'pom.xml$' | \
153             grep -v '^version.properties$' | \
154             grep -v "^releases/$latest_released_tag.yaml$" | \
155             grep -v "^releases/$latest_released_tag-container.yaml$" | \
156             wc -l | \
157             $SED 's/^[[:space:]]*//g'`
158
159         if [ -f $repo_location/$repo/releases/$latest_released_tag-container.yaml ]
160         then
161             docker_images=`grep '\- name:' $repo_location/$repo/releases/$latest_released_tag-container.yaml | \
162             $SED -e 's/\- //g' -e 's/\://g' -e "s/\'//g" -e 's/^[[:space:]]*//g' -e 's/^name //' | \
163             tr '\n' ':' | \
164             $SED 's/:$//'`
165         else
166             docker_images=""
167         fi
168
169         echo "$repo,$latest_released_tag,$latest_snapshot_tag,$changed_files,$docker_images"
170         echo "$repo,$latest_released_tag,$latest_snapshot_tag,$changed_files,$docker_images" >> $release_data_file
171     done
172 }
173
174 update_repos
175 get_tags
176