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))
chunk = f.read().decode("utf-8", errors="ignore")
# Find the last newline
last_newline = chunk.rfind("\n")
if last_newline != -1:
last_line = chunk[last_newline + 1:].strip()
else:
last_line = chunk.strip()
# Find the last non-empty line
lines = chunk.split("\n")
last_line = None
for line in reversed(lines):
if line.strip():
last_line = line.strip()
break
if last_line:
# Git log format: <old_hash> <new_hash> <author> <timestamp> <timezone> <message>
parts = last_line.split("\t")
if len(parts) >= 2:
# The format uses spaces, not tabs
parts = last_line.split()
if len(parts) >= 6:
# Extract timestamp and convert to readable date
timestamp_part = parts[0].split()[-2] # Get timestamp
if timestamp_part.isdigit():
import datetime
# Format: <old_hash> <new_hash> <author_name> <author_email> <timestamp> <timezone> <message>
# 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
timestamp = int(timestamp_part)
commit_date = datetime.datetime.fromtimestamp(
timestamp
).strftime("%Y-%m-%d %H:%M:%S %z")
timestamp = int(part)
commit_date = datetime.datetime.fromtimestamp(
timestamp
).strftime("%Y-%m-%d %H:%M:%S %z")
break
except (ValueError, IndexError, UnicodeDecodeError):
pass
@@ -911,11 +916,27 @@ def _get_git_info_from_files():
except Exception:
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 {
"commit_hash": commit_hash,
"short_hash": commit_hash[:7] if len(commit_hash) >= 7 else commit_hash,
"branch": branch_name,
"commit_date": commit_date or "unknown",
"deployment_date": deployment_date or "unknown",
"deployment_status": "active",
"version": f"{branch_name}-{commit_hash[:7] if len(commit_hash) >= 7 else commit_hash}",
}