Skip to content

Commit 5b3ea91

Browse files
authored
Merge pull request #13 from rust-lang/json-comments
Output structured comments for build events Co-authored-by: <kennytm@gmail.com>
2 parents 1f47436 + b04c34d commit 5b3ea91

File tree

3 files changed

+144
-33
lines changed

3 files changed

+144
-33
lines changed

homu/comments.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
3+
4+
class Comment:
5+
def __init__(self, **args):
6+
if len(args) != len(self.params):
7+
raise KeyError("different number of params")
8+
for key, value in args.items():
9+
if key in self.params:
10+
setattr(self, key, value)
11+
else:
12+
raise KeyError("unknown attribute: %s" % key)
13+
14+
def jsonify(self):
15+
out = {"type": self.__class__.__name__}
16+
for param in self.params:
17+
out[param] = getattr(self, param)
18+
return json.dumps(out, separators=(',', ':'))
19+
20+
21+
class BuildStarted(Comment):
22+
params = ["head_sha", "merge_sha"]
23+
24+
def render(self):
25+
return ":hourglass: Testing commit %s with merge %s..." % (
26+
self.head_sha, self.merge_sha,
27+
)
28+
29+
30+
class TryBuildStarted(Comment):
31+
params = ["head_sha", "merge_sha"]
32+
33+
def render(self):
34+
return ":hourglass: Trying commit %s with merge %s..." % (
35+
self.head_sha, self.merge_sha,
36+
)
37+
38+
39+
class BuildCompleted(Comment):
40+
params = ["approved_by", "base_ref", "builders", "merge_sha"]
41+
42+
def render(self):
43+
urls = ", ".join(
44+
"[%s](%s)" % kv for kv in sorted(self.builders.items())
45+
)
46+
return (
47+
":sunny: Test successful - %s\n"
48+
"Approved by: %s\n"
49+
"Pushing %s to %s..."
50+
% (
51+
urls, self.approved_by, self.merge_sha, self.base_ref,
52+
)
53+
)
54+
55+
56+
class TryBuildCompleted(Comment):
57+
params = ["builders", "merge_sha"]
58+
59+
def render(self):
60+
urls = ", ".join(
61+
"[%s](%s)" % kv for kv in sorted(self.builders.items())
62+
)
63+
return ":sunny: Try build successful - %s\nBuild commit: %s" % (
64+
urls, self.merge_sha,
65+
)
66+
67+
68+
class BuildFailed(Comment):
69+
params = ["builder_url", "builder_name"]
70+
71+
def render(self):
72+
return ":broken_heart: Test failed - [%s](%s)" % (
73+
self.builder_name, self.builder_url
74+
)
75+
76+
77+
class TryBuildFailed(Comment):
78+
params = ["builder_url", "builder_name"]
79+
80+
def render(self):
81+
return ":broken_heart: Test failed - [%s](%s)" % (
82+
self.builder_name, self.builder_url
83+
)
84+
85+
86+
class TimedOut(Comment):
87+
params = []
88+
89+
def render(self):
90+
return ":boom: Test timed out"

homu/main.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import re
66
import functools
7+
from . import comments
78
from . import utils
89
from .utils import lazy_debug
910
import logging
@@ -177,8 +178,12 @@ def get_issue(self):
177178
issue = self.issue = self.get_repo().issue(self.num)
178179
return issue
179180

180-
def add_comment(self, text):
181-
self.get_issue().create_comment(text)
181+
def add_comment(self, comment):
182+
if isinstance(comment, comments.Comment):
183+
comment = "%s\n<!-- homu: %s -->" % (
184+
comment.render(), comment.jsonify(),
185+
)
186+
self.get_issue().create_comment(comment)
182187

183188
def change_labels(self, event):
184189
event = self.label_events.get(event.value, {})
@@ -362,15 +367,14 @@ def timed_out(self):
362367
self.save()
363368
self.set_status('failure')
364369

365-
desc = 'Test timed out'
366370
utils.github_create_status(
367371
self.get_repo(),
368372
self.head_sha,
369373
'failure',
370374
'',
371-
desc,
375+
'Test timed out',
372376
context='homu')
373-
self.add_comment(':boom: {}'.format(desc))
377+
self.add_comment(comments.TimedOut())
374378
self.change_labels(LabelEvent.TIMED_OUT)
375379

376380

@@ -1242,7 +1246,7 @@ def start_build(state, repo_cfgs, buildbot_slots, logger, db, git_cfg):
12421246
builders += ['checks-' + key for key, value in repo_cfg['checks'].items() if 'name' in value] # noqa
12431247
only_status_builders = False
12441248

1245-
if len(builders) is 0:
1249+
if len(builders) == 0:
12461250
raise RuntimeError('Invalid configuration')
12471251

