From dcf7e944d12baade3861736001ef7e60e529ddcc Mon Sep 17 00:00:00 2001 From: asingh Date: Tue, 28 Apr 2015 14:32:14 -0700 Subject: [PATCH] KAFKA-2153: kafka-patch-review tool uploads a patch even if it is empty --- dev-utils/test-patch.py | 23 ++++++++++++++++------- kafka-patch-review.py | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/dev-utils/test-patch.py b/dev-utils/test-patch.py index 9d88a6e..66ec79f 100644 --- a/dev-utils/test-patch.py +++ b/dev-utils/test-patch.py @@ -328,6 +328,8 @@ parser.add_option("--patch-command", dest="patch_cmd", default="git apply", help="Patch command such as `git apply' or `patch'", metavar="COMMAND") parser.add_option("-p", "--strip", dest="strip", default="1", help="Remove leading slashes from diff paths", metavar="N") +parser.add_option("--get-latest-patch", dest="get_latest_patch", + help="Get the latest patch attached to JIRA", action="store_true") (options, args) = parser.parse_args() if not (options.defect or options.filename): @@ -342,6 +344,10 @@ if options.post_results and not options.password: print "FATAL: --post-results requires --password" sys.exit(1) +if options.get_latest_patch and not options.defect: + print "FATAL: --get-latest-patch requires --defect" + sys.exit(1) + branch = options.branch if options.output_dir and not options.output_dir.startswith('/'): print "INFO: A temporary staging dir for output will be used to avoid deletion of output files during 'git reset'" @@ -356,6 +362,7 @@ password = options.password run_tests = options.run_tests post_results = options.post_results strip = options.strip +get_latest_patch = options.get_latest_patch patch_cmd = options.patch_cmd result = Result() @@ -397,12 +404,11 @@ if output_dir.endswith("/"): if output_dir and not os.path.isdir(output_dir): os.makedirs(output_dir) -# If defect parameter is specified let's download the latest attachment -if defect: +def get_latest_patch(): + global jira_json, json, versions, branch, attachment, patch_contents, patch_file, fh print "Defect: %s" % defect jira_json = jira_get_defect(result, defect, username, password) json = json.loads(jira_json) - # JIRA must be in Patch Available state if '"Patch Available"' not in jira_json: print "ERROR: Defect %s not in patch available state" % (defect) @@ -417,19 +423,22 @@ if defect: sys.exit(1) else: print "INFO: Guessed branch as %s" % (branch) - attachment = jira_get_attachment(result, defect, username, password) if not attachment: print "ERROR: No attachments found for %s" % (defect) sys.exit(1) - result.attachment = attachment - patch_contents = jira_request(result, result.attachment, username, password, None, {}).read() patch_file = "%s/%s.patch" % (output_dir, defect) - with open(patch_file, 'a') as fh: fh.write(patch_contents) + +if defect: + # If defect parameter is specified let's download the latest attachment + get_latest_patch() + if options.get_latest_patch: + print "Saving latest attachment of %s as %s/%s.patch" % (defect, output_dir, defect) + sys.exit(0) elif options.filename: patch_file = options.filename else: diff --git a/kafka-patch-review.py b/kafka-patch-review.py index 9592680..b5a2e95 100644 --- a/kafka-patch-review.py +++ b/kafka-patch-review.py @@ -99,6 +99,37 @@ def main(): print "Failed to login to the JIRA instance", sys.exc_info()[0], sys.exc_info()[1] sys.exit(1) + git_command="git format-patch " + opt.branch + " --stdout > " + patch_file + if opt.debug: + print git_command + p=os.popen(git_command) + p.close() + + print 'Getting latest patch attached to the JIRA' + tmp_dir = tempfile.mkdtemp() + get_latest_patch_command="python ./dev-utils/test-patch.py --get-latest-patch --defect " + opt.jira + " --output " + tmp_dir + " > /dev/null 2>&1" + p=os.popen(get_latest_patch_command) + p.close() + + previous_patch=tmp_dir + "/" + opt.jira + ".patch" + diff_file=tmp_dir + "/" + opt.jira + ".diff" + if os.path.isfile(previous_patch) and os.stat(previous_patch).st_size > 0: + print 'Creating diff with previous version of patch uploaded to JIRA' + diff_command = "diff " + previous_patch+ " " + patch_file + " > " + diff_file + try: + p=os.popen(diff_command) + sys.stdout.flush() + p.close() + except: + pass + print 'Diff with previous version of patch uploaded to JIRA is saved to ' + diff_file + + print 'Checking if the there are changes that need to be pushed' + if os.stat(diff_file).st_size == 0: + print 'No changes found on top of changes uploaded to JIRA' + print 'Aborting' + sys.exit(1) + rb_command= post_review_tool + " --publish --tracking-branch " + opt.branch + " --target-groups=kafka --bugs-closed=" + opt.jira if opt.debug: rb_command=rb_command + " --debug" @@ -134,12 +165,6 @@ def main(): if opt.debug: print 'rb url=',rb_url - git_command="git format-patch " + opt.branch + " --stdout > " + patch_file - if opt.debug: - print git_command - p=os.popen(git_command) - p.close() - print 'Creating diff against', opt.branch, 'and uploading patch to JIRA',opt.jira issue = jira.issue(opt.jira) attachment=open(patch_file) -- 2.3.2 (Apple Git-55)