-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsentry_issues.py
More file actions
258 lines (204 loc) · 9.07 KB
/
sentry_issues.py
File metadata and controls
258 lines (204 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
"""
Sentry Issues -> GitHub Issues sync script.
Polls the Sentry API for newly created issues within the polling window
and creates corresponding GitHub issues, skipping duplicates.
"""
import os
import sys
import time
from datetime import datetime, timezone, timedelta
from typing import Any
import requests
# ---------------------------------------------------------------------------
# Configuration from environment
# ---------------------------------------------------------------------------
SENTRY_AUTH_TOKEN = os.environ["SENTRY_AUTH_TOKEN"]
SENTRY_ORG = os.environ["SENTRY_ORG"]
SENTRY_PROJECT = os.environ["SENTRY_PROJECT"]
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
GITHUB_REPO = os.environ["GITHUB_REPO"]
POLLING_MINUTES = int(os.environ.get("POLLING_MINUTES", "31"))
SENTRY_API = "https://sentry.io/api/0"
GITHUB_API = "https://api.github.com"
SENTRY_HEADERS = {"Authorization": f"Bearer {SENTRY_AUTH_TOKEN}"}
GITHUB_HEADERS = {
"Authorization": f"Bearer {GITHUB_TOKEN}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
# Sentry level -> (severity label, include "bug" label)
# fatal/error are genuine bugs; warning/info/debug are not necessarily bugs.
LEVEL_TO_SEVERITY: dict[str, tuple[str, bool]] = {
"fatal": ("severity: critical", True),
"error": ("severity: high", True),
"warning": ("severity: medium", False),
"info": ("severity: low", False),
"debug": ("severity: low", False),
}
# Transient HTTP status codes that are safe to retry
RETRYABLE_STATUSES = {429, 500, 502, 503, 504}
LABELS_TO_BOOTSTRAP = [
{"name": "sentry", "color": "6f42c1", "description": "Automatically created from Sentry error monitoring"},
{"name": "severity: critical", "color": "b60205", "description": "Fatal errors — immediate attention required"},
{"name": "severity: high", "color": "e11d48", "description": "Errors affecting users"},
{"name": "severity: medium", "color": "f59e0b", "description": "Warnings with user impact"},
{"name": "severity: low", "color": "6b7280", "description": "Informational or debug-level issues"},
]
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def request_with_retry(
method: str,
url: str,
*,
max_attempts: int = 3,
**kwargs: Any,
) -> requests.Response:
"""Perform an HTTP request, retrying on transient errors with exponential backoff."""
kwargs.setdefault("timeout", 30)
for attempt in range(1, max_attempts + 1):
resp = requests.request(method, url, **kwargs)
if resp.status_code not in RETRYABLE_STATUSES:
return resp
wait = 2 ** attempt # 2s, 4s, 8s
print(f" Transient {resp.status_code} on attempt {attempt}/{max_attempts}, retrying in {wait}s...")
if attempt < max_attempts:
time.sleep(wait)
return resp # Return last response after exhausting retries
def ensure_label(name: str, color: str, description: str) -> None:
"""Create a GitHub label if it does not already exist."""
url = f"{GITHUB_API}/repos/{GITHUB_REPO}/labels"
resp = request_with_retry(
"POST", url,
headers=GITHUB_HEADERS,
json={"name": name, "color": color, "description": description},
)
if resp.status_code == 201:
print(f" Created label: {name}")
elif resp.status_code == 422:
data = resp.json()
errors = data.get("errors", [])
# Only swallow "already_exists" — surface other validation errors
if not all(e.get("code") == "already_exists" for e in errors):
print(f" Warning: label creation validation error for '{name}': {resp.text}")
else:
print(f" Warning: unexpected status {resp.status_code} creating label '{name}': {resp.text}")
def bootstrap_labels() -> None:
for label in LABELS_TO_BOOTSTRAP:
ensure_label(**label)
def fetch_sentry_issues(cutoff: datetime) -> list[dict]:
"""Return all unresolved Sentry issues first seen after cutoff.
Follows Sentry's Link-header pagination so no issues are missed even when
there are more than 100 unresolved issues in the project.
Python 3.11+ fromisoformat handles the trailing 'Z' natively (no replace needed).
"""
url: str | None = f"{SENTRY_API}/projects/{SENTRY_ORG}/{SENTRY_PROJECT}/issues/"
params: dict | None = {"query": "is:unresolved", "limit": 100, "sort": "date"}
new_issues: list[dict] = []
while url:
resp = request_with_retry("GET", url, headers=SENTRY_HEADERS, params=params)
if resp.status_code != 200:
print(f"ERROR: Sentry API returned {resp.status_code}: {resp.text}", file=sys.stderr)
sys.exit(1)
page = resp.json()
for issue in page:
first_seen = datetime.fromisoformat(issue["firstSeen"])
if first_seen >= cutoff:
new_issues.append(issue)
else:
# Issues are sorted by date desc; once we pass the cutoff, stop paginating.
return new_issues
# Follow next-page link if present (format: <url>; rel="next"; results="true")
link_header = resp.headers.get("Link", "")
url = None
params = None
for part in link_header.split(","):
if 'rel="next"' in part and 'results="true"' in part:
url = part.split(";")[0].strip().strip("<>")
break
return new_issues
def github_issue_exists(sentry_id: str) -> bool:
"""Return True if a GitHub issue with this Sentry ID already exists.
Raises SystemExit on API errors to prevent silently creating duplicates.
"""
url = f"{GITHUB_API}/search/issues"
query = f'repo:{GITHUB_REPO} label:sentry in:body "SENTRY_ID:{sentry_id}"'
resp = request_with_retry("GET", url, headers=GITHUB_HEADERS, params={"q": query, "per_page": 1})
if resp.status_code != 200:
print(f"ERROR: GitHub search failed ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
return resp.json().get("total_count", 0) > 0
def build_issue_body(issue: dict) -> str:
sentry_id = issue["id"]
level = issue.get("level", "error")
count = issue.get("count", "?")
user_count = issue.get("userCount", "?")
first_seen = issue.get("firstSeen", "?")
last_seen = issue.get("lastSeen", "?")
culprit = issue.get("culprit", "?")
permalink = issue.get("permalink", f"https://{SENTRY_ORG}.sentry.io/issues/{sentry_id}/")
return f"""\
## Sentry Issue: {issue['title']}
**Sentry ID:** `{sentry_id}`
**Culprit:** `{culprit}`
**Level:** {level}
---
| Field | Value |
|-------|-------|
| First Seen | {first_seen} |
| Last Seen | {last_seen} |
| Occurrences | {count} |
| Affected Users | {user_count} |
**Sentry URL:** {permalink}
---
> This issue was automatically created by the Sentry monitoring workflow.
> To resolve, fix the underlying error and mark the Sentry issue as resolved.
<!-- SENTRY_ID:{sentry_id} -->
"""
def create_github_issue(issue: dict) -> None:
level = issue.get("level", "error")
severity_label, is_bug = LEVEL_TO_SEVERITY.get(level, ("severity: high", True))
# Only add "bug" for fatal/error levels — warnings and below are not necessarily bugs
labels = ["sentry", severity_label] + (["bug"] if is_bug else [])
title = f"[Sentry] {issue['title']}"
body = build_issue_body(issue)
url = f"{GITHUB_API}/repos/{GITHUB_REPO}/issues"
resp = request_with_retry(
"POST", url,
headers=GITHUB_HEADERS,
json={"title": title, "body": body, "labels": labels},
)
if resp.status_code == 201:
data = resp.json()
print(f" Created GitHub issue #{data['number']}: {data['html_url']}")
else:
print(f"ERROR: GitHub issue creation failed ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
cutoff = datetime.now(timezone.utc) - timedelta(minutes=POLLING_MINUTES)
print(f"Polling for Sentry issues first seen after {cutoff.isoformat()}")
print("Bootstrapping GitHub labels...")
bootstrap_labels()
print(f"Fetching unresolved Sentry issues for {SENTRY_ORG}/{SENTRY_PROJECT}...")
new_issues = fetch_sentry_issues(cutoff)
print(f"Found {len(new_issues)} new issue(s) in the last {POLLING_MINUTES} minutes.")
if not new_issues:
print("Nothing to do.")
return
for issue in new_issues:
sentry_id = issue["id"]
title = issue["title"]
print(f"\nProcessing Sentry issue {sentry_id}: {title[:80]}")
# Rate-limit guard for GitHub search API (30 req/min authenticated)
time.sleep(2)
if github_issue_exists(sentry_id):
print(f" Skipping — GitHub issue already exists for SENTRY_ID:{sentry_id}")
continue
create_github_issue(issue)
print("\nDone.")
if __name__ == "__main__":
main()