997a368dc6d74db39940f9ee9246c626f7b56712
[policy/parent.git] / integration / src / release_scripts / generateCommit.sh
1 SCRIPT_NAME=`basename $0`
2
3 #
4 # ============LICENSE_START================================================
5 # ONAP
6 # =========================================================================
7 # Copyright (C) 2021 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 repo_location="./"
24
25 usage()
26 {
27     echo ""
28     echo "$SCRIPT_NAME - generates a new commit or a patch on an existing commit for PF releases"
29     echo ""
30     echo "       usage:  $SCRIPT_NAME [-options]"
31     echo ""
32     echo "       options"
33     echo "         -h                - this help message"
34     echo "         -l location       - the location of the policy framework repos on the file system,"
35     echo "                             defaults to '$repo_location'"
36     echo "         -r repo           - the policy repo to which to commit"
37     echo "         -i issue-id       - issue ID in the format POLICY-nnnn"
38     echo "         -e commit-header  - the header for the commit"
39     echo "         -m commit-message - the message body for the commit"
40     echo ""
41     echo " example:"
42     echo "  $SCRIPT_NAME -l /home/git/onap -r policy/pap -i POLICY-1234 -e commit-header -m commit-message"
43     echo "    create a new commit or update an existing commit on policy/pap with the given details"
44     exit 255;
45 }
46
47 update_parent=false
48 update_common=false
49 update_models=false
50 update_drools_pdp=false
51 update_snapshot=false
52 update_docker=false
53
54 while getopts "hl:r:i:e:m:" opt
55 do
56     case $opt in
57     h)
58         usage
59         ;;
60     l)
61         repo_location=$OPTARG
62         ;;
63     r)
64         specified_repo=$OPTARG
65         ;;
66     i)
67         issue_id=$OPTARG
68         ;;
69     e)
70         commit_header=$OPTARG
71         ;;
72     m)
73         commit_message=$OPTARG
74         ;;
75     \?)
76         usage
77         exit 1
78         ;;
79     esac
80 done
81
82 if [ $OPTIND -eq 1 ]
83 then
84     echo "no arguments were specified"
85     usage
86 fi
87
88 if [[ -z "$repo_location" ]]
89 then
90     echo "policy repo location not specified on -l flag"
91     exit 1
92 fi
93 if ! [ -d "$repo_location" ]
94 then
95     echo "policy repo location '$repo_location' not found"
96     exit 1
97 fi
98
99 if [ -z "$specified_repo" ]
100 then
101     echo "repo not specified on -r flag"
102     exit 1
103 fi
104
105 if [ -z "$issue_id" ]
106 then
107     echo "issue_id not specified on -i flag"
108     exit 1
109 fi
110
111 if ! echo "$issue_id" | grep -Eq '^POLICY-[0-9]*$'
112 then
113   echo "issue ID is invalid, it should be of form 'POLICY-nnnn'"
114   exit 1
115 fi
116
117 if [ -z "$commit_header" ]
118 then
119     echo "commit_header not specified on -e flag"
120     exit 1
121 fi
122
123 if [ -z "$commit_message" ]
124 then
125     echo "commit_message not specified on -m flag"
126     exit 1
127 fi
128
129 git -C $repo_location/$specified_repo status | grep '^Your branch is up to date' > /dev/null 2>&1
130 return_code=$?
131
132 if [ $return_code -eq 0 ]
133 then
134     echo "generating commit '$commit_header' . . ."
135
136     commit_msg_temp_file=$(mktemp)
137     echo "$commit_header"                                           > $commit_msg_temp_file
138     echo ""                                                        >> $commit_msg_temp_file
139     echo "*** This commit is generated by a PF release script ***" >> $commit_msg_temp_file
140     echo ""                                                        >> $commit_msg_temp_file
141     echo "$commit_message"                                         >> $commit_msg_temp_file
142     echo ""                                                        >> $commit_msg_temp_file
143     echo "Issue-ID: $issue_id"                                     >> $commit_msg_temp_file
144
145     git -C $repo_location/$specified_repo add .
146     git -C $repo_location/$specified_repo commit -s -F "$commit_msg_temp_file"
147     rm $commit_msg_temp_file
148
149     echo "commit '$commit_header' generated"
150 else
151     echo "updating commit '$commit_header' . . ."
152
153     git -C $repo_location/$specified_repo add .
154     git -C $repo_location/$specified_repo commit -as --amend --no-edit
155
156     echo "commit '$commit_header' updated"
157 fi
158
159 echo "sending commit '$commit_header' to gerrit . . ."
160 git -C $repo_location/$specified_repo review
161 echo "commit '$commit_header' sent to gerrit . . ."
162
163