This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
metron-committer-common
357 lines (308 loc) · 10.7 KB
/
metron-committer-common
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# common and unlikely to change vars
#
# the upstream apache git repo for apache metron
METRON_UPSTREAM="https://git-wip-us.apache.org/repos/asf/metron.git"
# the upstream apache git repo for apache metron bro plugin kafka
BRO_PLUGIN_UPSTREAM="https://git-wip-us.apache.org/repos/asf/metron-bro-plugin-kafka.git"
# the common configuration file with the committer info
CONFIG_FILE=~/.metron-prepare-commit
GITHUB_REMOTE="origin"
BASE_BRANCH=master
# other var setup by these functions
PR=
WORK=
ORIGIN=
UPSTREAM=
PR_BRANCH=
USER=
EMAIL=
JIRA=
DESC=
APACHE_NAME=
APACHE_EMAIL=
GITHUB_NAME=
#
# Initialize the variables from the default configuration file, if it exists
#
function init_configuration {
# does a config file already exist?
echo "$CONFIG_FILE"
if [ -f ${CONFIG_FILE} ]; then
#shellcheck source=/dev/null
source ${CONFIG_FILE}
echo " ...using settings from $CONFIG_FILE"
fi
}
#
# Initialize the committer variables if they are not provided through the configuration file.
# If it is not present, it will be written out for the next time
#
function init_committer_info {
# github account of committer (you)
if [ -z "$GITHUB_NAME" ]; then
read -p " your github username [$GITHUB_NAME]: " INPUT
[ -n "$INPUT" ] && GITHUB_NAME=${INPUT}
# write setting to config file
echo "GITHUB_NAME=$GITHUB_NAME" >> ${CONFIG_FILE}
fi
# apache id of committer (you)
if [ -z "$APACHE_NAME" ]; then
read -p " your apache userid [$APACHE_NAME]: " INPUT
[ -n "$INPUT" ] && APACHE_NAME=${INPUT}
# write setting to config file
echo "APACHE_NAME=$APACHE_NAME" >> ${CONFIG_FILE}
fi
# apache email addr of committer (you)
if [ -z "$APACHE_EMAIL" ]; then
APACHE_EMAIL=${APACHE_NAME}@apache.org
read -p " your apache email [$APACHE_EMAIL]: " INPUT
[ -n "$INPUT" ] && APACHE_EMAIL=${INPUT}
# write setting to config file, so it is not needed next time
echo "APACHE_EMAIL=$APACHE_EMAIL" >> ${CONFIG_FILE}
fi
}
#
# Provide the user with a choice of the metron or bro repositories
#
function choose_metron_or_bro_repo {
# which repo? metron or metron-bro-plugin-kafka
echo " [1] metron"
echo " [2] metron-bro-plugin-kafka"
read -p " which repo? [1]: " INPUT
case "${INPUT}" in
[Bb][Rr][Oo]|[Mm][Ee][Tt][Rr][Oo][Nn]-[Bb][Rr][Oo]-[Pp][Ll][Uu][Gg][Ii][Nn]-[Kk][Aa][Ff][Kk][Aa]|*metron-bro-plugin-kafka\.git|2)
INPUT="${BRO_PLUGIN_UPSTREAM}" ;;
[Mm][Ee][Tt][Rr][Oo][Nn]|*metron\.git|1|'')
INPUT="${METRON_UPSTREAM}" ;;
*)
echo "Invalid repo, provided \"${INPUT}\". Please choose between metron or metron-bro-plugin-kafka"
exit 1
;;
esac
[ -n "$INPUT" ] && UPSTREAM=${INPUT}
CHOSEN_REPO=$(basename ${UPSTREAM%%.git})
}
#
# Ask the user for the PR number
#
function read_pull_request {
# retrieve the pull request identifier
read -p " pull request: " PR
if [ -z "$PR" ]; then
echo "Error: missing pr"
exit 1
fi
# ensure that the pull request exists
PR_EXISTS=`curl -sI https://api.github.com/repos/apache/${CHOSEN_REPO}/pulls/${PR} | grep Status: | sed 's/[^0-9]//g'`
if [ "$PR_EXISTS" != "200" ]; then
echo "Error: pull request #$PR does not exist"
exit 1
fi
}
#
# Setup the working directory.
# It is possible to override the default directory name by passing
# the desired directory name, using the available global variables
#
function setup_working_directory {
# working directory
if [ -z $1 ]; then
WORK=~/tmp/${CHOSEN_REPO}-pr${PR}
else
WORK=$1
fi
read -p " local working directory [$WORK]: " INPUT
[ -n "$INPUT" ] && WORK=${INPUT}
# handle tilde expansion
WORK="${WORK/#\~/$HOME}"
# warn the user if the working directory exists
if [ -d "$WORK" ]; then
read -p " directory exists [$WORK]. continue merge on existing repo? [yN] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
#
# If required will clone the desired repo. The default repo is metron, but an override
# may be passed as a parameter.
# If the directory already exists, it will ask the user for the name of the remote to use, with the
# default being "origin"
#
function setup_code {
# if working directory does not exist, checkout the base branch
if [ ! -d "$WORK" ]; then
REPO_NAME="metron"
if [ -z $1 ]; then
REPO_NAME=$1
fi
# origin repository
ORIGIN="https://github.com/apache/${REPO_NAME}"
read -p " origin repo [$ORIGIN]: " INPUT
[ -n "$INPUT" ] && ORIGIN=${INPUT}
# what branch did the PR get submitted against? could be a feature branch
BASE_BRANCH=`curl -s https://api.github.com/repos/apache/${REPO_NAME}/pulls/${PR} | python -c 'import sys, json; print json.load(sys.stdin)["base"]["ref"]'`
read -p " base branch to merge into [$BASE_BRANCH]: " INPUT
[ -n "$INPUT" ] && BASE_BRANCH=${INPUT}
# clone the repository and fetch updates
mkdir -p ${WORK}
git clone ${ORIGIN} ${WORK}
cd ${WORK} || exit "Failed to cd to ${WORK}"
# setup the git user and email for your apache account
git config user.name "$APACHE_NAME"
git config user.email "$APACHE_EMAIL"
# fetch any changes from upstream
git remote add upstream ${UPSTREAM}
if git fetch upstream "$BASE_BRANCH"; then
if [ ${BASE_BRANCH} = "master" ]; then
# merge any changes from upstream
git checkout ${BASE_BRANCH}
git merge upstream/${BASE_BRANCH}
else
# create a local branch from the remote feature branch
git checkout -B ${BASE_BRANCH} upstream/${BASE_BRANCH}
fi
else
# unable to fetch the base branch
exit $?
fi
else
# if the repo already exists, allow the user to provide the name of the Github remote
# this is needed to checkout the code for the PR
read -p " name of github remote [$GITHUB_REMOTE]: " INPUT
[ -n "$INPUT" ] && GITHUB_REMOTE=${INPUT}
fi
PR_BRANCH_REF="pull/$PR/head:pr-$PR"
PR_BRANCH="pr-$PR"
cd ${WORK} || exit "failed to move to ${WORK}"
git fetch ${GITHUB_REMOTE} ${PR_BRANCH_REF}
echo ""
}
#
# Populates the contributor information from the PR information
#
function get_contributor_info {
# use github api to retrieve the contributor's login
USER=`curl -s https://api.github.com/repos/apache/${CHOSEN_REPO}/pulls/${PR} | grep login | head -1 | awk -F":" '{print $2}' | sed 's/[^a-zA-Z.@_-]//g'`
read -p " github contributor's username [$USER]: " INPUT
[ -n "$INPUT" ] && USER=${INPUT}
# validate the github contributor
if [ -z "$USER" ]; then
echo "Error: missing username"
exit 1
fi
# retrieve the contributor's email from the git commit history
EMAIL=`git log ${PR_BRANCH} | grep Author | head -1 | awk -F"<" '{print $2}' | sed 's/[<>]//g'`
read -p " github contributor's email [$EMAIL]: " INPUT
[ -n "$INPUT" ] && EMAIL=${INPUT}
# validate email
if [ -z "$EMAIL" ] || [ "$EMAIL" = "null" ]; then
echo "Error: missing email"
exit 1
fi
}
#
# Populate the information for the JIRA associated with the PR
#
function get_jira_info {
# can we extract the JIRA from the PR title?
JIRA=`curl -s https://api.github.com/repos/apache/${CHOSEN_REPO}/pulls/${PR} | grep title | head -1 | egrep -o -i 'METRON-[0-9]+' | awk '{print toupper($0)}'`
read -p " issue identifier in jira [$JIRA]: " INPUT
[ -n "$INPUT" ] && JIRA=${INPUT}
# validate the JIRA issue
if [ -z "$JIRA" ]; then
echo "Error: missing jira"
exit 1
fi
# attempt to use the jira api to get a description of the jira
DESC=`curl -s https://issues.apache.org/jira/si/jira.issueviews:issue-xml/${JIRA}/${JIRA}.xml | grep "<summary>" | sed 's/^.*<summary>//' | sed 's/<.summary>.*$//'`
read -p " issue description [$DESC]: " INPUT
[ -n "$INPUT" ] && DESC=${INPUT}
# validate description
if [ -z "$DESC" ]; then
echo "Error: missing description"
exit 1
fi
}
#
# Merge and Commit
#
function commit {
# commit message
AUTHOR="$USER <$EMAIL>"
if [ "$USER" == "$GITHUB_NAME" ]; then
MSG="$JIRA $DESC ($USER) closes apache/${CHOSEN_REPO}#$PR"
else
MSG="$JIRA $DESC ($USER via $GITHUB_NAME) closes apache/${CHOSEN_REPO}#$PR"
fi
read -p " commit message [$MSG]: " INPUT
[ -n "$INPUT" ] && MSG=${INPUT}
# merge the contributor's branch and commit
echo ""
if git merge --squash "$PR_BRANCH"; then
git commit --author="$AUTHOR" -a -m "$MSG"
else
exit $?
fi
}
#
# Displays the commit info ( the diff and log )
#
function review_commit_info {
# review the commit
echo ""
echo ""
git diff --stat --color "upstream/$BASE_BRANCH..$BASE_BRANCH"
echo ""
echo ""
git log --oneline "$BASE_BRANCH" "^upstream/$BASE_BRANCH"
}
#
# Runs the metron unit, integration and metron-interface tests
#
function run_tests {
echo ""
echo ""
read -p " run test suite? [yN] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ "${UPSTREAM}" == "${METRON_UPSTREAM}" ]; then
mvn -q -T 2C -DskipTests clean install &&
mvn -q -T 2C surefire:test@unit-tests &&
mvn -q surefire:test@integration-tests &&
mvn -q test --projects metron-interface/metron-config &&
dev-utilities/build-utils/verify_licenses.sh
elif [ "${UPSTREAM}" == "${BRO_PLUGIN_UPSTREAM}" ]; then
echo "We don't currently support running metron-bro-plugin-kafka tests in this script"
fi
fi
}
#
# Gives the user instruction on next steps
#
function please_review_then {
echo ""
echo "Review commit carefully then run..."
echo " cd $WORK"
echo " git push upstream $BASE_BRANCH"
echo ""
}