|
| 1 | +import os, re |
| 2 | +from vars import input_data, upstream_repo, cherrypick_with_commits_infos |
| 3 | +from functions import cherry_pick, create_pr, issue_comment, get_pr_body, PushCpException, push_to_branch, get_middle_text |
| 4 | + |
| 5 | +milestone_title = os.environ["INPUT_MILESTONE_TITLE"] |
| 6 | +milestoned_issue_number = os.environ["INPUT_MILESTONED_ISSUE_NUMBER"] |
| 7 | +issue_title = os.environ['INPUT_ISSUE_TITLE'] |
| 8 | +issue_body = os.environ["INPUT_ISSUE_BODY"] |
| 9 | +commits_text = cherrypick_with_commits_infos["commits"] |
| 10 | +team_labels_text = cherrypick_with_commits_infos["team_labels"] |
| 11 | +reviewers_text = cherrypick_with_commits_infos["reviewers"] |
| 12 | +issue_body_dict = {} |
| 13 | +issue_body_dict["commits"] = get_middle_text(issue_body, commits_text["left"], commits_text["right"]).replace(" ", "").split(",") |
| 14 | + |
| 15 | +for commit_index in range(len(issue_body_dict["commits"])): |
| 16 | + issue_body_dict["commits"][commit_index] = re.sub(r'https://.*/commit/', "", issue_body_dict["commits"][commit_index]) |
| 17 | + |
| 18 | +issue_body_dict["labels"] = get_middle_text(issue_body, team_labels_text["left"], team_labels_text["right"]).replace(" ", "").replace("@", "").split(",") |
| 19 | +issue_body_dict["reviewers"] = get_middle_text(issue_body, reviewers_text["left"], reviewers_text["right"]).replace(" ", "").replace("@", "").split(",") |
| 20 | + |
| 21 | +release_number = milestone_title.split(" release blockers")[0] |
| 22 | +release_branch_name = f"{input_data['release_branch_name_initials']}{release_number}" |
| 23 | +target_branch_name = f"cp_ondemand_{milestoned_issue_number}-{release_number}" |
| 24 | +head_branch_name = f"{input_data['user_name']}:{target_branch_name}" |
| 25 | +reviewers = issue_body_dict["reviewers"] |
| 26 | +labels = issue_body_dict["labels"] |
| 27 | +requires_clone = True |
| 28 | +requires_checkout = True |
| 29 | +successful_commits = [] |
| 30 | +failed_commits = [] |
| 31 | + |
| 32 | +for idx, commit_id in enumerate(issue_body_dict["commits"]): |
| 33 | + try: |
| 34 | + cherry_pick(commit_id, release_branch_name, target_branch_name, requires_clone, requires_checkout, input_data) |
| 35 | + msg_body = get_pr_body(commit_id, input_data["api_repo_name"]) |
| 36 | + success_msg = {"commit_id": commit_id, "msg": msg_body} |
| 37 | + successful_commits.append(success_msg) |
| 38 | + except PushCpException as pe: |
| 39 | + issue_comment(milestoned_issue_number, str(pe), input_data["api_repo_name"], input_data["is_prod"]) |
| 40 | + raise SystemExit(0) |
| 41 | + except Exception as e: |
| 42 | + failed_commits.append(commit_id) |
| 43 | + requires_clone = False |
| 44 | + requires_checkout = False |
| 45 | + |
| 46 | +try: |
| 47 | + push_to_branch(target_branch_name) |
| 48 | +except Exception as e: |
| 49 | + issue_comment(milestoned_issue_number, str(e), input_data["api_repo_name"], input_data["is_prod"]) |
| 50 | + raise SystemExit(0) |
| 51 | + |
| 52 | +issue_comment_body = "" |
| 53 | +if len(successful_commits): |
| 54 | + if len(successful_commits) >= 2: |
| 55 | + pr_body = f"This PR contains {len(successful_commits)} commit(s).\n\n" |
| 56 | + for idx, commit in enumerate(successful_commits): |
| 57 | + pr_body += str((idx + 1)) + ")" + commit["msg"] + "\n\n" |
| 58 | + elif len(successful_commits) == 1: |
| 59 | + pr_body = f"{successful_commits[0]['msg']}" |
| 60 | + if "awaiting-review" not in labels: labels.append("awaiting-review") |
| 61 | + cherry_picked_pr_number = create_pr(reviewers, release_number, labels, issue_title, pr_body, release_branch_name, target_branch_name, input_data['user_name']) |
| 62 | + issue_comment_body = f"The following commits were cherry-picked in https://github.com/{upstream_repo}/pull/{cherry_picked_pr_number}: " |
| 63 | + for success_commit in successful_commits: |
| 64 | + issue_comment_body += f"https://github.com/{input_data['api_repo_name']}/commit/{success_commit['commit_id']}, " |
| 65 | + issue_comment_body = issue_comment_body[::-1].replace(" ,", ".", 1)[::-1] |
| 66 | + |
| 67 | + if len(failed_commits): |
| 68 | + failure_commits_str = f"\nFailed commits (likely due to merge conflicts): " |
| 69 | + for fail_commit in failed_commits: |
| 70 | + failure_commits_str += f"https://github.com/{input_data['api_repo_name']}/commit/{fail_commit}, " |
| 71 | + failure_commits_str = failure_commits_str[::-1].replace(" ,", "", 1)[::-1] |
| 72 | + failure_commits_str += "\n\nThe failed commits are NOT included in the PR. Please resolve manually.\ncc: @bazelbuild/triage" |
| 73 | + issue_comment_body += failure_commits_str |
| 74 | +elif len(failed_commits): |
| 75 | + issue_comment_body = "Failed commmits (likely due to merge conflicts): " |
| 76 | + for fail_commit in failed_commits: |
| 77 | + issue_comment_body += f"https://github.com/{input_data['api_repo_name']}/commit/{fail_commit}, " |
| 78 | + issue_comment_body = issue_comment_body[::-1].replace(" ,", ".", 1)[::-1] + "\nPlease resolve manually.\ncc: @bazelbuild/triage" |
| 79 | +issue_comment(milestoned_issue_number, issue_comment_body, input_data["api_repo_name"], input_data["is_prod"]) |
0 commit comments