Skip to content

Commit

Permalink
Optionally use the GH API to open PRs from the cherrypick_pr script (e…
Browse files Browse the repository at this point in the history
…lastic#3627)

If you add the `--create_pr` parameter, and have a GH token in
`~/.github_token`, the script will also create a PR for you, with the right
title and tags already set.
  • Loading branch information
tsg authored and ruflin committed Feb 21, 2017
1 parent f96dc66 commit 12f3b78
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions dev-tools/cherrypick_pr
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/usr/bin/env python
import sys
import json
import argparse
from os.path import expanduser
from subprocess import check_call, call, check_output
import requests

"""
Example usage:
./dev-tools/cherrypick_pr 5.0 2565 6490604aa0cf7fa61932a90700e6ca988fc8a527
./dev-tools/cherrypick_pr --create_pr 5.0 2565 6490604aa0cf7fa61932a90700e6ca988fc8a527
In case of backporting errors, fix them, then run:
git cherry-pick --continue
./dev-tools/cherrypick_pr 5.0 2565 6490604aa0cf7fa61932a90700e6ca988fc8a527 --continue
./dev-tools/cherrypick_pr --create_pr 5.0 2565 6490604aa0cf7fa61932a90700e6ca988fc8a527 --continue
This script does the following:
Expand All @@ -20,8 +23,9 @@ This script does the following:
* calls the git cherry-pick command in this branch
* after fixing the merge errors (if needed), pushes the branch to your
remote
You then just need to go to Github and open the PR.
* if the --create_pr flag is used, it uses the GitHub API to create the PR
for you. Note that this requires you to have a Github token in the
`~/.github_token` file.
Note that you need to take the commit hashes from `git log` on the
from_branch, copying the IDs from Github doesn't work in case we squashed the
Expand All @@ -45,6 +49,9 @@ def main():
help="Continue after fixing merging errors.")
parser.add_argument("--from_branch", default="master",
help="From branch")
parser.add_argument("--create_pr", action="store_true",
help="Create a PR using the Github API " +
"(requires token in ~/.github_token)")
args = parser.parse_args()

print args
Expand Down Expand Up @@ -91,9 +98,39 @@ def main():
shell=True)
check_call("git push --set-upstream {} {}"
.format(remote, tmp_branch), shell=True)
print("Done. Open PR by following this URL: \n\t" +
"https://github.com/elastic/beats/compare/{}...{}:{}?expand=1"
.format(args.to_branch, remote, tmp_branch))
if not args.create_pr:
print("Done. Open PR by following this URL: \n\t" +
"https://github.com/elastic/beats/compare/{}...{}:{}?expand=1"
.format(args.to_branch, remote, tmp_branch))
else:
token = open(expanduser("~/.github_token"), "r").read().strip()
base = "https://api.github.com/repos/elastic/beats"
s = requests.Session()
s.headers.update({"Authorization": "token " + token})

original_pr = s.get(base+"/pulls/"+args.pr_number).json()

# create PR
r = s.post(base+"/pulls", json=dict(
title="Cherry-pick to {}: {}"
.format(args.to_branch, original_pr["title"]),
head=remote + ":" + tmp_branch,
base=args.to_branch,
body="Cherry-pick of PR #{} to {} branch. Original message: \n\n{}"
.format(args.pr_number, args.to_branch, original_pr["body"])
))
if r.status_code > 299:
print("Creating PR failed: {}".format(r.json()))
sys.exit(1)
new_pr = r.json()

# add labels
s.post(base+"/issues/{}/labels".format(new_pr["number"]),
json=["backport", "review"])

print("\nDone. PR created: {}".format(new_pr["html_url"]))
print("Please go and check it and add the review tags")


if __name__ == "__main__":
sys.exit(main())

0 comments on commit 12f3b78

Please sign in to comment.