-
Notifications
You must be signed in to change notification settings - Fork 245
/
comment-gh-pr.py
executable file
·93 lines (85 loc) · 2.58 KB
/
comment-gh-pr.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
#!/bin/bash
""":"
python_cmd="python3"
python -V >/dev/null 2>&1 && python_cmd="python"
exec ${python_cmd} $0 ${1+"$@"}
"""
from __future__ import print_function
from github import Github
from os.path import expanduser, dirname, abspath, join, exists
from optparse import OptionParser
from sys import exit
import re, sys
from socket import setdefaulttimeout
setdefaulttimeout(120)
SCRIPT_DIR = dirname(abspath(sys.argv[0]))
if __name__ == "__main__":
parser = OptionParser(
usage="%prog -p|--pullrequest <number> -m|--message <message> [-r|--repository <repo>] [-n|--dry-run]"
)
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/cmssw.",
type=str,
default="cms-sw/cmssw",
)
parser.add_option(
"-p",
"--pullrequest",
dest="pr",
help="Github Pull Request Number e.g. 10500",
type="int",
metavar="N",
)
parser.add_option(
"-m",
"--message",
dest="msg",
help="Message to be added for Github Pull Request",
type="str",
)
parser.add_option(
"-R",
"--report-file",
dest="report_file",
help="Message from the file to be added for Github Pull Request",
type="str",
)
opts, args = parser.parse_args()
if not opts.pr:
parser.error("Missing pull request number : -p|--pullrequest <number>")
msg = ""
if opts.msg:
msg = re.sub("@N@", "\n", opts.msg)
elif opts.report_file:
msg = open(opts.report_file).read()
else:
parser.error(
"Missing pull request message: -m|--message <message> OR -R|--report-file <file-path>"
)
if opts.dryRun:
print("Addeding Comments:", msg)
else:
repo_dir = join(SCRIPT_DIR, "repos", opts.repository.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())
from github_utils import comment_gh_pr
try:
comment_gh_pr(gh, opts.repository, opts.pr, msg)
print("Added comment for %s#%s" % (opts.repository, opts.pr))
print("Comment message:\n", msg)
except Exception as e:
print("Failed to add comment: ", e)
exit(1)