Repository navigation
Expand file tree
/
Copy pathplugin.py
More file actions
233 lines (192 loc) · 6.67 KB
/
Copy pathplugin.py
File metadata and controls
233 lines (192 loc) · 6.67 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
"""
.. module: dispatch.plugins.dispatch_jira.plugin
:platform: Unix
:copyright: (c) 2019 by Netflix Inc., see AUTHORS for more
:license: Apache, see LICENSE for more details.
"""
from typing import Any
from jinja2 import Template
from jira import JIRA, User
from dispatch.decorators import apply, counter, timer
from dispatch.plugins import dispatch_jira as jira_plugin
from dispatch.plugins.bases import TicketPlugin
from .config import (
JIRA_API_URL,
JIRA_BROWSER_URL,
JIRA_HOSTING_TYPE,
JIRA_ISSUE_TYPE_NAME,
JIRA_PASSWORD,
JIRA_PROJECT_ID,
JIRA_USERNAME,
)
ISSUE_SUMMARY_TEMPLATE = """
{color:red}*Confidential Information - For Internal Use Only*{color}
*Incident Details*
Description: {{description}}
Type: {{incident_type}}
Priority: {{priority}}
Cost: ${{cost}}
*Incident Resources*
[Conversation|{{conversation_weblink}}]
[Investigation Document|{{document_weblink}}]
[Storage|{{storage_weblink}}]
[Conference|{{conference_weblink}}]
Incident Commander: [~{{commander_username}}]
"""
def get_email_username(email: str) -> str:
"""Returns username part of email, if valid email is provided."""
if "@" in email:
return email.split("@")[0]
return email
def get_user_field(client: JIRA, user_email) -> dict:
"""Returns correct Jira user field based on Jira hosting type."""
if JIRA_HOSTING_TYPE == "Server":
user = client.search_users(user_email, maxResults=1)[0]
return {"name": user.name}
if JIRA_HOSTING_TYPE == "Cloud":
username = get_email_username(user_email)
user = next(
client._fetch_pages(
User,
None,
"user/search",
startAt=0,
maxResults=1,
params={"query": username},
)
)
return {"id": user.accountId}
def process_incident_type_plugin_metadata(plugin_metadata: dict):
"""
Processes the given incident type plugin metadata.
"""
project_id = JIRA_PROJECT_ID
issue_type_name = JIRA_ISSUE_TYPE_NAME
if plugin_metadata:
for key_value in plugin_metadata["metadata"]:
if key_value["key"] == "project_id":
project_id = key_value["value"]
if key_value["key"] == "issue_type_name":
issue_type_name = key_value["value"]
return project_id, issue_type_name
def create_issue_fields(
title: str,
description: str,
incident_type: str,
priority: str,
assignee: dict,
reporter: dict,
commander_username: str,
conversation_weblink: str,
document_weblink: str,
storage_weblink: str,
conference_weblink: str,
cost: float,
):
"""Creates Jira issue fields."""
issue_fields = {}
issue_fields.update({"summary": title})
issue_fields.update({"assignee": assignee})
issue_fields.update({"reporter": reporter})
description = Template(ISSUE_SUMMARY_TEMPLATE).render(
description=description,
incident_type=incident_type,
priority=priority,
cost=cost,
commander_username=commander_username,
document_weblink=document_weblink,
conference_weblink=conference_weblink,
conversation_weblink=conversation_weblink,
storage_weblink=storage_weblink,
)
issue_fields.update({"description": description})
return issue_fields
def create(client: Any, issue_fields: dict) -> dict:
"""Creates a Jira issue."""
issue = client.create_issue(fields=issue_fields)
return {"resource_id": issue.key, "weblink": f"{JIRA_BROWSER_URL}/browse/{issue.key}"}
def update(client: Any, issue: Any, issue_fields: dict, transition: str = None) -> dict:
"""Updates a Jira issue."""
data = {"resource_id": issue.key, "link": f"{JIRA_BROWSER_URL}/browse/{issue.key}"}
if issue_fields:
issue.update(fields=issue_fields)
if transition:
transitions = client.transitions(issue)
for t in transitions:
if t["name"].lower() == transition.lower():
client.transition_issue(issue, t["id"])
break
return data
@apply(counter, exclude=["__init__"])
@apply(timer, exclude=["__init__"])
class JiraTicketPlugin(TicketPlugin):
title = "Jira Plugin - Ticket Management"
slug = "jira-ticket"
description = "Uses Jira to help manage external tickets."
version = jira_plugin.__version__
author = "Netflix"
author_url = "https://github.com/netflix/dispatch.git"
_schema = None
def create(
self,
incident_id: int,
title: str,
incident_type: str,
incident_priority: str,
commander_email: str,
reporter_email: str,
incident_type_plugin_metadata: dict = {},
):
"""Creates a Jira issue."""
client = JIRA(str(JIRA_API_URL), basic_auth=(JIRA_USERNAME, str(JIRA_PASSWORD)))
assignee = get_user_field(client, commander_email)
reporter = get_user_field(client, reporter_email)
project_id, issue_type_name = process_incident_type_plugin_metadata(
incident_type_plugin_metadata
)
issue_fields = {
"project": {"id": project_id},
"issuetype": {"name": issue_type_name},
"assignee": assignee,
"reporter": reporter,
"summary": title,
}
return create(client, issue_fields)
def update(
self,
ticket_id: str,
title: str,
description: str,
incident_type: str,
priority: str,
status: str,
commander_email: str,
reporter_email: str,
conversation_weblink: str,
document_weblink: str,
storage_weblink: str,
conference_weblink: str,
cost: float,
incident_type_plugin_metadata: dict = {},
):
"""Updates Jira issue fields."""
client = JIRA(str(JIRA_API_URL), basic_auth=(JIRA_USERNAME, str(JIRA_PASSWORD)))
assignee = get_user_field(client, commander_email)
reporter = get_user_field(client, reporter_email)
commander_username = get_email_username(commander_email)
issue = client.issue(ticket_id)
issue_fields = create_issue_fields(
title=title,
description=description,
incident_type=incident_type,
priority=priority,
assignee=assignee,
reporter=reporter,
commander_username=commander_username,
conversation_weblink=conversation_weblink,
document_weblink=document_weblink,
storage_weblink=storage_weblink,
conference_weblink=conference_weblink,
cost=cost,
)
return update(client, issue, issue_fields, status)