51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import logging
|
|
import uuid
|
|
from typing import Annotated, List
|
|
|
|
from injectable import injectable, autowired, Autowired
|
|
|
|
from app.core.queue import EnqueuedProcessor, ProcessQueue, Task, Result
|
|
from app.model.healthcheck import HealthCheck
|
|
from app.model.webhook import WebhookEvent
|
|
from app.services import DockerService, GitService, Passwords
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@injectable(singleton=True)
|
|
class WebhookProcessor(EnqueuedProcessor):
|
|
@autowired
|
|
def __init__(self, docker: Annotated[DockerService, Autowired],
|
|
git: Annotated[GitService, Autowired],
|
|
keepass: Annotated[Passwords, Autowired],
|
|
queue: Annotated[ProcessQueue, Autowired]):
|
|
super().__init__(queue)
|
|
self._docker = docker
|
|
self._git = git
|
|
self._keepass = keepass
|
|
|
|
def enqueue(self, event: WebhookEvent):
|
|
self._enqueue(Task(uuid.UUID(), self, event))
|
|
|
|
def _process(self, task: Task[WebhookEvent]) -> Result:
|
|
event: WebhookEvent = task.payload
|
|
# TODO: persist event data
|
|
commit_hash = self._git.get_new_commit_hash()
|
|
if commit_hash != event.commit:
|
|
logger.warning(f"Commit hash mismatch: {commit_hash} != {event.commit}")
|
|
return Result(task.id, False, "Commit hash mismatch")
|
|
# TODO: persist commit data
|
|
service = self._get_service(event.files)
|
|
|
|
|
|
|
|
return Result(task.id, True)
|
|
|
|
def _get_service(self, files: List[str]) -> str:
|
|
pass
|
|
|
|
@property
|
|
def health(self) -> HealthCheck:
|
|
return HealthCheck(
|
|
self._docker is not None and self._git is not None and self._keepass is not None,
|
|
f"Docker: {self._docker is not None}, Git: {self._git is not None}, KeePass: {self._keepass is not None}"
|
|
)
|