47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
|
from typing import Any, Callable, Literal
|
||
|
from buildbot.reporters.message import MessageFormatterBase
|
||
|
import dataclasses
|
||
|
|
||
|
@dataclasses.dataclass
|
||
|
class CallbackPayloadBuild:
|
||
|
# buddy i have no idea what the fuck is in this
|
||
|
build: dict[str, Any]
|
||
|
|
||
|
@dataclasses.dataclass
|
||
|
class CallbackPayloadBuildSet:
|
||
|
buildset: dict[str, Any]
|
||
|
# i have no idea what the fuck is in this honestly
|
||
|
builds: Any
|
||
|
|
||
|
@dataclasses.dataclass
|
||
|
class CallbackReturn:
|
||
|
body: str | None = None
|
||
|
subject: str | None = None
|
||
|
type: Literal['plain'] | Literal['html'] | Literal['json'] = 'plain'
|
||
|
extra_info: dict[str, Any] | None = None
|
||
|
# FIXME: support other template types, if they actually become necessary
|
||
|
template_type: Literal['plain'] = 'plain'
|
||
|
|
||
|
class ReasonableMessageFormatter(MessageFormatterBase):
|
||
|
"""
|
||
|
Message formatter which uses strongly typed data classes to reduce suffering slightly.
|
||
|
"""
|
||
|
CallbackFunc = Callable[[CallbackPayloadBuild | CallbackPayloadBuildSet], CallbackReturn]
|
||
|
def __init__(self, function: CallbackFunc, template_type: str, **kwargs):
|
||
|
super().__init__(**kwargs)
|
||
|
self.template_type = template_type
|
||
|
self._function = function
|
||
|
|
||
|
def format_message_for_build(self, master, build, **kwargs):
|
||
|
return dataclasses.asdict(self._function(CallbackPayloadBuild(build=build)))
|
||
|
|
||
|
def format_message_for_buildset(self, master, buildset, builds, **kwargs):
|
||
|
return dataclasses.asdict(self._function(CallbackPayloadBuildSet(buildset=buildset, builds=builds)))
|
||
|
|
||
|
# These only exist as callbacks, the only one actually used is render_message_dict
|
||
|
def render_message_body(self, context):
|
||
|
return None
|
||
|
|
||
|
def render_message_subject(self, context):
|
||
|
return None
|