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