87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import sys
|
|
import json
|
|
import subprocess
|
|
|
|
# Fügen Sie das Projekt-Stammverzeichnis zum Python-Pfad hinzu
|
|
# Das ist das Verzeichnis, in dem sich das Hook-Skript befindet
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
try:
|
|
from dev_session import append_blocks_to_notion_page, decimal_hours_to_hhmm
|
|
from datetime import datetime
|
|
except ImportError:
|
|
print("Fehler: Konnte dev_session.py nicht importieren.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
SESSION_DIR = ".dev_session"
|
|
SESSION_FILE_PATH = os.path.join(SESSION_DIR, "SESSION_INFO")
|
|
|
|
def get_last_commit_message() -> str:
|
|
"""Holt die letzte Commit-Nachricht mit git."""
|
|
try:
|
|
result = subprocess.run(
|
|
["git", "log", "-1", "--pretty=%B"],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True
|
|
)
|
|
return result.stdout.strip()
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
return ""
|
|
|
|
def main():
|
|
"""Hauptfunktion des Git-Hooks."""
|
|
if not os.path.exists(SESSION_FILE_PATH):
|
|
sys.exit(0)
|
|
|
|
try:
|
|
with open(SESSION_FILE_PATH, "r") as f:
|
|
session_data = json.load(f)
|
|
|
|
task_id = session_data.get("task_id")
|
|
token = session_data.get("token")
|
|
session_start_time_str = session_data.get("session_start_time")
|
|
|
|
if not task_id or not token:
|
|
sys.exit(0)
|
|
|
|
commit_message = get_last_commit_message()
|
|
|
|
if commit_message:
|
|
time_comment = ""
|
|
if session_start_time_str:
|
|
session_start_time = datetime.fromisoformat(session_start_time_str)
|
|
elapsed_time = datetime.now() - session_start_time
|
|
elapsed_hours = elapsed_time.total_seconds() / 3600
|
|
elapsed_hhmm = decimal_hours_to_hhmm(elapsed_hours)
|
|
time_comment = f"⏱️ Arbeitszeit in dieser Session: {elapsed_hhmm}\\n"
|
|
|
|
report_content = f"✅ New Commit:\n{time_comment}---\n{commit_message}"
|
|
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M')
|
|
|
|
notion_blocks = [
|
|
{
|
|
"object": "block",
|
|
"type": "heading_2",
|
|
"heading_2": {
|
|
"rich_text": [{"type": "text", "text": {"content": f"🤖 Git Commit ({timestamp})"}}]
|
|
}
|
|
},
|
|
{
|
|
"object": "block",
|
|
"type": "code",
|
|
"code": {
|
|
"rich_text": [{"type": "text", "text": {"content": report_content}}],
|
|
"language": "yaml"
|
|
}
|
|
}
|
|
]
|
|
append_blocks_to_notion_page(token, task_id, notion_blocks)
|
|
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
main() |