|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +# Utility for creating well-formed pull request merges and pushing them to |
| 21 | +# Apache. |
| 22 | +# usage: ./apache-pr-merge.py (see config env vars below) |
| 23 | +# |
| 24 | +# Lightly modified from version of this script in incubator-parquet-format |
| 25 | + |
| 26 | +from __future__ import print_function |
| 27 | + |
| 28 | +from requests.auth import HTTPBasicAuth |
| 29 | +import requests |
| 30 | + |
| 31 | +import os |
| 32 | +import six |
| 33 | +import subprocess |
| 34 | +import sys |
| 35 | +import textwrap |
| 36 | + |
| 37 | +REPO_HOME = '.' |
| 38 | +PROJECT_NAME = 'pandas-design' |
| 39 | +print("REPO_HOME = " + REPO_HOME) |
| 40 | + |
| 41 | +# Remote name with the PR |
| 42 | +PR_REMOTE_NAME = os.environ.get("PR_REMOTE_NAME", "upstream") |
| 43 | + |
| 44 | +# Remote name where results pushed |
| 45 | +PUSH_REMOTE_NAME = os.environ.get("PUSH_REMOTE_NAME", "upstream") |
| 46 | + |
| 47 | +GITHUB_BASE = "https://github.com/pydata/" + PROJECT_NAME + "/pull" |
| 48 | +GITHUB_API_BASE = "https://api.github.com/repos/pydata/" + PROJECT_NAME |
| 49 | + |
| 50 | +# Prefix added to temporary branches |
| 51 | +BRANCH_PREFIX = "PR_TOOL" |
| 52 | + |
| 53 | +os.chdir(REPO_HOME) |
| 54 | + |
| 55 | +auth_required = False |
| 56 | + |
| 57 | +if auth_required: |
| 58 | + GITHUB_USERNAME = os.environ['GITHUB_USER'] |
| 59 | + import getpass |
| 60 | + GITHUB_PASSWORD = getpass.getpass('Enter github.com password for %s:' |
| 61 | + % GITHUB_USERNAME) |
| 62 | + |
| 63 | + def get_json_auth(url): |
| 64 | + auth = HTTPBasicAuth(GITHUB_USERNAME, GITHUB_PASSWORD) |
| 65 | + req = requests.get(url, auth=auth) |
| 66 | + return req.json() |
| 67 | + |
| 68 | + get_json = get_json_auth |
| 69 | +else: |
| 70 | + def get_json_no_auth(url): |
| 71 | + req = requests.get(url) |
| 72 | + return req.json() |
| 73 | + |
| 74 | + get_json = get_json_no_auth |
| 75 | + |
| 76 | + |
| 77 | +def fail(msg): |
| 78 | + print(msg) |
| 79 | + clean_up() |
| 80 | + sys.exit(-1) |
| 81 | + |
| 82 | + |
| 83 | +def run_cmd(cmd): |
| 84 | + # py2.6 does not have subprocess.check_output |
| 85 | + if isinstance(cmd, six.string_types): |
| 86 | + cmd = cmd.split(' ') |
| 87 | + |
| 88 | + popenargs = [cmd] |
| 89 | + kwargs = {} |
| 90 | + |
| 91 | + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs) |
| 92 | + output, unused_err = process.communicate() |
| 93 | + retcode = process.poll() |
| 94 | + if retcode: |
| 95 | + cmd = kwargs.get("args") |
| 96 | + if cmd is None: |
| 97 | + cmd = popenargs[0] |
| 98 | + raise subprocess.CalledProcessError(retcode, cmd, output=output) |
| 99 | + return output |
| 100 | + |
| 101 | + |
| 102 | +def continue_maybe(prompt): |
| 103 | + result = raw_input("\n%s (y/n): " % prompt) |
| 104 | + if result.lower() != "y": |
| 105 | + fail("Okay, exiting") |
| 106 | + |
| 107 | + |
| 108 | +original_head = run_cmd("git rev-parse HEAD")[:8] |
| 109 | + |
| 110 | + |
| 111 | +def clean_up(): |
| 112 | + print("Restoring head pointer to %s" % original_head) |
| 113 | + run_cmd("git checkout %s" % original_head) |
| 114 | + |
| 115 | + branches = run_cmd("git branch").replace(" ", "").split("\n") |
| 116 | + |
| 117 | + for branch in filter(lambda x: x.startswith(BRANCH_PREFIX), branches): |
| 118 | + print("Deleting local branch %s" % branch) |
| 119 | + run_cmd("git branch -D %s" % branch) |
| 120 | + |
| 121 | + |
| 122 | +# merge the requested PR and return the merge hash |
| 123 | +def merge_pr(pr_num, target_ref): |
| 124 | + pr_branch_name = "%s_MERGE_PR_%s" % (BRANCH_PREFIX, pr_num) |
| 125 | + target_branch_name = "%s_MERGE_PR_%s_%s" % (BRANCH_PREFIX, pr_num, |
| 126 | + target_ref.upper()) |
| 127 | + run_cmd("git fetch %s pull/%s/head:%s" % (PR_REMOTE_NAME, pr_num, |
| 128 | + pr_branch_name)) |
| 129 | + run_cmd("git fetch %s %s:%s" % (PUSH_REMOTE_NAME, target_ref, |
| 130 | + target_branch_name)) |
| 131 | + run_cmd("git checkout %s" % target_branch_name) |
| 132 | + |
| 133 | + had_conflicts = False |
| 134 | + try: |
| 135 | + run_cmd(['git', 'merge', pr_branch_name, '--squash']) |
| 136 | + except Exception as e: |
| 137 | + msg = ("Error merging: %s\nWould you like to manually fix-up " |
| 138 | + "this merge?" % e) |
| 139 | + continue_maybe(msg) |
| 140 | + msg = ("Okay, please fix any conflicts and 'git add' " |
| 141 | + "conflicting files... Finished?") |
| 142 | + continue_maybe(msg) |
| 143 | + had_conflicts = True |
| 144 | + |
| 145 | + commit_authors = run_cmd(['git', 'log', 'HEAD..%s' % pr_branch_name, |
| 146 | + '--pretty=format:%an <%ae>']).split("\n") |
| 147 | + distinct_authors = sorted(set(commit_authors), |
| 148 | + key=lambda x: commit_authors.count(x), |
| 149 | + reverse=True) |
| 150 | + primary_author = distinct_authors[0] |
| 151 | + commits = run_cmd(['git', 'log', 'HEAD..%s' % pr_branch_name, |
| 152 | + '--pretty=format:%h [%an] %s']).split("\n\n") |
| 153 | + |
| 154 | + merge_message_flags = [] |
| 155 | + |
| 156 | + merge_message_flags += ["-m", title] |
| 157 | + if body is not None: |
| 158 | + merge_message_flags += ["-m", '\n'.join(textwrap.wrap(body))] |
| 159 | + |
| 160 | + authors = "\n".join(["Author: %s" % a for a in distinct_authors]) |
| 161 | + |
| 162 | + merge_message_flags += ["-m", authors] |
| 163 | + |
| 164 | + if had_conflicts: |
| 165 | + committer_name = run_cmd("git config --get user.name").strip() |
| 166 | + committer_email = run_cmd("git config --get user.email").strip() |
| 167 | + message = ("This patch had conflicts when merged, " |
| 168 | + "resolved by\nCommitter: %s <%s>" |
| 169 | + % (committer_name, committer_email)) |
| 170 | + merge_message_flags += ["-m", message] |
| 171 | + |
| 172 | + # The string "Closes #%s" string is required for GitHub to correctly close |
| 173 | + # the PR |
| 174 | + merge_message_flags += [ |
| 175 | + "-m", |
| 176 | + "Closes #%s from %s and squashes the following commits:" |
| 177 | + % (pr_num, pr_repo_desc)] |
| 178 | + for c in commits: |
| 179 | + merge_message_flags += ["-m", c] |
| 180 | + |
| 181 | + run_cmd(['git', 'commit', '--author="%s"' % primary_author] + |
| 182 | + merge_message_flags) |
| 183 | + |
| 184 | + continue_maybe("Merge complete (local ref %s). Push to %s?" % ( |
| 185 | + target_branch_name, PUSH_REMOTE_NAME)) |
| 186 | + |
| 187 | + try: |
| 188 | + run_cmd('git push %s %s:%s' % (PUSH_REMOTE_NAME, target_branch_name, |
| 189 | + target_ref)) |
| 190 | + except Exception as e: |
| 191 | + clean_up() |
| 192 | + fail("Exception while pushing: %s" % e) |
| 193 | + |
| 194 | + merge_hash = run_cmd("git rev-parse %s" % target_branch_name)[:8] |
| 195 | + clean_up() |
| 196 | + print("Pull request #%s merged!" % pr_num) |
| 197 | + print("Merge hash: %s" % merge_hash) |
| 198 | + return merge_hash |
| 199 | + |
| 200 | + |
| 201 | +def cherry_pick(pr_num, merge_hash, default_branch): |
| 202 | + pick_ref = raw_input("Enter a branch name [%s]: " % default_branch) |
| 203 | + if pick_ref == "": |
| 204 | + pick_ref = default_branch |
| 205 | + |
| 206 | + pick_branch_name = "%s_PICK_PR_%s_%s" % (BRANCH_PREFIX, pr_num, |
| 207 | + pick_ref.upper()) |
| 208 | + |
| 209 | + run_cmd("git fetch %s %s:%s" % (PUSH_REMOTE_NAME, pick_ref, |
| 210 | + pick_branch_name)) |
| 211 | + run_cmd("git checkout %s" % pick_branch_name) |
| 212 | + run_cmd("git cherry-pick -sx %s" % merge_hash) |
| 213 | + |
| 214 | + continue_maybe("Pick complete (local ref %s). Push to %s?" % ( |
| 215 | + pick_branch_name, PUSH_REMOTE_NAME)) |
| 216 | + |
| 217 | + try: |
| 218 | + run_cmd('git push %s %s:%s' % (PUSH_REMOTE_NAME, pick_branch_name, |
| 219 | + pick_ref)) |
| 220 | + except Exception as e: |
| 221 | + clean_up() |
| 222 | + fail("Exception while pushing: %s" % e) |
| 223 | + |
| 224 | + pick_hash = run_cmd("git rev-parse %s" % pick_branch_name)[:8] |
| 225 | + clean_up() |
| 226 | + |
| 227 | + print("Pull request #%s picked into %s!" % (pr_num, pick_ref)) |
| 228 | + print("Pick hash: %s" % pick_hash) |
| 229 | + return pick_ref |
| 230 | + |
| 231 | + |
| 232 | +def fix_version_from_branch(branch, versions): |
| 233 | + # Note: Assumes this is a sorted (newest->oldest) list of un-released |
| 234 | + # versions |
| 235 | + if branch == "master": |
| 236 | + return versions[0] |
| 237 | + else: |
| 238 | + branch_ver = branch.replace("branch-", "") |
| 239 | + return filter(lambda x: x.name.startswith(branch_ver), versions)[-1] |
| 240 | + |
| 241 | + |
| 242 | +# branches = get_json("%s/branches" % GITHUB_API_BASE) |
| 243 | +# branch_names = filter(lambda x: x.startswith("branch-"), |
| 244 | +# [x['name'] for x in branches]) |
| 245 | +# Assumes branch names can be sorted lexicographically |
| 246 | +# latest_branch = sorted(branch_names, reverse=True)[0] |
| 247 | + |
| 248 | +pr_num = raw_input("Which pull request would you like to merge? (e.g. 34): ") |
| 249 | +pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num)) |
| 250 | + |
| 251 | +url = pr["url"] |
| 252 | +title = pr["title"] |
| 253 | +body = pr["body"] |
| 254 | +target_ref = pr["base"]["ref"] |
| 255 | +user_login = pr["user"]["login"] |
| 256 | +base_ref = pr["head"]["ref"] |
| 257 | +pr_repo_desc = "%s/%s" % (user_login, base_ref) |
| 258 | + |
| 259 | +if pr["merged"] is True: |
| 260 | + print("Pull request {0} has already been merged, please backport manually" |
| 261 | + .format(pr_num)) |
| 262 | + sys.exit(0) |
| 263 | + |
| 264 | +if not bool(pr["mergeable"]): |
| 265 | + msg = ("Pull request {0} is not mergeable in its current form.\n" |
| 266 | + "Continue? (experts only!)".format(pr_num)) |
| 267 | + continue_maybe(msg) |
| 268 | + |
| 269 | +print("\n=== Pull Request #%s ===" % pr_num) |
| 270 | +print("title\t%s\nsource\t%s\ntarget\t%s\nurl\t%s" |
| 271 | + % (title, pr_repo_desc, target_ref, url)) |
| 272 | +continue_maybe("Proceed with merging pull request #%s?" % pr_num) |
| 273 | + |
| 274 | +merged_refs = [target_ref] |
| 275 | + |
| 276 | +merge_hash = merge_pr(pr_num, target_ref) |
0 commit comments