|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import re
|
4 |
| -from functools import partial |
5 | 4 | from typing import TYPE_CHECKING, Any, Protocol, cast
|
6 | 5 |
|
7 | 6 | from loguru import logger
|
|
15 | 14 |
|
16 | 15 | if TYPE_CHECKING:
|
17 | 16 | from githubkit.typing import Missing
|
18 |
| - from githubkit.versions.latest.models import RepositoryWebhooks |
19 | 17 | from monalisten import Monalisten, events
|
20 | 18 |
|
21 | 19 | from app.bot import EmojiName, GhosttyBot
|
22 | 20 |
|
23 | 21 |
|
24 |
| -GITHUB_DISCUSSION_URL = re.compile( |
25 |
| - # Ignore if already inside a link block |
26 |
| - r"(?<!\()" |
27 |
| - r"https://github\.com/" |
28 |
| - r"(?P<owner>\b[a-zA-Z0-9\-]+/)" |
29 |
| - r"(?P<repo>\b[a-zA-Z0-9\-\._]+)" |
30 |
| - r"(?P<sep>/(?:issues|pull|discussions)/)" |
31 |
| - r"(?P<number>\d+)" |
32 |
| - r"(?!\))" |
33 |
| -) # fmt: skip |
34 |
| -SUP_HTML = re.compile(r"\s*<sup>((?:.|\s)+?)</sup>\s*") |
35 | 22 | DISUCSSION_DIV_TAG = re.compile(
|
36 | 23 | r"\s*<div type='discussions-op-text'>((?:.|\s)*?)\s*</div>\s*", re.MULTILINE
|
37 | 24 | )
|
38 | 25 |
|
39 | 26 |
|
40 |
| -def shorten_same_repo_links( |
41 |
| - origin_repo: RepositoryWebhooks, matchobj: re.Match[str] |
42 |
| -) -> str: |
43 |
| - if ( |
44 |
| - matchobj.group("owner") == origin_repo.owner.name |
45 |
| - and matchobj.group("repo") == origin_repo.name |
46 |
| - ): |
47 |
| - # Only short hand if link comes from same repo |
48 |
| - return f"[#{matchobj.group('number')}]({matchobj.group()})" |
49 |
| - return matchobj.group() |
50 |
| - |
51 |
| - |
52 |
| -def reformat_converted_discussion_header( |
53 |
| - body: str | None, repo: RepositoryWebhooks |
54 |
| -) -> str | None: |
55 |
| - if not body: |
56 |
| - return body |
57 |
| - body = SUP_HTML.sub( |
58 |
| - lambda x: "".join(f"\n-# {line}\n" for line in x.group(1).splitlines()), body |
59 |
| - ) |
60 |
| - body = DISUCSSION_DIV_TAG.sub(r"\g<1>", body) |
61 |
| - body = GITHUB_DISCUSSION_URL.sub(partial(shorten_same_repo_links, repo), body) |
62 |
| - return body # noqa: RET504 |
| 27 | +def remove_discussion_div(body: str | None) -> str | None: |
| 28 | + return DISUCSSION_DIV_TAG.sub(r"\g<1>", body) if body else body |
63 | 29 |
|
64 | 30 |
|
65 | 31 | class IssueLike(Protocol):
|
@@ -94,13 +60,14 @@ def register_hooks(bot: GhosttyBot, webhook: Monalisten) -> None:
|
94 | 60 | @webhook.event.issues.opened
|
95 | 61 | async def _(event: events.IssuesOpened) -> None:
|
96 | 62 | issue = event.issue
|
97 |
| - body = reformat_converted_discussion_header(issue.body, event.repository) |
| 63 | + body = remove_discussion_div(issue.body) |
98 | 64 | await send_embed(
|
99 | 65 | bot,
|
100 | 66 | event.sender,
|
101 | 67 | issue_embed_content(issue, "opened {}", body),
|
102 | 68 | issue_footer(issue, emoji="issue_open"),
|
103 | 69 | color="green",
|
| 70 | + origin_repo=event.repository, |
104 | 71 | )
|
105 | 72 |
|
106 | 73 | @webhook.event.issues.closed
|
@@ -206,4 +173,5 @@ async def _(event: events.IssueCommentCreated) -> None:
|
206 | 173 | event.sender,
|
207 | 174 | EmbedContent(title, event.comment.html_url, event.comment.body),
|
208 | 175 | Footer(emoji, f"{entity}: {issue.title}"),
|
| 176 | + origin_repo=event.repository, |
209 | 177 | )
|
0 commit comments