Skip to content

Commit 64ae763

Browse files
Ajit Pratap Singhclaude
authored andcommitted
fix(ci): address code review feedback on Sentry→GitHub workflow
- Add timeout=30 to all HTTP requests (prevents hangs) - Use Python 3.12 native fromisoformat (handles Z natively, no fragile replace) - Fail fast on GitHub search API errors instead of silently returning false (prevents duplicate issue creation on transient API failures) - Distinguish "already_exists" 422 from other label validation errors - Move SENTRY_PROJECT to vars.SENTRY_PROJECT (repository variable) - Remove unused json import Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent aa5da0b commit 64ae763

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

.github/scripts/sentry_issues.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import os
1010
import sys
1111
import time
12-
import json
1312
from datetime import datetime, timezone, timedelta
1413

1514
import requests
@@ -63,11 +62,16 @@ def ensure_label(name: str, color: str, description: str) -> None:
6362
url,
6463
headers=GITHUB_HEADERS,
6564
json={"name": name, "color": color, "description": description},
65+
timeout=30,
6666
)
6767
if resp.status_code == 201:
6868
print(f" Created label: {name}")
6969
elif resp.status_code == 422:
70-
pass # Already exists
70+
data = resp.json()
71+
errors = data.get("errors", [])
72+
# Only swallow "already_exists" — surface other validation errors
73+
if not all(e.get("code") == "already_exists" for e in errors):
74+
print(f" Warning: label creation validation error for '{name}': {resp.text}")
7175
else:
7276
print(f" Warning: unexpected status {resp.status_code} creating label '{name}': {resp.text}")
7377

@@ -81,28 +85,29 @@ def fetch_sentry_issues(cutoff: datetime) -> list[dict]:
8185
"""Return all unresolved Sentry issues first seen after cutoff."""
8286
url = f"{SENTRY_API}/projects/{SENTRY_ORG}/{SENTRY_PROJECT}/issues/"
8387
params = {"query": "is:unresolved", "limit": 100, "sort": "date"}
84-
resp = requests.get(url, headers=SENTRY_HEADERS, params=params)
88+
resp = requests.get(url, headers=SENTRY_HEADERS, params=params, timeout=30)
8589
if resp.status_code != 200:
8690
print(f"ERROR: Sentry API returned {resp.status_code}: {resp.text}", file=sys.stderr)
8791
sys.exit(1)
8892

89-
issues = resp.json()
90-
new_issues = []
91-
for issue in issues:
92-
first_seen = datetime.fromisoformat(issue["firstSeen"].replace("Z", "+00:00"))
93-
if first_seen >= cutoff:
94-
new_issues.append(issue)
95-
return new_issues
93+
# Python 3.11+ fromisoformat handles Z natively; we target 3.12 in the workflow
94+
return [
95+
issue for issue in resp.json()
96+
if datetime.fromisoformat(issue["firstSeen"]) >= cutoff
97+
]
9698

9799

98100
def github_issue_exists(sentry_id: str) -> bool:
99-
"""Return True if a GitHub issue with this Sentry ID already exists."""
101+
"""Return True if a GitHub issue with this Sentry ID already exists.
102+
103+
Raises SystemExit on API errors to prevent silently creating duplicates.
104+
"""
100105
url = f"{GITHUB_API}/search/issues"
101106
query = f'repo:{GITHUB_REPO} label:sentry in:body "SENTRY_ID:{sentry_id}"'
102-
resp = requests.get(url, headers=GITHUB_HEADERS, params={"q": query, "per_page": 1})
107+
resp = requests.get(url, headers=GITHUB_HEADERS, params={"q": query, "per_page": 1}, timeout=30)
103108
if resp.status_code != 200:
104-
print(f" Warning: GitHub search returned {resp.status_code}: {resp.text}")
105-
return False
109+
print(f"ERROR: GitHub search failed ({resp.status_code}): {resp.text}", file=sys.stderr)
110+
sys.exit(1)
106111
return resp.json().get("total_count", 0) > 0
107112

108113

@@ -156,6 +161,7 @@ def create_github_issue(issue: dict) -> None:
156161
url,
157162
headers=GITHUB_HEADERS,
158163
json={"title": title, "body": body, "labels": labels},
164+
timeout=30,
159165
)
160166
if resp.status_code == 201:
161167
data = resp.json()

.github/workflows/sentry-to-github.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
env:
3030
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
3131
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
32-
SENTRY_PROJECT: gosqlx-website
32+
SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}
3333
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3434
GITHUB_REPO: ${{ github.repository }}
3535
POLLING_MINUTES: '31'

0 commit comments

Comments
 (0)