Skip to content

Commit 944628b

Browse files
Ashish SinghNeha Narkhede
Ashish Singh
authored and
Neha Narkhede
committed
KAFKA-2153 kafka-patch-review tool uploads a patch even if it is empty; reviewed by Neha Narkhede, Gwen Shapira
1 parent e40ebcf commit 944628b

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

dev-utils/test-patch.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ def exit(self):
328328
help="Patch command such as `git apply' or `patch'", metavar="COMMAND")
329329
parser.add_option("-p", "--strip", dest="strip", default="1",
330330
help="Remove <n> leading slashes from diff paths", metavar="N")
331+
parser.add_option("--get-latest-patch", dest="get_latest_patch",
332+
help="Get the latest patch attached to JIRA", action="store_true")
331333

332334
(options, args) = parser.parse_args()
333335
if not (options.defect or options.filename):
@@ -342,6 +344,10 @@ def exit(self):
342344
print "FATAL: --post-results requires --password"
343345
sys.exit(1)
344346

347+
if options.get_latest_patch and not options.defect:
348+
print "FATAL: --get-latest-patch requires --defect"
349+
sys.exit(1)
350+
345351
branch = options.branch
346352
if options.output_dir and not options.output_dir.startswith('/'):
347353
print "INFO: A temporary staging dir for output will be used to avoid deletion of output files during 'git reset'"
@@ -356,6 +362,7 @@ def exit(self):
356362
run_tests = options.run_tests
357363
post_results = options.post_results
358364
strip = options.strip
365+
get_latest_patch = options.get_latest_patch
359366
patch_cmd = options.patch_cmd
360367
result = Result()
361368

@@ -397,12 +404,11 @@ def post_jira_comment_and_exit():
397404
if output_dir and not os.path.isdir(output_dir):
398405
os.makedirs(output_dir)
399406

400-
# If defect parameter is specified let's download the latest attachment
401-
if defect:
407+
def get_latest_patch():
408+
global jira_json, json, versions, branch, attachment, patch_contents, patch_file, fh
402409
print "Defect: %s" % defect
403410
jira_json = jira_get_defect(result, defect, username, password)
404411
json = json.loads(jira_json)
405-
406412
# JIRA must be in Patch Available state
407413
if '"Patch Available"' not in jira_json:
408414
print "ERROR: Defect %s not in patch available state" % (defect)
@@ -417,19 +423,22 @@ def post_jira_comment_and_exit():
417423
sys.exit(1)
418424
else:
419425
print "INFO: Guessed branch as %s" % (branch)
420-
421426
attachment = jira_get_attachment(result, defect, username, password)
422427
if not attachment:
423428
print "ERROR: No attachments found for %s" % (defect)
424429
sys.exit(1)
425-
426430
result.attachment = attachment
427-
428431
patch_contents = jira_request(result, result.attachment, username, password, None, {}).read()
429432
patch_file = "%s/%s.patch" % (output_dir, defect)
430-
431433
with open(patch_file, 'a') as fh:
432434
fh.write(patch_contents)
435+
436+
if defect:
437+
# If defect parameter is specified let's download the latest attachment
438+
get_latest_patch()
439+
if options.get_latest_patch:
440+
print "Saving latest attachment of %s as %s/%s.patch" % (defect, output_dir, defect)
441+
sys.exit(0)
433442
elif options.filename:
434443
patch_file = options.filename
435444
else:

kafka-patch-review.py

+31-6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,37 @@ def main():
9999
print "Failed to login to the JIRA instance", sys.exc_info()[0], sys.exc_info()[1]
100100
sys.exit(1)
101101

102+
git_command="git format-patch " + opt.branch + " --stdout > " + patch_file
103+
if opt.debug:
104+
print git_command
105+
p=os.popen(git_command)
106+
p.close()
107+
108+
print 'Getting latest patch attached to the JIRA'
109+
tmp_dir = tempfile.mkdtemp()
110+
get_latest_patch_command="python ./dev-utils/test-patch.py --get-latest-patch --defect " + opt.jira + " --output " + tmp_dir + " > /dev/null 2>&1"
111+
p=os.popen(get_latest_patch_command)
112+
p.close()
113+
114+
previous_patch=tmp_dir + "/" + opt.jira + ".patch"
115+
diff_file=tmp_dir + "/" + opt.jira + ".diff"
116+
if os.path.isfile(previous_patch) and os.stat(previous_patch).st_size > 0:
117+
print 'Creating diff with previous version of patch uploaded to JIRA'
118+
diff_command = "diff " + previous_patch+ " " + patch_file + " > " + diff_file
119+
try:
120+
p=os.popen(diff_command)
121+
sys.stdout.flush()
122+
p.close()
123+
except:
124+
pass
125+
print 'Diff with previous version of patch uploaded to JIRA is saved to ' + diff_file
126+
127+
print 'Checking if the there are changes that need to be pushed'
128+
if os.stat(diff_file).st_size == 0:
129+
print 'No changes found on top of changes uploaded to JIRA'
130+
print 'Aborting'
131+
sys.exit(1)
132+
102133
rb_command= post_review_tool + " --publish --tracking-branch " + opt.branch + " --target-groups=kafka --bugs-closed=" + opt.jira
103134
if opt.debug:
104135
rb_command=rb_command + " --debug"
@@ -134,12 +165,6 @@ def main():
134165
if opt.debug:
135166
print 'rb url=',rb_url
136167

137-
git_command="git format-patch " + opt.branch + " --stdout > " + patch_file
138-
if opt.debug:
139-
print git_command
140-
p=os.popen(git_command)
141-
p.close()
142-
143168
print 'Creating diff against', opt.branch, 'and uploading patch to JIRA',opt.jira
144169
issue = jira.issue(opt.jira)
145170
attachment=open(patch_file)

0 commit comments

Comments
 (0)