Fix /deployment/version/ endpoint to show commit_date and add deployment_date

- Fix commit_date parsing from git logs (was showing 'unknown')
- Add deployment_date field using manage.py modification time
- Improve git log parsing to handle author names with spaces
- Both dates now show in proper format: YYYY-MM-DD HH:MM:SS +timezone

The endpoint now returns:
- commit_date: When the commit was made (from git logs)
- deployment_date: When the code was deployed (from file mtime)
This commit is contained in:
Oliver Falk
2025-10-18 12:48:37 +02:00
parent 6f39be3b6d
commit c7efcb7246

View File

@@ -876,26 +876,31 @@ def _get_git_info_from_files():
f.seek(max(0, file_size - chunk_size)) f.seek(max(0, file_size - chunk_size))
chunk = f.read().decode("utf-8", errors="ignore") chunk = f.read().decode("utf-8", errors="ignore")
# Find the last newline # Find the last non-empty line
last_newline = chunk.rfind("\n") lines = chunk.split("\n")
if last_newline != -1: last_line = None
last_line = chunk[last_newline + 1:].strip() for line in reversed(lines):
else: if line.strip():
last_line = chunk.strip() last_line = line.strip()
break
if last_line: if last_line:
# Git log format: <old_hash> <new_hash> <author> <timestamp> <timezone> <message> # Git log format: <old_hash> <new_hash> <author> <timestamp> <timezone> <message>
parts = last_line.split("\t") # The format uses spaces, not tabs
if len(parts) >= 2: parts = last_line.split()
if len(parts) >= 6:
# Extract timestamp and convert to readable date # Extract timestamp and convert to readable date
timestamp_part = parts[0].split()[-2] # Get timestamp # Format: <old_hash> <new_hash> <author_name> <author_email> <timestamp> <timezone> <message>
if timestamp_part.isdigit(): # We need to find the timestamp which is after the author email
for i, part in enumerate(parts):
if part.isdigit() and len(part) == 10: # Unix timestamp
import datetime import datetime
timestamp = int(timestamp_part) timestamp = int(part)
commit_date = datetime.datetime.fromtimestamp( commit_date = datetime.datetime.fromtimestamp(
timestamp timestamp
).strftime("%Y-%m-%d %H:%M:%S %z") ).strftime("%Y-%m-%d %H:%M:%S %z")
break
except (ValueError, IndexError, UnicodeDecodeError): except (ValueError, IndexError, UnicodeDecodeError):
pass pass
@@ -911,11 +916,27 @@ def _get_git_info_from_files():
except Exception: except Exception:
commit_date = "unknown" commit_date = "unknown"
# Get deployment date from file modification time
# Use manage.py as it's always updated during deployment
deployment_date = None
manage_py_path = path.join(project_root, "manage.py")
if path.exists(manage_py_path):
try:
import datetime
mtime = path.getmtime(manage_py_path)
deployment_date = datetime.datetime.fromtimestamp(mtime).strftime(
"%Y-%m-%d %H:%M:%S %z"
)
except Exception:
deployment_date = "unknown"
return { return {
"commit_hash": commit_hash, "commit_hash": commit_hash,
"short_hash": commit_hash[:7] if len(commit_hash) >= 7 else commit_hash, "short_hash": commit_hash[:7] if len(commit_hash) >= 7 else commit_hash,
"branch": branch_name, "branch": branch_name,
"commit_date": commit_date or "unknown", "commit_date": commit_date or "unknown",
"deployment_date": deployment_date or "unknown",
"deployment_status": "active", "deployment_status": "active",
"version": f"{branch_name}-{commit_hash[:7] if len(commit_hash) >= 7 else commit_hash}", "version": f"{branch_name}-{commit_hash[:7] if len(commit_hash) >= 7 else commit_hash}",
} }