@@ -60,6 +60,17 @@ def send_email_via_graph(subject, body):
6060 except Exception as e :
6161 print (f"❌ Exception occurred while sending email: { e } " )
6262
63+ # --- Format GitHub timestamp ---
64+ def format_timestamp (ts ):
65+ """Convert GitHub timestamp to 'YYYY-MM-DD HH:MM:SS' format."""
66+ if not ts :
67+ return None
68+ try :
69+ dt = datetime .strptime (ts , "%Y-%m-%dT%H:%M:%SZ" )
70+ return dt .strftime ("%Y-%m-%d %H:%M:%S" )
71+ except Exception :
72+ return ts # fallback
73+
6374# --- Verify GitHub webhook signature ---
6475def verify_github_signature (payload_body , signature , secret ):
6576 if not secret :
@@ -81,15 +92,15 @@ def github_webhook():
8192 signature = request .headers .get ("X-Hub-Signature-256" )
8293 secret = os .getenv ("GITHUB_WEBHOOK_SECRET" )
8394
84- # --- Debug logging ---
95+ # Debug logging
8596 print ("📥 Incoming GitHub webhook" )
8697 print (f"📥 Payload size: { len (payload_body )} bytes" )
8798 print (f"📥 GitHub Event: { request .headers .get ('X-GitHub-Event' )} " )
8899 print (f"📥 Signature header: { signature } " )
89100 print (f"📥 Secret length: { len (secret ) if secret else 'None' } " )
90101
91102 if not verify_github_signature (payload_body , signature , secret ):
92- print (f "❌ Invalid signature! Webhook rejected." )
103+ print ("❌ Invalid signature! Webhook rejected." )
93104 return "❌ Invalid signature" , 401
94105
95106 try :
@@ -101,38 +112,34 @@ def github_webhook():
101112 event = request .headers .get ("X-GitHub-Event" , "" )
102113
103114 if event == "repository" and data .get ("action" ) in ["created" , "deleted" ]:
104- repo = data ["repository" ]
105- repo_name = repo ["name" ]
106- visibility = repo .get ("private" , False )
107- visibility_icon = "🔒 Private" if visibility else "🌐 Public"
108- owner = repo ["owner" ]["login" ]
109- default_branch = repo .get ("default_branch" , "N/A" )
110- creator = data ["sender" ]["login" ]
111- created_at = repo .get ("created_at" )
112- updated_at = repo .get ("updated_at" )
113- url = repo .get ("html_url" , "" )
114-
115- # Remove T and Z
116- if created_at :
117- created_at = created_at .replace ("T" , " " ).replace ("Z" , "" )
118- if updated_at :
119- updated_at = updated_at .replace ("T" , " " ).replace ("Z" , "" )
120-
121- subject = f"[GitHub Alert] Repository { data ['action' ]} : { repo_name } "
122- body = (
123- f"📌 Repository Name: { repo_name } \n "
124- f"{ visibility_icon } \n "
125- f"👤 Owner: { owner } \n "
126- f"🌿 Default Branch: { default_branch } \n "
127- f"🕒 Created at: { created_at } \n "
128- f"🕒 Last updated: { updated_at } \n "
129- f"🌍 URL: { url } \n "
130- f"👤 Action by: { creator } \n \n "
131- f"Full payload:\n { json .dumps (data , indent = 2 )} "
132- )
115+ repo = data .get ("repository" , {})
116+ action = data ["action" ]
117+
118+ repo_name = repo .get ("name" )
119+ full_name = repo .get ("full_name" )
120+ org = repo .get ("owner" , {}).get ("login" )
121+ owner_id = repo .get ("owner" , {}).get ("id" )
122+ default_branch = repo .get ("default_branch" )
123+ created_at = format_timestamp (repo .get ("created_at" ))
124+ updated_at = format_timestamp (repo .get ("updated_at" ))
125+ html_url = repo .get ("html_url" )
126+
127+ subject = f"[GitHub Alert] Repository { action } : { full_name } "
128+ body = f"""
129+ A repository was { action } in your GitHub organization.
130+
131+ Repository: { repo_name }
132+ Full name: { full_name }
133+ Organization: { org }
134+ Owner ID: { owner_id }
135+ Default branch: { default_branch }
136+ Created at: { created_at }
137+ Last updated: { updated_at }
138+ URL: { html_url }
139+ """
133140
134141 print (f"📩 Sending email alert: { subject } " )
135- send_email_via_graph (subject , body )
142+ send_email_via_graph (subject , body . strip () )
136143 else :
137144 print (f"ℹ️ Ignored event: { event } , action: { data .get ('action' )} " )
138145
@@ -153,4 +160,4 @@ def health_check():
153160 )
154161 else :
155162 print ("✅ Flask is up and listening on /webhook and /health" )
156- app .run (host = "0.0.0.0" , port = 8000 )
163+ app .run (host = "0.0.0.0" , port = int ( os . getenv ( "PORT" , 8000 )) )
0 commit comments