Skip to content

Commit df43bc0

Browse files
authored
Merge pull request #35 from bryanburgers/more-structured-comments
Add `Approved` and `Delegated` structured comments
2 parents 0c36bc4 + 4bedb67 commit df43bc0

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

homu/comments.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,59 @@ def jsonify(self):
1818
return json.dumps(out, separators=(',', ':'))
1919

2020

21+
class Approved(Comment):
22+
def __init__(self, bot=None, **args):
23+
# Because homu needs to leave a comment for itself to kick off a build,
24+
# we need to know the correct botname to use. However, we don't want to
25+
# save that botname in our state JSON. So we need a custom constructor
26+
# to grab the botname and delegate the rest of the keyword args to the
27+
# Comment constructor.
28+
super().__init__(**args)
29+
self.bot = bot
30+
31+
params = ["sha", "approver"]
32+
33+
def render(self):
34+
# The comment here is required because Homu wants a full, unambiguous,
35+
# pinned commit hash to kick off the build, and this note-to-self is
36+
# how it gets it. This is to safeguard against situations where Homu
37+
# reloads and another commit has been pushed since the approval.
38+
message = ":pushpin: Commit {sha} has been " + \
39+
"approved by `{approver}`\n\n" + \
40+
"<!-- @{bot} r={approver} {sha} -->"
41+
return message.format(
42+
sha=self.sha,
43+
approver=self.approver,
44+
bot=self.bot
45+
)
46+
47+
48+
class ApprovalIgnoredWip(Comment):
49+
def __init__(self, wip_keyword=None, **args):
50+
# We want to use the wip keyword in the message, but not in the json
51+
# blob.
52+
super().__init__(**args)
53+
self.wip_keyword = wip_keyword
54+
55+
params = ["sha"]
56+
57+
def render(self):
58+
message = ':clipboard:' + \
59+
' Looks like this PR is still in progress,' + \
60+
' ignoring approval.\n\n' + \
61+
'Hint: Remove **{wip_keyword}** from this PR\'s title when' + \
62+
' it is ready for review.'
63+
return message.format(wip_keyword=self.wip_keyword)
64+
65+
66+
class Delegated(Comment):
67+
params = ["delegator", "delegate"]
68+
69+
def render(self):
70+
message = ':v: @{} can now approve this pull request'
71+
return message.format(self.delegate)
72+
73+
2174
class BuildStarted(Comment):
2275
params = ["head_sha", "merge_sha"]
2376

homu/main.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,10 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
497497
for wip_kw in ['WIP', 'TODO', '[WIP]', '[TODO]', '[DO NOT MERGE]']:
498498
if state.title.upper().startswith(wip_kw):
499499
if realtime:
500-
state.add_comment((
501-
':clipboard:'
502-
' Looks like this PR is still in progress,'
503-
' ignoring approval.\n\n'
504-
'Hint: Remove **{}** from this PR\'s title when'
505-
' it is ready for review.'
506-
).format(wip_kw))
500+
state.add_comment(comments.ApprovalIgnoredWip(
501+
sha=state.head_sha,
502+
wip_keyword=wip_kw,
503+
))
507504
is_wip = True
508505
break
509506
if is_wip:
@@ -565,14 +562,10 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
565562
.format(msg, state.head_sha)
566563
)
567564
else:
568-
state.add_comment(
569-
':pushpin: Commit {} has been approved by `{}`\n\n<!-- @{} r={} {} -->' # noqa
570-
.format(
571-
state.head_sha,
572-
approver,
573-
my_username,
574-
approver,
575-
state.head_sha,
565+
state.add_comment(comments.Approved(
566+
sha=state.head_sha,
567+
approver=approver,
568+
bot=my_username,
576569
))
577570
treeclosed = state.blocked_by_closed_tree()
578571
if treeclosed:
@@ -620,10 +613,10 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
620613
state.save()
621614

622615
if realtime:
623-
state.add_comment(
624-
':v: @{} can now approve this pull request'
625-
.format(state.delegate)
626-
)
616+
state.add_comment(comments.Delegated(
617+
delegator=username,
618+
delegate=state.delegate
619+
))
627620

628621
elif word == 'delegate-':
629622
# TODO: why is this a TRY?
@@ -640,10 +633,10 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
640633
state.save()
641634

642635
if realtime:
643-
state.add_comment(
644-
':v: @{} can now approve this pull request'
645-
.format(state.delegate)
646-
)
636+
state.add_comment(comments.Delegated(
637+
delegator=username,
638+
delegate=state.delegate
639+
))
647640

648641
elif word == 'retry' and realtime:
649642
if not _try_auth_verified():

0 commit comments

Comments
 (0)