Skip to content

Commit 6ca25ad

Browse files
MilesMorelclaude
andcommitted
Address PR review feedback on Slack alert
- Move notify_slack_of_exception below run() so definitions match call order - Always print traceback (even when SLACK_WEBHOOK_URL is unset) - Rename param tb_text -> traceback_text - Drop empty-string default on GITHUB_REPOSITORY - Extract MAX_TRACEBACK_CHARS constant with comment on Slack size limit - Add APP_ENV (dev/prod) and GITHUB_EVENT_NAME to Slack message header so prod vs dev and scheduled vs manual runs are distinguishable - Add APP_ENV to .env.example Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8ec8834 commit 6ca25ad

4 files changed

Lines changed: 39 additions & 27 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ INSTAGRAM_USERNAME=
33
INSTAGRAM_PASSWORD=
44
BLUESKY_HANDLE=
55
BLUESKY_PASSWORD=
6+
APP_ENV=
67

.github/workflows/dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
BLUESKY_HANDLE: ${{ secrets.BLUESKY_HANDLE }}
3030
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
3131
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
32+
APP_ENV: dev
3233
run: |
3334
#In order to create posts on the test accounts remove the --debugposters debug flag
3435
python ./main.py --debugsources --debugposters

.github/workflows/prod.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ jobs:
3333
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
3434
MASTODON_TOKEN: ${{ secrets.MASTODON_TOKEN }}
3535
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
36+
APP_ENV: prod
3637
run: python ./main.py

main.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,6 @@ def main():
2323
raise
2424

2525

26-
def notify_slack_of_exception(tb_text):
27-
webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
28-
if not webhook_url:
29-
print("SLACK_WEBHOOK_URL not set; skipping Slack alert.")
30-
return
31-
32-
workflow = os.environ.get("GITHUB_WORKFLOW", "local run")
33-
repo = os.environ.get("GITHUB_REPOSITORY", "")
34-
run_id = os.environ.get("GITHUB_RUN_ID")
35-
run_link = (
36-
f"https://github.com/{repo}/actions/runs/{run_id}"
37-
if repo and run_id
38-
else None
39-
)
40-
41-
header = f"CutePetsBoston run failed in *{workflow}*"
42-
if run_link:
43-
header += f" (<{run_link}|view run>)"
44-
text = f"{header}\n```{tb_text.strip()[-2500:]}```"
45-
46-
try:
47-
response = requests.post(webhook_url, json={"text": text}, timeout=10)
48-
response.raise_for_status()
49-
except Exception as slack_exc:
50-
print(f"Failed to post Slack alert: {slack_exc}")
51-
52-
5326
def create_posters(debug=False):
5427
from social_posters.debug import PosterDebug
5528

@@ -120,6 +93,42 @@ def pick_pet(pets):
12093
return random.choice(eligible)
12194

12295

96+
# Slack incoming-webhook messages have a ~40k-char limit; cap the traceback
97+
# well below that so the post stays readable and is never rejected.
98+
MAX_TRACEBACK_CHARS = 2500
99+
100+
101+
def notify_slack_of_exception(traceback_text):
102+
print(traceback_text)
103+
104+
webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
105+
if not webhook_url:
106+
print("SLACK_WEBHOOK_URL not set; skipping Slack alert.")
107+
return
108+
109+
app_env = os.environ.get("APP_ENV", "local")
110+
workflow = os.environ.get("GITHUB_WORKFLOW", "local run")
111+
event = os.environ.get("GITHUB_EVENT_NAME")
112+
repo = os.environ.get("GITHUB_REPOSITORY")
113+
run_id = os.environ.get("GITHUB_RUN_ID")
114+
run_link = (
115+
f"https://github.com/{repo}/actions/runs/{run_id}"
116+
if repo and run_id
117+
else None
118+
)
119+
120+
header = f"CutePetsBoston [{app_env}] run failed in *{workflow}*"
121+
if event:
122+
header += f" (trigger: {event})"
123+
if run_link:
124+
header += f" (<{run_link}|view run>)"
125+
text = f"{header}\n```{traceback_text.strip()[-MAX_TRACEBACK_CHARS:]}```"
126+
127+
try:
128+
response = requests.post(webhook_url, json={"text": text}, timeout=10)
129+
response.raise_for_status()
130+
except Exception as slack_exc:
131+
print(f"Failed to post Slack alert: {slack_exc}")
123132

124133

125134
if __name__ == "__main__":

0 commit comments

Comments
 (0)