Skip to content

Commit 33ba4cb

Browse files
authored
feat: add more discussion state emojis (#383)
2 parents 5f965c0 + d4368fd commit 33ba4cb

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

app/bot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"commit",
3030
"discussion",
3131
"discussion_answered",
32+
"discussion_duplicate",
33+
"discussion_outdated",
3234
"issue_closed_completed",
3335
"issue_closed_unplanned",
3436
"issue_open",

app/components/github_integration/comments/discussions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
}
2626
created_at: createdAt
2727
html_url: url
28+
state_reason: stateReason
29+
closed
2830
answer {
2931
user: author {
3032
login

app/components/github_integration/entities/discussions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
}
1616
created_at: createdAt
1717
html_url: url
18+
state_reason: stateReason
19+
closed
1820
answer {
1921
user: author {
2022
login

app/components/github_integration/entities/fmt.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ def get_entity_emoji(bot: GhosttyBot, entity: Entity) -> dc.Emoji:
3535
) # fmt: skip
3636
elif isinstance(entity, Discussion):
3737
emoji_name = "discussion"
38-
if entity.answered_by:
39-
emoji_name += "_answered"
38+
if entity.closed or entity.answered_by:
39+
emoji_name += (
40+
"_duplicate" if entity.state_reason == "DUPLICATE"
41+
else "_outdated" if entity.state_reason == "OUTDATED"
42+
else "_answered"
43+
) # fmt: skip
4044
else:
4145
msg = f"Unknown entity type: {type(entity)}"
4246
raise TypeError(msg)

app/components/github_integration/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class PullRequest(Entity):
117117

118118
class Discussion(Entity):
119119
answered_by: GitHubUser | None
120+
closed: bool
121+
state_reason: Literal["DUPLICATE", "RESOLVED", "OUTDATED", "REOPENED"] | None
120122

121123

122124
class EntityGist(NamedTuple):

app/components/github_integration/webhooks/discussions.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@ class DiscussionLike(Protocol):
2323
category: DiscussionPropCategory
2424
answer_html_url: str | None
2525
state: Literal["open", "closed", "locked", "converting", "transferring"]
26+
state_reason: Literal["resolved", "outdated", "duplicate", "reopened"] | None
27+
28+
29+
def get_discussion_emoji(discussion: DiscussionLike) -> EmojiName:
30+
emoji = "discussion"
31+
if discussion.state_reason in ("outdated", "duplicate"):
32+
emoji += "_" + discussion.state_reason
33+
elif discussion.answer_html_url or discussion.state == "closed":
34+
emoji += "_answered"
35+
return emoji
2636

2737

2838
def discussion_footer(
2939
discussion: DiscussionLike, *, emoji: EmojiName | None = None
3040
) -> Footer:
31-
emoji = emoji or (
32-
"discussion_answered"
33-
if discussion.answer_html_url or discussion.state == "closed"
34-
else "discussion"
41+
return Footer(
42+
emoji or get_discussion_emoji(discussion),
43+
f"Discussion #{discussion.number}: {discussion.title}",
3544
)
36-
return Footer(emoji, f"Discussion #{discussion.number}: {discussion.title}")
3745

3846

3947
def discussion_embed_content(
@@ -74,12 +82,11 @@ async def _(event: events.DiscussionClosed) -> None:
7482
@webhook.event.discussion.reopened
7583
async def _(event: events.DiscussionReopened) -> None:
7684
discussion = event.discussion
77-
emoji = "discussion_answered" if discussion.answer_html_url else "discussion"
7885
await send_embed(
7986
bot,
8087
event.sender,
8188
discussion_embed_content(discussion, "reopened"),
82-
discussion_footer(discussion, emoji=emoji),
89+
discussion_footer(discussion),
8390
color="gray",
8491
feed_type="discussions",
8592
)
@@ -104,25 +111,23 @@ async def _(event: events.DiscussionAnswered) -> None:
104111
@webhook.event.discussion.unanswered
105112
async def _(event: events.DiscussionUnanswered) -> None:
106113
discussion = event.discussion
107-
emoji = "discussion_answered" if discussion.state == "closed" else "discussion"
108114
await send_embed(
109115
bot,
110116
event.sender or cast("SimpleUser", GitHubUser.default()),
111117
discussion_embed_content(discussion, "unmarked an answer for"),
112-
discussion_footer(discussion, emoji=emoji),
118+
discussion_footer(discussion),
113119
color="red",
114120
feed_type="discussions",
115121
)
116122

117123
@webhook.event.discussion.locked
118124
async def _(event: events.DiscussionLocked) -> None:
119125
discussion = event.discussion
120-
emoji = "discussion_answered" if discussion.answer_html_url else "discussion"
121126
await send_embed(
122127
bot,
123128
event.sender,
124129
discussion_embed_content(discussion, "locked"),
125-
discussion_footer(discussion, emoji=emoji),
130+
discussion_footer(discussion),
126131
color="orange",
127132
feed_type="discussions",
128133
)

tests/test_close_help_post.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def fill_entity(**kwargs: Any) -> SimpleNamespace:
2222
"user": GitHubUser(login="", url="", icon_url=""),
2323
"created_at": dt.datetime(1, 1, 1, tzinfo=dt.UTC),
2424
"state": False,
25+
"closed": False,
2526
"state_reason": None,
2627
"draft": False,
2728
"merged": False,

0 commit comments

Comments
 (0)