Skip to content

Commit aa20a6e

Browse files
committed
Move code into main() and add doctest for new text parsing method
Also corrected a spelling mistake in one of the print statements.
1 parent 48520ba commit aa20a6e

File tree

1 file changed

+78
-65
lines changed

1 file changed

+78
-65
lines changed

dev/merge_spark_pr.py

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@
5656
# Prefix added to temporary branches
5757
BRANCH_PREFIX = "PR_TOOL"
5858

59-
os.chdir(SPARK_HOME)
60-
6159

6260
def get_json(url):
6361
try:
@@ -86,10 +84,6 @@ def continue_maybe(prompt):
8684
if result.lower() != "y":
8785
fail("Okay, exiting")
8886

89-
90-
original_head = run_cmd("git rev-parse HEAD")[:8]
91-
92-
9387
def clean_up():
9488
print "Restoring head pointer to %s" % original_head
9589
run_cmd("git checkout %s" % original_head)
@@ -275,7 +269,7 @@ def get_version_json(version_str):
275269
asf_jira.transition_issue(
276270
jira_id, resolve["id"], fixVersions=jira_fix_versions, comment=comment)
277271

278-
print "Succesfully resolved %s with fixVersions=%s!" % (jira_id, fix_versions)
272+
print "Successfully resolved %s with fixVersions=%s!" % (jira_id, fix_versions)
279273

280274

281275
def resolve_jira_issues(title, merge_branches, comment):
@@ -286,10 +280,19 @@ def resolve_jira_issues(title, merge_branches, comment):
286280
for jira_id in jira_ids:
287281
resolve_jira_issue(merge_branches, comment, jira_id)
288282

283+
289284
def standardize_jira_ref(text):
290-
# Standardize the [MODULE] SPARK-XXXXX prefix
291-
# Converts "[SPARK-XXX][mllib] Issue", "[MLLib] SPARK-XXX. Issue" or "SPARK XXX [MLLIB]: Issue" to "[MLLIB] SPARK-XXX: Issue"
285+
"""
286+
Standardize the [MODULE] SPARK-XXXXX prefix
287+
Converts "[SPARK-XXX][mllib] Issue", "[MLLib] SPARK-XXX. Issue" or "SPARK XXX [MLLIB]: Issue" to "[MLLIB] SPARK-XXX: Issue"
292288
289+
>>> standardize_jira_ref("[SPARK-5821] [SQL] ParquetRelation2 CTAS should check if delete is successful")
290+
'[SQL] SPARK-5821: ParquetRelation2 CTAS should check if delete is successful'
291+
>>> standardize_jira_ref("[SPARK-4123][Project Infra][WIP]: Show new dependencies added in pull requests")
292+
'[PROJECT INFRA] [WIP] SPARK-4123: Show new dependencies added in pull requests'
293+
>>> standardize_jira_ref("[MLlib] Spark 5954: Top by key")
294+
'[MLLIB] SPARK-5954: Top by key'
295+
"""
293296
#If the string is compliant, no need to process any further
294297
if (re.search(r'\[[A-Z0-9_]+\] SPARK-[0-9]{3,5}: \S+', text)):
295298
return text
@@ -300,7 +303,7 @@ def standardize_jira_ref(text):
300303
while (pattern.search(text) is not None):
301304
ref = pattern.search(text).groups()[0]
302305
# Replace any whitespace with a dash & convert to uppercase
303-
jira_refs.append(re.sub(r'\s', '-', ref.upper()))
306+
jira_refs.append(re.sub(r'\s+', '-', ref.upper()))
304307
text = text.replace(ref, '')
305308

306309
# Extract spark component(s):
@@ -337,68 +340,78 @@ def standardize_jira_ref(text):
337340

338341
return clean_text
339342

