Skip to content

Commit 1807d38

Browse files
committed
added information about repo owner
1 parent c832558 commit 1807d38

File tree

2 files changed

+69
-60
lines changed

2 files changed

+69
-60
lines changed

get_email_webhook.py

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ 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-
7463
# --- Verify GitHub webhook signature ---
7564
def verify_github_signature(payload_body, signature, secret):
7665
if not secret:
@@ -92,15 +81,15 @@ def github_webhook():
9281
signature = request.headers.get("X-Hub-Signature-256")
9382
secret = os.getenv("GITHUB_WEBHOOK_SECRET")
9483

95-
# Debug logging
84+
# --- Debug logging ---
9685
print("📥 Incoming GitHub webhook")
9786
print(f"📥 Payload size: {len(payload_body)} bytes")
9887
print(f"📥 GitHub Event: {request.headers.get('X-GitHub-Event')}")
9988
print(f"📥 Signature header: {signature}")
10089
print(f"📥 Secret length: {len(secret) if secret else 'None'}")
10190

10291
if not verify_github_signature(payload_body, signature, secret):
103-
print("❌ Invalid signature! Webhook rejected.")
92+
print(f"❌ Invalid signature! Webhook rejected.")
10493
return "❌ Invalid signature", 401
10594

10695
try:
@@ -112,34 +101,31 @@ def github_webhook():
112101
event = request.headers.get("X-GitHub-Event", "")
113102

114103
if event == "repository" and data.get("action") in ["created", "deleted"]:
115-
repo = data.get("repository", {})
104+
repo = data["repository"]
105+
repo_name = repo["full_name"]
116106
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-
"""
107+
default_branch = repo.get("default_branch", "N/A")
108+
owner = repo["owner"]["login"]
109+
creator = data["sender"]["login"]
110+
created_at = repo.get("created_at")
111+
if created_at:
112+
# Remove T and Z
113+
created_at = created_at.replace("T", " ").replace("Z", "")
114+
115+
subject = f"[GitHub Alert] Repository {action}: {repo_name}"
116+
body = (
117+
f"A repository was {action} in your GitHub organization.\n\n"
118+
f"Repository: {repo_name}\n"
119+
f"Owner: {owner}\n"
120+
f"Default branch: {default_branch}\n"
121+
f"Action by: {creator}\n"
122+
)
123+
if created_at:
124+
body += f"Created at: {created_at}\n"
125+
body += f"\nFull payload:\n{json.dumps(data, indent=2)}"
140126

141127
print(f"📩 Sending email alert: {subject}")
142-
send_email_via_graph(subject, body.strip())
128+
send_email_via_graph(subject, body)
143129
else:
144130
print(f"ℹ️ Ignored event: {event}, action: {data.get('action')}")
145131

@@ -160,4 +146,4 @@ def health_check():
160146
)
161147
else:
162148
print("✅ Flask is up and listening on /webhook and /health")
163-
app.run(host="0.0.0.0", port=int(os.getenv("PORT", 8000)))
149+
app.run(host="0.0.0.0", port=8000)

get_email_webhook_old_musha.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import hashlib
55
import requests
66
from msal import ConfidentialClientApplication
7+
from datetime import datetime
78

89
# --- Determine mode ---
910
TEST_MODE = os.getenv("TEST_MODE", "true").lower() == "true"
1011

1112
# Only import Flask if not in TEST_MODE
1213
if not TEST_MODE:
13-
from flask import Flask, request, jsonify
14+
from flask import Flask, request
1415
app = Flask(__name__)
1516

1617
# --- Microsoft Graph Email Sending Function ---
@@ -23,7 +24,7 @@ def send_email_via_graph(subject, body):
2324

2425
if not all([TENANT_ID, CLIENT_ID, CLIENT_SECRET, FROM_EMAIL, TO_EMAIL]):
2526
print("❌ Missing required environment variables")
26-
return False
27+
return
2728

2829
try:
2930
app_msal = ConfidentialClientApplication(
@@ -35,7 +36,7 @@ def send_email_via_graph(subject, body):
3536
access_token = token.get("access_token")
3637
if not access_token:
3738
print(f"❌ Failed to get access token: {token}")
38-
return False
39+
return
3940

4041
email_msg = {
4142
"message": {
@@ -53,14 +54,22 @@ def send_email_via_graph(subject, body):
5354

5455
if response.status_code == 202:
5556
print(f"✅ Email sent to {TO_EMAIL}")
56-
return True
5757
else:
5858
print(f"❌ Failed to send email: {response.status_code} {response.text}")
59-
return False
6059

6160
except Exception as e:
6261
print(f"❌ Exception occurred while sending email: {e}")
63-
return False
62+
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
6473

6574
# --- Verify GitHub webhook signature ---
6675
def verify_github_signature(payload_body, signature, secret):
@@ -75,13 +84,13 @@ def verify_github_signature(payload_body, signature, secret):
7584
expected_signature = "sha256=" + mac.hexdigest()
7685
return hmac.compare_digest(expected_signature, signature)
7786

78-
# --- GitHub Webhook Handler ---
87+
# --- GitHub Webhook + Health Handlers ---
7988
if not TEST_MODE:
8089
@app.route("/webhook", methods=["POST"])
8190
def github_webhook():
8291
payload_body = request.data
8392
signature = request.headers.get("X-Hub-Signature-256")
84-
secret = os.getenv("GITHUB_WEBHOOK_SECRET") # ✅ Correct usage
93+
secret = os.getenv("GITHUB_WEBHOOK_SECRET")
8594

8695
# Debug logging
8796
print("📥 Incoming GitHub webhook")
@@ -102,19 +111,35 @@ def github_webhook():
102111

103112
event = request.headers.get("X-GitHub-Event", "")
104113

105-
# Handle repository create/delete events
106114
if event == "repository" and data.get("action") in ["created", "deleted"]:
107-
repo_name = data["repository"]["full_name"]
115+
repo = data.get("repository", {})
108116
action = data["action"]
109117

110-
subject = f"[GitHub Alert] Repository {action}: {repo_name}"
111-
body = (
112-
f"A repository was {action} in your GitHub organization.\n\n"
113-
f"Details:\n{json.dumps(data, indent=2)}"
114-
)
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+
"""
115140

116141
print(f"📩 Sending email alert: {subject}")
117-
send_email_via_graph(subject, body)
142+
send_email_via_graph(subject, body.strip())
118143
else:
119144
print(f"ℹ️ Ignored event: {event}, action: {data.get('action')}")
120145

@@ -123,18 +148,16 @@ def github_webhook():
123148
# Health check endpoint
124149
@app.route("/health", methods=["GET"])
125150
def health_check():
126-
return jsonify({"status": "running"}), 200
151+
return {"status": "running"}, 200
127152

128153
# --- Main Entry Point ---
129154
if __name__ == "__main__":
130155
if TEST_MODE:
131156
print("🔹 TEST_MODE: sending test email")
132157
send_email_via_graph(
133158
"[Test] Graph Email",
134-
"This is a test email sent via Microsoft Graph with application permissions."
159+
"The information of Quantori's GitHub repositories has been updated"
135160
)
136161
else:
137162
print("✅ Flask is up and listening on /webhook and /health")
138-
# Azure expects port 8000 by default for containerized apps
139-
port = int(os.getenv("PORT", 8000))
140-
app.run(host="0.0.0.0", port=port)
163+
app.run(host="0.0.0.0", port=int(os.getenv("PORT", 8000)))

0 commit comments

Comments
 (0)