Refactor codebase to by DRY
This commit is contained in:
parent
c925079e8b
commit
83a526c533
13 changed files with 320 additions and 131 deletions
|
|
@ -1,12 +1,15 @@
|
|||
import logging
|
||||
import re
|
||||
from typing import Any, List, Tuple
|
||||
from typing import Any, List, Optional, Tuple
|
||||
|
||||
import attr
|
||||
from fastapi import Request
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBasicCredentials
|
||||
from jinja2 import TemplateNotFound
|
||||
from mautrix.types import Format, MessageType, TextMessageEventContent
|
||||
from mautrix.util.formatter import parse_html
|
||||
|
||||
from ..config import RoutingKey
|
||||
from ..util.template import TemplateManager, TemplateUtil
|
||||
from .types import OTHER_ENUMS, Action, EventParse # type: ignore
|
||||
|
||||
|
|
@ -18,7 +21,17 @@ messages = TemplateManager("gitlab", "messages")
|
|||
templates = TemplateManager("gitlab", "mixins")
|
||||
|
||||
|
||||
async def parse_event(x_gitlab_event: str, payload: Any) -> List[Tuple[str, str]]:
|
||||
async def authorize(
|
||||
route: RoutingKey,
|
||||
request: Request,
|
||||
basic_credentials: Optional[HTTPBasicCredentials],
|
||||
bearer_credentials: Optional[HTTPAuthorizationCredentials],
|
||||
) -> bool:
|
||||
provided: Optional[str] = request.headers.get("x-gitlab-token")
|
||||
return provided == route.secret_token
|
||||
|
||||
|
||||
async def handle_event(x_gitlab_event: str, payload: Any) -> List[Tuple[str, str]]:
|
||||
evt = EventParse[x_gitlab_event].deserialize(payload)
|
||||
try:
|
||||
tpl = messages[evt.template_name]
|
||||
|
|
@ -68,3 +81,10 @@ async def parse_event(x_gitlab_event: str, payload: Any) -> List[Tuple[str, str]
|
|||
}
|
||||
msgs.append((content.body, content.formatted_body))
|
||||
return msgs
|
||||
|
||||
|
||||
async def parse_event(
|
||||
route: RoutingKey, payload: Any, request: Request
|
||||
) -> List[Tuple[str, str]]:
|
||||
x_gitlab_event = request.headers.get("x-gitlab-event")
|
||||
return await handle_event(x_gitlab_event, payload)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue