forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh_update_pr_milestone.py
executable file
·54 lines (49 loc) · 2.08 KB
/
gh_update_pr_milestone.py
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
#!/usr/bin/env python
from github import Github, GithubException
from sys import exit
from os.path import expanduser
from argparse import ArgumentParser
from datetime import datetime
import re
import urllib2
from time import sleep
from cms_static import GH_CMSSW_ORGANIZATION as gh_user
from cms_static import GH_CMSSW_REPO as gh_cmssw
from socket import setdefaulttimeout
setdefaulttimeout(120)
import json
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-s", "--source", dest="source", help="Source repository, default is master", type=str, default="master")
parser.add_argument("-d", "--dest")
parser.add_argument("-r", "--repository", dest="repository", help="Github Repositoy name e.g. cms-sw/cmssw.", type=str, default=gh_user+"/"+gh_cmssw)
parser.add_argument("-f", "--force", dest="force", default=False, action="store_true")
parser.add_argument("-n", "-dry-run", dest="dryRun", default=False, action="store_true")
args = parser.parse_args()
if args.source == args.dest:
print "Source and destination branches are same"
exit(1)
elif (not args.source) or (not args.dest):
print "Missing source or destination branch"
exit(1)
GH_TOKEN = open(expanduser("~/.github-token")).read().strip()
gh = Github(login_or_token=GH_TOKEN)
repo = gh.get_repo(args.repository)
desMilestone = None
milestones = repo.get_milestones()
for item in repo.get_milestones():
if args.dest in item.title:
desMilestone = item
break
if not desMilestone:
print "ERROR: Unable to find milestone for with title %s" % args.dest
print "Found milestone: %s" % desMilestone.number
pulls = repo.get_pulls(base=args.source, state="open", sort="created", direction="asc")
for pr in pulls:
print "Wroking on PR ",pr.number,"with milestone",pr.milestone.number
if (not args.force) and (pr.milestone.number == desMilestone.number): continue
if not args.dryRun:
issue = repo.get_issue(pr.number)
if args.force: issue.edit(milestone=None)
issue.edit(milestone=desMilestone)
print " Updated milestone:",desMilestone.number