-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathsimple-cms-bot.py
executable file
·98 lines (88 loc) · 3.17 KB
/
simple-cms-bot.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
"""
Returns top commit of a PR (mostly used to comments)
"""
from os.path import expanduser, dirname, abspath, join, exists
from optparse import OptionParser
from socket import setdefaulttimeout
from github_utils import (
api_rate_limits,
get_gh_token,
set_gh_user,
)
setdefaulttimeout(120)
import sys, json, re
from github import Github
SCRIPT_DIR = dirname(abspath(sys.argv[0]))
def process_issue_comment(payload, repo_config, gh, dryRun):
if not "pull_request" in payload["issue"]:
print("WARNING: comment is made on a GH issue")
return
if not payload["action"] in ["created"]:
print("WARNING: Unknown action %s" % payload["action"])
return
cobj = payload["comment"]
comment = [l.strip() for l in cobj["body"].strip().split("\n")]
if not re.match("^((@*cmsbot(,\s*|\s+))|)please\s+(run\s+|)test$", comment[0], re.I):
print("Unknown comment first line:[%s]" % comment[0])
return
commentor = cobj["user"]["login"]
print("Test requested by %s at %s" % (commentor, cobj["created_at"]))
repo_owner = payload["repository"]["owner"]["login"]
repo_name = payload["repository"]["full_name"]
repo = gh.get_repo(repo_name)
prId = payload["issue"]["number"]
issue = repo.get_issue(prId)
gh_comment = issue.get_comment(cobj["id"])
from process_pr import set_emoji, create_property_file
if not commentor in repo_config.TRIGGER_PR_TESTS + [repo_owner]:
print("Not an authorized user to trigger test")
if not dryRun:
set_emoji(repo, gh_comment, "-1", True)
return
print("Starting Tests")
if not dryRun:
set_emoji(repo, gh_comment, "+1", True)
parameters = {"PULL_REQUEST": prId, "REPOSITORY": repo_name, "REQUESTOR": commentor}
try:
parameters["BOT_JOB_NAME"] = repo_config.BOT_JOB_NAME
except:
parameters["BOT_JOB_NAME"] = "pr-test-%s" % repo_name.replace("/", "-")
create_property_file("simple-cms-bot-pr-test-%s.properties" % prId, parameters, dryRun)
return
if __name__ == "__main__":
parser = OptionParser(usage="%prog")
parser.add_option(
"-n",
"--dry-run",
dest="dryRun",
action="store_true",
help="Do not modify Github",
default=False,
)
parser.add_option(
"-e",
"--event",
dest="event",
help="Type of github webhook event e.g. issue_comment",
type=str,
default="",
)
opts, args = parser.parse_args()
if len(args) != 0:
parser.error("Too many")
if not opts.event in ["issue_comment"]:
print("ERROR: Event %s not recognized" % opts.event)
sys.exit(0)
payload = json.load(sys.stdin)
repo_name = payload["repository"]["full_name"]
repo_dir = join(SCRIPT_DIR, "repos", repo_name.replace("-", "_"))
if exists(repo_dir):
sys.path.insert(0, repo_dir)
print("Reading ", repo_dir)
import repo_config
set_gh_user(repo_config.CMSBUILD_USER)
gh = Github(login_or_token=get_gh_token(repo_name), per_page=100)
api_rate_limits(gh)
process_issue_comment(payload, repo_config, gh, opts.dryRun)
api_rate_limits(gh)