in zk-merge-pr.py [0:0]
def main():
global original_head
original_head = get_current_branch()
check_jira_env()
check_git_remote()
branches = get_json("%s/branches" % GITHUB_API_BASE)
branch_names = [x for x in [x['name'] for x in branches] if x.startswith(RELEASE_BRANCH_PREFIX)]
# Assumes branch names can be sorted lexicographically
latest_branch = sorted(branch_names, reverse=True)[0]
pr_num = input("Which pull request would you like to merge? (e.g. 34): ")
pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num))
# Check if the pull request has already been closed or merged.
pull_request_state = pr.get("state", "")
if pull_request_state == "closed":
merge_hash = pr.get("merge_commit_sha", "")
merged = pr.get("merged")
# Verify if the pull request has been merged by the GitHub API.
if merged is True:
print(f"Pull request #{pr['number']} has already been merged, assuming you want to backport")
cherry_pick(pr_num, merge_hash, latest_branch)
sys.exit(0)
# Some merged pull requests may not appear as merged in the GitHub API,
# for example, those closed by an older version of this script.
else:
pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num))
for event in pr_events:
if event.get("event") == "closed":
commit_id = event.get("commit_id")
if commit_id is not None:
print(f"Pull request #{pr['number']} has already been merged, assuming you want to backport")
cherry_pick(pr_num, merge_hash, latest_branch)
sys.exit(0)
else:
print(f"Pull request #{pr['number']} has already been closed, but not merged, exiting.")
exit()
if not bool(pr["mergeable"]):
print(f"Pull request %s is not mergeable in its current form.\n" % pr_num)
exit()
url = pr["url"]
pr_title = pr["title"]
commit_title = input("Commit title [%s]: " % pr_title)
if commit_title == "":
commit_title = pr_title
# Decide whether to use the modified title or not
modified_title = standardize_jira_ref(commit_title)
if modified_title != commit_title:
print("I've re-written the title as follows to match the standard format:")
print("Original: %s" % commit_title)
print("Modified: %s" % modified_title)
result = input("Would you like to use the modified title? (y/n): ")
if result.lower().strip() == "y":
commit_title = modified_title
print("Using modified title:")
else:
print("Using original title:")
print(commit_title)
target_ref = pr["base"]["ref"]
user_login = pr["user"]["login"]
base_ref = pr["head"]["ref"]
pr_repo_desc = "%s/%s" % (user_login, base_ref)
print(("\n=== Pull Request #%s ===" % pr_num))
print(("PR title\t%s\nCommit title\t%s\nSource\t\t%s\nTarget\t\t%s\nURL\t\t%s" % (
pr_title, commit_title, pr_repo_desc, target_ref, url)))
continue_maybe("Proceed with merging pull request #%s?" % pr_num)
merged_refs = [target_ref]
merge_hash = merge_pr(pr_num, commit_title, pr_repo_desc)
pick_prompt = "Would you like to pick %s into another branch?" % merge_hash
while input("\n%s (y/n): " % pick_prompt).lower().strip() == "y":
merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, latest_branch)]
if JIRA_IMPORTED:
if (JIRA_ACCESS_TOKEN is not None) or (JIRA_USERNAME and JIRA_PASSWORD):
continue_maybe("Would you like to update an associated JIRA?")
jira_comment = "Issue resolved by pull request %s\n[%s/%s]" % (pr_num, GITHUB_BASE, pr_num)
resolve_jira_issues(commit_title, merged_refs, jira_comment)
else:
print("Neither JIRA_ACCESS_TOKEN nor JIRA_USERNAME and/or JIRA_PASSWORD are set.")
print("Exiting without trying to close the associated JIRA.")
else:
print("Could not find jira-python library. Run 'sudo pip install jira' to install.")
print("Exiting without trying to close the associated JIRA.")