44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import shlex
|
|
import subprocess
|
|
from typing import Dict
|
|
|
|
from . import config, output
|
|
|
|
|
|
def run_hooks(event: str, context: Dict[str, str] | None = None) -> None:
|
|
"""Voer alle geconfigureerde hooks voor een gegeven event uit.
|
|
|
|
Hooks worden gedefinieerd in `CONFIG.hooks[event]` als een lijst van
|
|
shell-commando's. We geven context door via environment-variabelen.
|
|
"""
|
|
|
|
hooks = config.CONFIG.hooks.get(event) or []
|
|
if not hooks:
|
|
return
|
|
|
|
base_env = os.environ.copy()
|
|
base_env.setdefault("GITFLOW_EVENT", event)
|
|
|
|
if context:
|
|
for key, value in context.items():
|
|
base_env[f"GITFLOW_{key.upper()}"] = value
|
|
|
|
for cmd in hooks:
|
|
# Eenvoudige logging
|
|
output.info(f"[HOOK {event}] {cmd}")
|
|
|
|
if config.CONFIG.dry_run:
|
|
output.info("[DRY-RUN] Hook niet echt uitgevoerd.")
|
|
continue
|
|
|
|
# Gebruik shlex.split zodat eenvoudige strings netjes opgesplitst worden.
|
|
args = shlex.split(cmd)
|
|
result = subprocess.run(args, env=base_env, text=True)
|
|
if result.returncode != 0:
|
|
raise RuntimeError(
|
|
f"Hook voor event '{event}' faalde met exitcode {result.returncode}: {cmd}"
|
|
)
|