Format, lint, type
This commit is contained in:
parent
a1ae717c8f
commit
c925079e8b
8 changed files with 159 additions and 91 deletions
|
|
@ -1,18 +1,14 @@
|
|||
import re
|
||||
import attr
|
||||
import logging
|
||||
from typing import Any, Tuple
|
||||
from jinja2 import TemplateNotFound
|
||||
import re
|
||||
from typing import Any, List, Tuple
|
||||
|
||||
from mautrix.types import (EventType, RoomID, StateEvent, Membership, MessageType, JSON,
|
||||
TextMessageEventContent, Format, ReactionEventContent, RelationType)
|
||||
import attr
|
||||
from jinja2 import TemplateNotFound
|
||||
from mautrix.types import Format, MessageType, TextMessageEventContent
|
||||
from mautrix.util.formatter import parse_html
|
||||
|
||||
from ..util.template import TemplateManager, TemplateUtil
|
||||
|
||||
from .types import EventParse, OTHER_ENUMS, Action
|
||||
|
||||
from ..common import COLOR_ALARM, COLOR_OK, COLOR_UNKNOWN
|
||||
from .types import OTHER_ENUMS, Action, EventParse # type: ignore
|
||||
|
||||
spaces = re.compile(" +")
|
||||
space = " "
|
||||
|
|
@ -21,22 +17,23 @@ space = " "
|
|||
messages = TemplateManager("gitlab", "messages")
|
||||
templates = TemplateManager("gitlab", "mixins")
|
||||
|
||||
async def parse_event(x_gitlab_event: str, payload: Any) -> Tuple[str, str]:
|
||||
|
||||
async def parse_event(x_gitlab_event: str, payload: Any) -> List[Tuple[str, str]]:
|
||||
evt = EventParse[x_gitlab_event].deserialize(payload)
|
||||
print("processing", evt)
|
||||
try:
|
||||
tpl = messages[evt.template_name]
|
||||
except TemplateNotFound as e:
|
||||
except TemplateNotFound:
|
||||
msg = f"Received unhandled gitlab event type {x_gitlab_event}"
|
||||
logging.info(msg)
|
||||
logging.info(payload)
|
||||
return [(msg, msg)]
|
||||
logging.error(msg)
|
||||
logging.debug(payload)
|
||||
return []
|
||||
|
||||
aborted = False
|
||||
|
||||
def abort() -> None:
|
||||
nonlocal aborted
|
||||
aborted = True
|
||||
|
||||
base_args = {
|
||||
**{field.key: field for field in Action if field.key.isupper()},
|
||||
**OTHER_ENUMS,
|
||||
|
|
@ -45,14 +42,13 @@ async def parse_event(x_gitlab_event: str, payload: Any) -> Tuple[str, str]:
|
|||
|
||||
msgs = []
|
||||
for subevt in evt.preprocess():
|
||||
print("preprocessing", subevt)
|
||||
args = {
|
||||
**attr.asdict(subevt, recurse=False),
|
||||
**{key: getattr(subevt, key) for key in subevt.event_properties},
|
||||
"abort": abort,
|
||||
**base_args,
|
||||
**base_args, # type: ignore
|
||||
}
|
||||
args["templates"] = templates.proxy(args)
|
||||
args["templates"] = templates.proxy(args) # type: ignore
|
||||
|
||||
html = tpl.render(**args)
|
||||
if not html or aborted:
|
||||
|
|
@ -60,11 +56,15 @@ async def parse_event(x_gitlab_event: str, payload: Any) -> Tuple[str, str]:
|
|||
continue
|
||||
html = spaces.sub(space, html.strip())
|
||||
|
||||
content = TextMessageEventContent(msgtype=MessageType.TEXT, format=Format.HTML,
|
||||
formatted_body=html, body=await parse_html(html))
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT,
|
||||
format=Format.HTML,
|
||||
formatted_body=html,
|
||||
body=await parse_html(html),
|
||||
)
|
||||
content["xyz.maubot.gitlab.webhook"] = {
|
||||
"event_type": x_gitlab_event,
|
||||
**subevt.meta,
|
||||
}
|
||||
msgs.append((content.body, content.formatted_body))
|
||||
return msgs
|
||||
return msgs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue