forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentry-unresolved-issues-by-project.template.py
executable file
·106 lines (83 loc) · 2.97 KB
/
sentry-unresolved-issues-by-project.template.py
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
#!/usr/bin/env python3
# How to use this script?
# It's a template which needs further setup. Duplicate the file,
# remove `.template.` from the filename and set an API token as
# well as the Sentry organization.
#
# API: https://docs.sentry.io/api/events/list-a-projects-issues/
# Parameters
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Unresolved Issues By Project
# @raycast.mode fullOutput
# Conditional parameters:
# @raycast.refreshTime 1h
# Optional parameters:
# @raycast.packageName Sentry
# @raycast.icon images/sentry.png
# @raycast.iconDark images/sentry-dark.png
# @raycast.argument1 { "type": "text", "placeholder": "Project" }
# Documentation:
# @raycast.author Phil Salant
# @raycast.authorURL https://github.com/PSalant726
# @raycast.author Thomas Paul Mann
# @raycast.authorURL https://github.com/thomaspaulmann
# @raycast.description Show unresolved issues in the last 24 hours (by project) from Sentry.
#########################
##### Configuration #####
#########################
# API token with `project:read` scope (https://sentry.io/settings/account/api/auth-tokens/)
API_TOKEN = ""
# Slug of organization the issues belong to
ORGANIZATION = ""
if not API_TOKEN:
print(error("No API token provided"))
exit(1)
if not ORGANIZATION:
print(error("No Sentry organization provided"))
exit(1)
# Main program
import json, sys, urllib.request
from datetime import datetime as dt
colors = {
'ok': '\033[92m',
'error': '\033[91m',
'end': '\033[0m',
'warn': '\033[93m',
}
def error(message):
return f"{colors['error']}{message}{colors['end']}"
def ok(message):
return f"{colors['ok']}{message}{colors['end']}"
def warn(message):
return f"{colors['warn']}{message}{colors['end']}"
project = sys.argv[1]
if not project:
print(error("No Sentry project provided"))
exit(1)
request = urllib.request.Request(
method="GET",
url=f"https://sentry.io/api/0/projects/{ORGANIZATION}/{project}/issues/?statsPeriod=24h&query=is:unresolved",
headers={ "Authorization": f"Bearer {API_TOKEN}" }
)
try:
response = urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
print(f"{error('Failed to get unresolved issues from Sentry:')} {e.code} {e.reason}")
exit(1)
except urllib.error.URLError as e:
print(f"{error('Failed to reach Sentry:')} {e.reason}")
exit(1)
else:
unresolved_issues = json.loads(response.read().decode("utf-8"))
unresolved_issues_count = len(unresolved_issues)
if unresolved_issues_count == 0:
print(ok("No unresolved issues in the last 24 hours."))
else:
issue_text = "issue" if unresolved_issues_count == 1 else "issues"
print(error(f"{unresolved_issues_count} unresolved {issue_text} in the last 24 hours:\n"))
for i, issue in enumerate(unresolved_issues, 1):
last_seen = dt.strptime(issue['lastSeen'], "%Y-%m-%dT%H:%M:%S.%fZ").strftime('%b %d, %Y at %I:%M %p')
print(f"{i}. {warn(issue['title'])}")
print(f" Last seen {last_seen}.")
print(f" {issue['permalink']}\n")