Update release scripts for branches
[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 while getopts "hl:r:i:e:m:" opt
51 do
52     case $opt in
53     h)
54         usage
55         ;;
56     l)
57         repo_location=$OPTARG
58         ;;
59     r)
60         specified_repo=$OPTARG
61         ;;
62     i)
63         issue_id=$OPTARG
64         ;;
65     e)
66         commit_header=$OPTARG
67         ;;
68     m)
69         commit_message=$OPTARG
70         ;;
71     \?)
72         usage
73         exit 1
74         ;;
75     esac
76 done
77
78 if [ $OPTIND -eq 1 ]
79 then
80     echo "no arguments were specified"
81     usage
82 fi
83
84 if [[ -z "$repo_location" ]]
85 then
86     echo "policy repo location not specified on -l flag"
87     exit 1
88 fi
89 if ! [ -d "$repo_location" ]
90 then
91     echo "policy repo location '$repo_location' not found"
92     exit 1
93 fi
94
95 if [ -z "$specified_repo" ]
96 then
97     echo "repo not specified on -r flag"
98     exit 1
99 fi
100
101 if [ -z "$issue_id" ]
102 then
103     echo "issue_id not specified on -i flag"
104     exit 1
105 fi
106
107 if ! echo "$issue_id" | grep -Eq '^POLICY-[0-9]*$'
108 then
109   echo "issue ID is invalid, it should be of form 'POLICY-nnnn'"
110   exit 1
111 fi
112
113 if [ -z "$commit_header" ]
114 then
115     echo "commit_header not specified on -e flag"
116     exit 1
117 fi
118
119 if [ -z "$commit_message" ]
120 then
121     echo "commit_message not specified on -m flag"
122     exit 1
123 fi
124
125 git -C "$repo_location/$specified_repo" status | grep '^Your branch is up to date' > /dev/null 2>&1
126 return_code=$?
127
128 if [ $return_code -eq 0 ]
129 then
130     echo "generating commit '$commit_header' . . ."
131
132     commit_msg_temp_file=$(mktemp)
133     {
134         echo "$commit_header"
135         echo ""
136         echo "$commit_message"
137         echo ""
138         echo "*** This commit is generated by a PF release script ***"
139         echo ""
140         echo "Issue-ID: $issue_id"
141     } > "$commit_msg_temp_file"
142
143     git -C "$repo_location/$specified_repo" add .
144     git -C "$repo_location/$specified_repo" commit -s -F "$commit_msg_temp_file"
145     rm "$commit_msg_temp_file"
146
147     echo "commit '$commit_header' generated"
148 else
149     echo "updating commit '$commit_header' . . ."
150
151     git -C "$repo_location/$specified_repo" add .
152     git -C "$repo_location/$specified_repo" commit -as --amend --no-edit
153
154     echo "commit '$commit_header' updated"
155 fi
156
157 echo "sending commit '$commit_header' to gerrit . . ."
158 git -C "$repo_location/$specified_repo" review
159 echo "commit '$commit_header' sent to gerrit . . ."
160
161