forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmsdist-comp-pr-process.py
executable file
·75 lines (70 loc) · 2.88 KB
/
cmsdist-comp-pr-process.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
from sys import exit,argv
from re import match
from github import Github
from os.path import expanduser, dirname, abspath, join, exists
from optparse import OptionParser
from socket import setdefaulttimeout
setdefaulttimeout(120)
SCRIPT_DIR = dirname(abspath(argv[0]))
def process_pr(gh, repo, issue, dryRun):
from cmsdist_merge_permissions import USERS_TO_TRIGGER_HOOKS, getCommentCommand, hasRights
print "Issue state:", issue.state
prId = issue.number
pr = None
branch = None
cmdType = None
chg_files= []
if issue.pull_request:
pr = repo.get_pull(prId)
branch = pr.base.ref
print "PR merged:", pr.merged
if pr.merged: return True
from process_pr import get_changed_files
chg_files = get_changed_files(repo, pr)
USERS_TO_TRIGGER_HOOKS.add("cmsbuild")
for comment in issue.get_comments():
commenter = comment.user.login
if not commenter in USERS_TO_TRIGGER_HOOKS: continue
comment_msg = comment.body.encode("ascii", "ignore")
comment_lines = [ l.strip() for l in comment_msg.split("\n") if l.strip() ][0:1]
print "Comment first line: %s => %s" % (commenter, comment_lines)
if not comment_lines: continue
first_line = comment_lines[0]
if commenter == "cmsbuild":
if not cmdType: continue
if match("^Command\s+"+cmdType+"\s+acknowledged.$",first_line):
print "Acknowledged ",cmdType
cmdType = None
continue
cmd = getCommentCommand(first_line)
if not cmd: continue
if (cmd == "ping") and cmdType: continue
if cmd == "merge" and not pr: continue
if not hasRights (commenter, branch, cmd, chg_files): continue
cmdType = cmd
print "Found: Command %s issued by %s" % (cmdType, commenter)
if not cmdType: return True
print "Processing ",cmdType
if dryRun: return True
if issue.state == "open":
if cmdType == "merge": pr.merge()
if cmdType == "close": issue.edit(state="closed")
elif cmdType == "open": issue.edit(state="open")
issue.create_comment("Command "+cmdType+" acknowledged.")
return True
if __name__ == "__main__":
parser = OptionParser(usage="%prog <pull-request-id>")
parser.add_option("-n", "--dry-run", dest="dryRun", action="store_true", help="Do not modify Github", default=False)
#parser.add_option("-r", "--repository", dest="repository", help="Github Repositoy name e.g. cms-sw/cmsdist.", type=str, default="cms-sw/cmsdist")
opts, args = parser.parse_args()
if len(args) != 1:
parser.error("Too many/few arguments")
prId = int(args[0])
repo_dir = join(SCRIPT_DIR,'repos',"cms-sw/cmsdist".replace("-","_"))
if exists(join(repo_dir,"repo_config.py")): sys.path.insert(0,repo_dir)
import repo_config
gh = Github(login_or_token=open(expanduser(repo_config.GH_TOKEN)).read().strip())
repo = gh.get_repo("cms-sw/cmsdist")
if not process_pr(gh, repo, repo.get_issue(prId), opts.dryRun): exit(1)
exit (0)