Skip to content

Commit 642752f

Browse files
committed
updated file with port info
1 parent 1f5b6e1 commit 642752f

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

get_email_webhook.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# Only import Flask if not in TEST_MODE
1212
if not TEST_MODE:
13-
from flask import Flask, request
13+
from flask import Flask, request, jsonify
1414
app = Flask(__name__)
1515

1616
# --- Microsoft Graph Email Sending Function ---
@@ -23,7 +23,7 @@ def send_email_via_graph(subject, body):
2323

2424
if not all([TENANT_ID, CLIENT_ID, CLIENT_SECRET, FROM_EMAIL, TO_EMAIL]):
2525
print("❌ Missing required environment variables")
26-
return
26+
return False
2727

2828
try:
2929
app_msal = ConfidentialClientApplication(
@@ -35,7 +35,7 @@ def send_email_via_graph(subject, body):
3535
access_token = token.get("access_token")
3636
if not access_token:
3737
print(f"❌ Failed to get access token: {token}")
38-
return
38+
return False
3939

4040
email_msg = {
4141
"message": {
@@ -53,11 +53,14 @@ def send_email_via_graph(subject, body):
5353

5454
if response.status_code == 202:
5555
print(f"✅ Email sent to {TO_EMAIL}")
56+
return True
5657
else:
5758
print(f"❌ Failed to send email: {response.status_code} {response.text}")
59+
return False
5860

5961
except Exception as e:
6062
print(f"❌ Exception occurred while sending email: {e}")
63+
return False
6164

6265
# --- Verify GitHub webhook signature ---
6366
def verify_github_signature(payload_body, signature, secret):
@@ -72,23 +75,23 @@ def verify_github_signature(payload_body, signature, secret):
7275
expected_signature = "sha256=" + mac.hexdigest()
7376
return hmac.compare_digest(expected_signature, signature)
7477

75-
# --- GitHub Webhook + Health Handlers ---
78+
# --- GitHub Webhook Handler ---
7679
if not TEST_MODE:
7780
@app.route("/webhook", methods=["POST"])
7881
def github_webhook():
7982
payload_body = request.data
8083
signature = request.headers.get("X-Hub-Signature-256")
81-
secret = os.getenv("GITHUB_WEBHOOK_SECRET") # ✅ Use webhook secret here
84+
secret = os.getenv("GITHUB_WEBHOOK_SECRET") # ✅ Correct usage
8285

83-
# --- Debug logging ---
86+
# Debug logging
8487
print("📥 Incoming GitHub webhook")
8588
print(f"📥 Payload size: {len(payload_body)} bytes")
8689
print(f"📥 GitHub Event: {request.headers.get('X-GitHub-Event')}")
8790
print(f"📥 Signature header: {signature}")
8891
print(f"📥 Secret length: {len(secret) if secret else 'None'}")
8992

9093
if not verify_github_signature(payload_body, signature, secret):
91-
print(f"❌ Invalid signature! Webhook rejected.")
94+
print("❌ Invalid signature! Webhook rejected.")
9295
return "❌ Invalid signature", 401
9396

9497
try:
@@ -99,6 +102,7 @@ def github_webhook():
99102

100103
event = request.headers.get("X-GitHub-Event", "")
101104

105+
# Handle repository create/delete events
102106
if event == "repository" and data.get("action") in ["created", "deleted"]:
103107
repo_name = data["repository"]["full_name"]
104108
action = data["action"]
@@ -119,17 +123,18 @@ def github_webhook():
119123
# Health check endpoint
120124
@app.route("/health", methods=["GET"])
121125
def health_check():
122-
return {"status": "running"}, 200
126+
return jsonify({"status": "running"}), 200
123127

124128
# --- Main Entry Point ---
125129
if __name__ == "__main__":
126-
port = int(os.getenv("PORT", 8000)) # Use Azure PORT or default 8000
127130
if TEST_MODE:
128131
print("🔹 TEST_MODE: sending test email")
129132
send_email_via_graph(
130133
"[Test] Graph Email",
131134
"This is a test email sent via Microsoft Graph with application permissions."
132135
)
133136
else:
134-
print(f"✅ Flask is up and listening on /webhook and /health (port {port})")
137+
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))
135140
app.run(host="0.0.0.0", port=port)

0 commit comments

Comments
 (0)