|
4 | 4 | import hashlib |
5 | 5 | import requests |
6 | 6 | from msal import ConfidentialClientApplication |
| 7 | +from datetime import datetime, timezone, timedelta |
7 | 8 |
|
8 | 9 | # --- Determine mode --- |
9 | 10 | TEST_MODE = os.getenv("TEST_MODE", "true").lower() == "true" |
@@ -72,14 +73,26 @@ def verify_github_signature(payload_body, signature, secret): |
72 | 73 | expected_signature = "sha256=" + mac.hexdigest() |
73 | 74 | return hmac.compare_digest(expected_signature, signature) |
74 | 75 |
|
| 76 | +# --- Convert UTC timestamp to UTC+4 --- |
| 77 | +def convert_to_utc4(timestamp): |
| 78 | + if not timestamp: |
| 79 | + return "N/A" |
| 80 | + try: |
| 81 | + dt_utc = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) |
| 82 | + dt_utc4 = dt_utc.astimezone(timezone(timedelta(hours=4))) |
| 83 | + return dt_utc4.strftime("%Y-%m-%d %H:%M:%S") |
| 84 | + except Exception as e: |
| 85 | + print(f"⚠️ Failed to convert timestamp {timestamp}: {e}") |
| 86 | + return timestamp |
| 87 | + |
75 | 88 | # --- Format GitHub repository event for email --- |
76 | 89 | def format_repo_event(repo, action): |
77 | 90 | repo_name = repo["full_name"] |
78 | 91 | visibility_icon = "🔒 Private" if repo.get("private") else "🌐 Public" |
79 | 92 | owner = repo["owner"]["login"] |
80 | 93 | default_branch = repo.get("default_branch", "N/A") |
81 | | - created_at = repo.get("created_at", "").replace("T", " ").replace("Z", "") |
82 | | - updated_at = repo.get("updated_at", "").replace("T", " ").replace("Z", "") |
| 94 | + created_at = convert_to_utc4(repo.get("created_at")) |
| 95 | + updated_at = convert_to_utc4(repo.get("updated_at")) |
83 | 96 | url = repo.get("html_url", "") |
84 | 97 |
|
85 | 98 | subject = f"[GitHub Alert] Repository {action}: {repo_name}" |
|
0 commit comments