-
Notifications
You must be signed in to change notification settings - Fork 245
/
cms-docker-gh-issues.py
118 lines (106 loc) · 3.8 KB
/
cms-docker-gh-issues.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
from __future__ import print_function
from github import Github
from os.path import expanduser, abspath, dirname, join, exists
import sys, re
from argparse import ArgumentParser
from github_utils import add_issue_labels, create_issue_comment, get_issue_labels
SCRIPT_DIR = dirname(abspath(sys.argv[0]))
parser = ArgumentParser()
parser.add_argument(
"-r",
"--repository",
dest="repo",
help="Github Repositoy name e.g cms-sw/cms-bot",
type=str,
)
parser.add_argument("-t", "--title", dest="title", help="Issue title", type=str)
parser.add_argument(
"-m",
"--message",
dest="msg",
help="Message to be posted s body of the GH issue",
type=str,
default="",
)
parser.add_argument(
"-l",
"--labels",
dest="labels",
nargs="*",
help="Labels for the GH issue (undefined number)",
default="",
)
parser.add_argument(
"-c",
"--comment-only",
dest="comment",
help="Only comment on an existing issue.",
default=False,
)
args = parser.parse_args()
mgs = ""
if not args.repo:
parser.error("Missing Repo")
if args.msg:
msg = re.sub("@N@", "\n", args.msg)
else:
parser.error("Missing issue message: -m|--message <message>")
print("Authenticating to Github and connecting to repo")
repo_dir = join(SCRIPT_DIR, "repos", args.repo.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())
gh_repo = gh.get_repo(args.repo)
print("Authentication succeeded to " + str(gh_repo.full_name))
if not args.comment:
issue_number = None
for issue in gh_repo.get_issues(
labels=[str(label) for label in args.labels], state="all", creator="cmsbuild"
):
if issue.state == "open":
print("Issue already opened... Nothing to do!")
# Delete property files
sys.exit(1)
# We can have multiple issues closed, we take the one that was opened first
print("Issue already closed... Ready for building!")
issue_number = issue.number
if issue_number is None:
print("Creating issue request...")
issue_obj = gh_repo.create_issue(title=args.title, body=msg, labels=args.labels)
issue_number = issue_obj.number
print("New issue number: ", issue_number)
print("Checking existing PR with matching labels...")
urls = ""
for pull in gh_repo.get_issues(labels=[args.labels[0]]):
if pull.pull_request:
urls += "* " + str(pull.html_url) + "\n"
print("The following PRs have matching labels: \n", urls)
# Comment related PRs
if urls != "":
issue_comment = (
"The following PRs should be probably merged before building the new image: \n"
+ urls
)
print(issue_comment)
create_issue_comment(gh_repo.full_name, issue_number, issue_comment)
else:
print("Ready for building!")
# Process "building" or "queued" labels
existing_labels = get_issue_labels(gh_repo.full_name, issue_number)
print("Existing labels:", existing_labels)
for label_obj in existing_labels:
if "building" in label_obj["name"] or "queued" in label_obj["name"]:
print("Build already triggered... Nothing to do!")
with open("gh-info.tmp", "a") as f:
f.write(str(label_obj["name"]) + "\n")
# Don't delete property files
sys.exit(0)
# Delete property files
sys.exit(1)
else:
for issue in gh_repo.get_issues(labels=[str(label) for label in args.labels], state="all"):
issue_number = issue.number
print("Adding issue comment...")
create_issue_comment(gh_repo.full_name, issue_number, msg)