340-
branches = get_json("%s/branches" % GITHUB_API_BASE)
341-
branch_names = filter(lambda x: x.startswith("branch-"), [x['name'] for x in branches])
342-
# Assumes branch names can be sorted lexicographically
343-
latest_branch = sorted(branch_names, reverse=True)[0]
344-
345-
pr_num = raw_input("Which pull request would you like to merge? (e.g. 34): ")
346-
pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num))
347-
pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num))
348-
349-
url = pr["url"]
350-
title = standardize_jira_ref(pr["title"])
351-
body = pr["body"]
352-
target_ref = pr["base"]["ref"]
353-
user_login = pr["user"]["login"]
354-
base_ref = pr["head"]["ref"]
355-
pr_repo_desc = "%s/%s" % (user_login, base_ref)
356-
357-
# Merged pull requests don't appear as merged in the GitHub API;
358-
# Instead, they're closed by asfgit.
359-
merge_commits = \
360-
[e for e in pr_events if e["actor"]["login"] == "asfgit" and e["event"] == "closed"]
361-
362-
if merge_commits:
363-
merge_hash = merge_commits[0]["commit_id"]
364-
message = get_json("%s/commits/%s" % (GITHUB_API_BASE, merge_hash))["commit"]["message"]
365-
366-
print "Pull request %s has already been merged, assuming you want to backport" % pr_num
367-
commit_is_downloaded = run_cmd(['git', 'rev-parse', '--quiet', '--verify',
343+
def main():
344+
os.chdir(SPARK_HOME)
345+
original_head = run_cmd("git rev-parse HEAD")[:8]
346+
347+
branches = get_json("%s/branches" % GITHUB_API_BASE)
348+
branch_names = filter(lambda x: x.startswith("branch-"), [x['name'] for x in branches])
349+
# Assumes branch names can be sorted lexicographically
350+
latest_branch = sorted(branch_names, reverse=True)[0]
351+
352+
pr_num = raw_input("Which pull request would you like to merge? (e.g. 34): ")
353+
pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num))
354+
pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num))
355+
356+
url = pr["url"]
357+
title = standardize_jira_ref(pr["title"])
358+
body = pr["body"]
359+
target_ref = pr["base"]["ref"]
360+
user_login = pr["user"]["login"]
361+
base_ref = pr["head"]["ref"]
362+
pr_repo_desc = "%s/%s" % (user_login, base_ref)
363+
364+
# Merged pull requests don't appear as merged in the GitHub API;
365+
# Instead, they're closed by asfgit.
366+
merge_commits = \
367+
[e for e in pr_events if e["actor"]["login"] == "asfgit" and e["event"] == "closed"]
368+
369+
if merge_commits:
370+
merge_hash = merge_commits[0]["commit_id"]
371+
message = get_json("%s/commits/%s" % (GITHUB_API_BASE, merge_hash))["commit"]["message"]
372+
373+
print "Pull request %s has already been merged, assuming you want to backport" % pr_num
374+
commit_is_downloaded = run_cmd(['git', 'rev-parse', '--quiet', '--verify',
368375
"%s^{commit}" % merge_hash]).strip() != ""
369-
if not commit_is_downloaded:
370-
fail("Couldn't find any merge commit for #%s, you may need to update HEAD." % pr_num)
376+
if not commit_is_downloaded:
377+
fail("Couldn't find any merge commit for #%s, you may need to update HEAD." % pr_num)
371378

372-
print "Found commit %s:\n%s" % (merge_hash, message)
373-
cherry_pick(pr_num, merge_hash, latest_branch)
374-
sys.exit(0)
379+
print "Found commit %s:\n%s" % (merge_hash, message)
380+
cherry_pick(pr_num, merge_hash, latest_branch)
381+
sys.exit(0)
375382

376-
if not bool(pr["mergeable"]):
377-
msg = "Pull request %s is not mergeable in its current form.\n" % pr_num + \
378-
"Continue? (experts only!)"
379-
continue_maybe(msg)
383+
if not bool(pr["mergeable"]):
384+
msg = "Pull request %s is not mergeable in its current form.\n" % pr_num + \
385+
"Continue? (experts only!)"
386+
continue_maybe(msg)
380387

381-
print ("\n=== Pull Request #%s ===" % pr_num)
382-
print ("title\t%s\nsource\t%s\ntarget\t%s\nurl\t%s" % (
383-
title, pr_repo_desc, target_ref, url))
384-
continue_maybe("Proceed with merging pull request #%s?" % pr_num)
388+
print ("\n=== Pull Request #%s ===" % pr_num)
389+
print ("title\t%s\nsource\t%s\ntarget\t%s\nurl\t%s" % (
390+
title, pr_repo_desc, target_ref, url))
391+
continue_maybe("Proceed with merging pull request #%s?" % pr_num)
385392

386-
merged_refs = [target_ref]
393+
merged_refs = [target_ref]
387394

388-
merge_hash = merge_pr(pr_num, target_ref)
395+
merge_hash = merge_pr(pr_num, target_ref)
389396

390-
pick_prompt = "Would you like to pick %s into another branch?" % merge_hash
391-
while raw_input("\n%s (y/n): " % pick_prompt).lower() == "y":
392-
merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, latest_branch)]
397+
pick_prompt = "Would you like to pick %s into another branch?" % merge_hash
398+
while raw_input("\n%s (y/n): " % pick_prompt).lower() == "y":
399+
merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, latest_branch)]
393400

394-
if JIRA_IMPORTED:
395-
if JIRA_USERNAME and JIRA_PASSWORD:
396-
continue_maybe("Would you like to update an associated JIRA?")
397-
jira_comment = "Issue resolved by pull request %s\n[%s/%s]" % (pr_num, GITHUB_BASE, pr_num)
398-
resolve_jira_issues(title, merged_refs, jira_comment)
401+
if JIRA_IMPORTED:
402+
if JIRA_USERNAME and JIRA_PASSWORD:
403+
continue_maybe("Would you like to update an associated JIRA?")
404+
jira_comment = "Issue resolved by pull request %s\n[%s/%s]" % (pr_num, GITHUB_BASE, pr_num)
405+
resolve_jira_issues(title, merged_refs, jira_comment)
406+
else:
407+
print "JIRA_USERNAME and JIRA_PASSWORD not set"
408+
print "Exiting without trying to close the associated JIRA."
399409
else:
400-
print "JIRA_USERNAME and JIRA_PASSWORD not set"
410+
print "Could not find jira-python library. Run 'sudo pip install jira-python' to install."
401411
print "Exiting without trying to close the associated JIRA."
402-
else:
403-
print "Could not find jira-python library. Run 'sudo pip install jira-python' to install."
404-
print "Exiting without trying to close the associated JIRA."
412+
413+
if __name__ == "__main__":
414+
import doctest
415+
doctest.testmod()
416+
417+
main()

0 commit comments

Comments
 (0)