forked from getsentry/integration-platform-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue_handler.py
139 lines (121 loc) · 4.75 KB
/
issue_handler.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
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
from typing import Any, Mapping
from src import app
from src.models import Item, SentryInstallation, User
from src.models.item import ItemColumn
from src.database import db_session
from flask import Response
def handle_assigned(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
):
# Find or create an item to associate with the Sentry issue
item, item_created = get_or_create_item(sentry_installation, issue_data)
app.logger.info(
f"{'Created' if item_created else 'Found'} linked item from Sentry issue"
)
item.column = ItemColumn.Doing
# Find or create a user to associate with the item
# Note: The assignee in Sentry might be a team, which you could handle here as well
assignee_data = issue_data.get("assignedTo", {})
if assignee_data.get("type") == "user":
user, user_created = get_or_create_user(sentry_installation, assignee_data)
item.assignee_id = user.id
app.logger.info(
f"Assigned to {'new' if user_created else 'existing'} user: {user.username}"
)
db_session.commit()
def handle_created(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
):
# Create an item to associate with the Sentry Issue
item = Item(**get_item_defaults_from_sentry(sentry_installation, issue_data))
db_session.add(item)
db_session.commit()
app.logger.info("Created linked item from Sentry issue")
def handle_ignored(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
):
# Find or create an item to associate with the Sentry Issue
item, item_created = get_or_create_item(sentry_installation, issue_data)
app.logger.info(
f"{'Created' if item_created else 'Found'} linked item from Sentry issue"
)
# Mark the item as ignored
item.is_ignored = True
db_session.commit()
app.logger.info("Marked item as ignored")
def handle_resolved(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
):
# Find or create an item to associate with the Sentry Issue
item, item_created = get_or_create_item(sentry_installation, issue_data)
app.logger.info(
f"{'Created' if item_created else 'Found'} linked item from Sentry issue"
)
# Update the item's column to DONE
item.column = ItemColumn.Done
db_session.commit()
app.logger.info(f"Updated item's column to {ItemColumn.Done.value}")
def issue_handler(
action: str, sentry_installation: SentryInstallation, data: Mapping[str, Any]
) -> Response:
issue_data = data.get("issue")
if action == "assigned":
handle_assigned(sentry_installation, issue_data)
return Response("", 202)
elif action == "created":
handle_created(sentry_installation, issue_data)
return Response("", 201)
elif action == "ignored":
handle_ignored(sentry_installation, issue_data)
return Response("", 202)
elif action == "resolved":
handle_resolved(sentry_installation, issue_data)
return Response("", 202)
else:
app.logger.info(f"Unhandled Sentry issue action: {action}")
return Response("", 400)
def get_item_defaults_from_sentry(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
) -> Mapping[str, Any]:
return {
"organization_id": sentry_installation.organization_id,
"title": issue_data.get("title"),
"description": f"{issue_data.get('shortId')} - {issue_data.get('culprit')}",
"column": ItemColumn.Done
if issue_data.get("status") == "resolved"
else ItemColumn.Todo,
"is_ignored": issue_data.get("status") == "ignored",
"sentry_id": issue_data.get("id"),
}
def get_or_create_item(
sentry_installation: SentryInstallation, issue_data: Mapping[str, Any]
):
item = Item.query.filter(
Item.sentry_id == issue_data.get("id"),
Item.organization_id == sentry_installation.organization_id,
).first()
if item:
return item, False
else:
item = Item(**get_item_defaults_from_sentry(sentry_installation, issue_data))
db_session.add(item)
db_session.commit()
return item, True
def get_or_create_user(
sentry_installation: SentryInstallation, assignee_data: Mapping[str, Any]
):
user = User.query.filter(
User.username == assignee_data.get("email"),
).first()
if user:
return user, False
else:
user = User(
name=assignee_data.get("name"),
username=assignee_data.get("email"),
organization_id=sentry_installation.organization_id,
avatar=f"https://ui-avatars.com/api/?name={assignee_data.get('name')}&background=random",
)
db_session.add(user)
db_session.commit()
return user, True