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