forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forward-pull-requests
executable file
·68 lines (57 loc) · 2.26 KB
/
forward-pull-requests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Given a branch, gets all of its pull requests
# and recreates them in a different branch.
from github import Github, GithubException
from sys import exit
from os.path import expanduser
from argparse import ArgumentParser
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-s", "--source")
parser.add_argument("-d", "--dest")
parser.add_argument("-n", "-dry-run", dest="dryRun", default=False, action="store_true")
parser.add_argument("issues", nargs="*", type=int)
args = parser.parse_args()
print args
gh = Github(login_or_token=open(expanduser("~/.github-token")).read().strip())
try:
gh_repo = gh.get_organization("cms-sw").get_repo("cmssw")
except:
print "Could not find repository."
exit(1)
milestones = gh_repo.get_milestones(state="open")
milestone = [x for x in milestones if args.source in x.title]
destMilestone = [x for x in milestones if args.dest in x.title]
if not milestone:
print "Could not find source milestone."
exit(1)
if not destMilestone:
print "Could not find destination milestone."
exit(1)
issues = args.issues or gh_repo.get_issues(milestone=milestone.pop(), state="open")
for issue in issues:
# If we just have the numeric Id, let's get the associated issue.
if type(issue) == int:
issue = gh_repo.get_issue(issue)
pr = gh_repo.get_pull(issue.number)
print issue.number, pr.head.user.login, pr.head.ref, destMilestone
newBody = issue.body + "\nAutomatically ported from " + args.source + " #%s (original by @%s)." % (issue.number, str(pr.head.user.login))
try:
newHead = "%s:%s" % (pr.head.user.login, pr.head.ref)
print
print '-----'
print "Forward porting %s" % issue.number
print issue.title
print newBody.encode('utf-8')
print args.dest
print "%s:%s" % (pr.head.user.login, pr.head.ref)
print '---'
if args.dryRun:
print 'ATTENTION: Not creating new PR on Github (dry-run)'
continue
newIssue = gh_repo.create_pull(title = issue.title, body =newBody, base = args.dest, head = newHead )
print "New issue number", newIssue.number
except GithubException, e:
print "Error while processing: ", issue.number
print e
continue