12481252
lazy_debug(logger, lambda: "start_build: builders={!r}".format(builders))
@@ -1291,7 +1295,16 @@ def start_build(state, repo_cfgs, buildbot_slots, logger, db, git_cfg):
12911295
desc,
12921296
context='homu')
12931297

1294-
state.add_comment(':hourglass: ' + desc)
1298+
if state.try_:
1299+
state.add_comment(comments.TryBuildStarted(
1300+
head_sha=state.head_sha,
1301+
merge_sha=state.merge_sha,
1302+
))
1303+
else:
1304+
state.add_comment(comments.BuildStarted(
1305+
head_sha=state.head_sha,
1306+
merge_sha=state.merge_sha,
1307+
))
12951308

12961309
return True
12971310

@@ -1687,7 +1700,7 @@ def main():
16871700
builders += ['status-' + key for key, value in repo_cfg['status'].items() if 'context' in value] # noqa
16881701
if 'checks' in repo_cfg:
16891702
builders += ['checks-' + key for key, value in repo_cfg['checks'].items() if 'name' in value] # noqa
1690-
if len(builders) is 0:
1703+
if len(builders) == 0:
16911704
raise RuntimeError('Invalid configuration')
16921705

16931706
state.init_build_res(builders, use_db=False)

homu/server.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
synchronize,
1010
LabelEvent,
1111
)
12+
from . import comments
1213
from . import utils
1314
from .utils import lazy_debug
1415
import github3
@@ -528,7 +529,7 @@ def fail(err):
528529
for name, value in repo_cfg['status'].items():
529530
if 'context' in value and value['context'] == info['context']:
530531
status_name = name
531-
if status_name is "":
532+
if status_name == "":
532533
return 'OK'
533534

534535
if info['state'] == 'pending':
@@ -582,19 +583,18 @@ def report_build_res(succ, url, builder, state, logger, repo_cfg):
582583
if succ:
583584
if all(x['res'] for x in state.build_res.values()):
584585
state.set_status('success')
585-
desc = 'Test successful'
586-
utils.github_create_status(state.get_repo(), state.head_sha,
587-
'success', url, desc, context='homu')
588-
589-
urls = ', '.join('[{}]({})'.format(builder, x['url']) for builder, x in sorted(state.build_res.items())) # noqa
590-
test_comment = ':sunny: {} - {}'.format(desc, urls)
586+
utils.github_create_status(
587+
state.get_repo(), state.head_sha,
588+
'success', url, "Test successful", context='homu'
589+
)
591590

592591
if state.approved_by and not state.try_:
593-
comment = (test_comment + '\n' +
594-
'Approved by: {}\nPushing {} to {}...'
595-
).format(state.approved_by, state.merge_sha,
596-
state.base_ref)
597-
state.add_comment(comment)
592+
state.add_comment(comments.BuildCompleted(
593+
approved_by=state.approved_by,
594+
base_ref=state.base_ref,
595+
builders={k: v["url"] for k, v in state.build_res.items()},
596+
merge_sha=state.merge_sha,
597+
))
598598
state.change_labels(LabelEvent.SUCCEED)
599599
try:
600600
try:
@@ -622,24 +622,32 @@ def report_build_res(succ, url, builder, state, logger, repo_cfg):
622622

623623
state.add_comment(':eyes: ' + desc)
624624
else:
625-
comment = (test_comment + '\n' +
626-
'State: approved={} try={}'
627-
).format(state.approved_by, state.try_)
628-
state.add_comment(comment)
625+
state.add_comment(comments.TryBuildCompleted(
626+
builders={k: v["url"] for k, v in state.build_res.items()},
627+
merge_sha=state.merge_sha,
628+
))
629629
state.change_labels(LabelEvent.TRY_SUCCEED)
630630

631631
else:
632632
if state.status == 'pending':
633633
state.set_status('failure')
634-
desc = 'Test failed'
635-
utils.github_create_status(state.get_repo(), state.head_sha,
636-
'failure', url, desc, context='homu')
637-
638-
state.add_comment(':broken_heart: {} - [{}]({})'.format(desc,
639-
builder,
640-
url))
641-
event = LabelEvent.TRY_FAILED if state.try_ else LabelEvent.FAILED
642-
state.change_labels(event)
634+
utils.github_create_status(
635+
state.get_repo(), state.head_sha,
636+
'failure', url, "Test failed", context='homu'
637+
)
638+
639+
if state.try_:
640+
state.add_comment(comments.TryBuildFailed(
641+
builder_url=url,
642+
builder_name=builder,
643+
))
644+
state.change_labels(LabelEvent.TRY_FAILED)
645+
else:
646+
state.add_comment(comments.BuildFailed(
647+
builder_url=url,
648+
builder_name=builder,
649+
))
650+
state.change_labels(LabelEvent.FAILED)
643651

644652
g.queue_handler()
645653

0 commit comments

Comments
 (0)