Skip to content

Commit 765d642

Browse files
authored
Update get_email_webhook.py
1 parent aa72013 commit 765d642

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

get_email_webhook.py

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,56 @@ def format_repo_event(repo, action):
9595
updated_at = convert_to_utc4(repo.get("updated_at"))
9696
url = repo.get("html_url", "")
9797

98-
subject = f"[GitHub Alert] Repository {action}: {repo_name}"
99-
body = (
100-
f"A repository was {action} in your GitHub organization.\n\n"
101-
f"📌 Repository Name: {repo_name}\n"
102-
f"{visibility_icon}\n"
103-
f"👤 Owner: {owner}\n"
104-
f"🌿 Default branch: {default_branch}\n"
105-
f"🕒 Created at: {created_at}\n"
106-
f"🕒 Last updated: {updated_at}\n"
107-
f"🌍 URL: {url}\n"
108-
)
98+
# Special cases for visibility changes
99+
if action == "publicized":
100+
subject = f"[GitHub Alert] Repository changed from PRIVATE → PUBLIC: {repo_name}"
101+
body = (
102+
f"The repository visibility has been changed.\n\n"
103+
f"➡️ Now PUBLIC\n\n"
104+
f"📌 Repository Name: {repo_name}\n"
105+
f"{visibility_icon}\n"
106+
f"👤 Owner: {owner}\n"
107+
f"🌿 Default branch: {default_branch}\n"
108+
f"🕒 Created at: {created_at}\n"
109+
f"🕒 Last updated: {updated_at}\n"
110+
f"🌍 URL: {url}\n"
111+
)
112+
elif action == "privatized":
113+
subject = f"[GitHub Alert] Repository changed from PUBLIC → PRIVATE: {repo_name}"
114+
body = (
115+
f"The repository visibility has been changed.\n\n"
116+
f"➡️ Now PRIVATE\n\n"
117+
f"📌 Repository Name: {repo_name}\n"
118+
f"{visibility_icon}\n"
119+
f"👤 Owner: {owner}\n"
120+
f"🌿 Default branch: {default_branch}\n"
121+
f"🕒 Created at: {created_at}\n"
122+
f"🕒 Last updated: {updated_at}\n"
123+
f"🌍 URL: {url}\n"
124+
)
125+
else:
126+
# Generic events
127+
action_labels = {
128+
"created": "Repository created",
129+
"deleted": "Repository deleted",
130+
"archived": "Repository archived",
131+
"unarchived": "Repository unarchived",
132+
"edited": "Repository edited",
133+
"renamed": "Repository renamed",
134+
"transferred": "Repository transferred"
135+
}
136+
subject = f"[GitHub Alert] {action_labels.get(action, action)}: {repo_name}"
137+
body = (
138+
f"A repository event occurred: {action_labels.get(action, action)}\n\n"
139+
f"📌 Repository Name: {repo_name}\n"
140+
f"{visibility_icon}\n"
141+
f"👤 Owner: {owner}\n"
142+
f"🌿 Default branch: {default_branch}\n"
143+
f"🕒 Created at: {created_at}\n"
144+
f"🕒 Last updated: {updated_at}\n"
145+
f"🌍 URL: {url}\n"
146+
)
147+
109148
return subject, body
110149

111150
# --- GitHub Webhook + Health Handlers ---
@@ -131,13 +170,19 @@ def github_webhook():
131170
return "❌ Bad payload", 400
132171

133172
event = request.headers.get("X-GitHub-Event", "")
173+
action = data.get("action")
174+
175+
repo_actions = [
176+
"created", "deleted", "archived", "unarchived",
177+
"publicized", "privatized", "edited", "renamed", "transferred"
178+
]
134179

135-
if event == "repository" and data.get("action") in ["created", "deleted"]:
136-
subject, body = format_repo_event(data["repository"], data["action"])
180+
if event == "repository" and action in repo_actions:
181+
subject, body = format_repo_event(data["repository"], action)
137182
print(f"📩 Sending email alert: {subject}")
138183
send_email_via_graph(subject, body)
139184
else:
140-
print(f"ℹ️ Ignored event: {event}, action: {data.get('action')}")
185+
print(f"ℹ️ Ignored event: {event}, action: {action}")
141186

142187
return "OK", 200
143188

@@ -150,16 +195,17 @@ def health_check():
150195
if TEST_MODE:
151196
print("🔹 TEST_MODE: sending test email")
152197
test_repo = {
153-
"full_name": "quantori/sadsrepo",
154-
"private": True,
198+
"full_name": "quantori/test-repo",
199+
"private": False,
155200
"owner": {"login": "quantori"},
156201
"default_branch": "main",
157202
"created_at": "2025-09-09T13:06:02Z",
158203
"updated_at": "2025-09-09T13:09:20Z",
159-
"html_url": "https://github.com/quantori/sadsrepo"
204+
"html_url": "https://github.com/quantori/test-repo"
160205
}
161-
subject, body = format_repo_event(test_repo, "created")
206+
subject, body = format_repo_event(test_repo, "publicized")
162207
send_email_via_graph(subject, body)
163208
else:
164209
print("✅ Flask is up and listening on /webhook and /health")
165-
app.run(host="0.0.0.0", port=8000)
210+
port = int(os.environ.get("PORT", 8000))
211+
app.run(host="0.0.0.0", port=port)

0 commit comments

Comments
 (0)