Generics & visitor pattern?
This commit is contained in:
parent
3845cc7ecf
commit
1440ec51b7
4 changed files with 33 additions and 6 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import logging
|
||||
import uuid
|
||||
from typing import Annotated
|
||||
from typing import Annotated, List
|
||||
|
||||
from injectable import injectable, autowired, Autowired
|
||||
|
||||
|
|
@ -8,6 +9,7 @@ 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):
|
||||
|
|
@ -24,7 +26,21 @@ class WebhookProcessor(EnqueuedProcessor):
|
|||
def enqueue(self, event: WebhookEvent):
|
||||
self._enqueue(Task(uuid.UUID(), self, event))
|
||||
|
||||
def _process(self, task: Task) -> Result:
|
||||
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
|
||||
|
|
|
|||
|
|
@ -3,15 +3,17 @@ import uuid
|
|||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from multiprocessing import Queue, Process
|
||||
from typing import TypeVar
|
||||
|
||||
from injectable import injectable
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
_id: uuid.UUID
|
||||
class Task[T]:
|
||||
id: uuid.UUID
|
||||
processor: 'EnqueuedProcessor'
|
||||
payload: object
|
||||
payload: T
|
||||
|
||||
@dataclass
|
||||
class Result:
|
||||
|
|
|
|||
|
|
@ -1,14 +1,19 @@
|
|||
import logging
|
||||
|
||||
import docker
|
||||
from docker.models.containers import Container
|
||||
from injectable import injectable
|
||||
|
||||
from app.model.containers import Tree, Compose, SimpleContainer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@injectable(singleton=True)
|
||||
class DockerService:
|
||||
def __init__(self):
|
||||
self._client = docker.from_env()
|
||||
# logger.info(f"Docker client initialized. Plugins: {self._client.plugins()}")
|
||||
self._tree = self._init_tree()
|
||||
|
||||
def _init_tree(self) -> Tree:
|
||||
|
|
|
|||
|
|
@ -13,15 +13,19 @@ class GitService:
|
|||
self._repo.git.checkout(self._settings.git.branch)
|
||||
self._origin: Remote = self._repo.remotes.origin
|
||||
|
||||
|
||||
def get_modified_compose(self) -> str | None:
|
||||
self._update()
|
||||
return self._diff()
|
||||
|
||||
def get_new_commit_hash(self) -> str:
|
||||
self._update()
|
||||
return self._repo.head.commit.hexsha
|
||||
|
||||
@staticmethod
|
||||
def _check_preconditions(config: GitConfig) -> Repo:
|
||||
def clone():
|
||||
return Repo.clone_from(config.url, config.path, branch=config.branch)
|
||||
|
||||
import os
|
||||
if not config.path.exists():
|
||||
return clone()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue