Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 15cc89d

Browse files
authored
feat(ticket): update ticket metadata on change of incident type (#5575)
1 parent ebf54c9 commit 15cc89d

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/dispatch/incident/service.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from dispatch.project import service as project_service
3030
from dispatch.tag import service as tag_service
3131
from dispatch.term import service as term_service
32+
from dispatch.ticket import flows as ticket_flows
3233

3334
from .enums import IncidentStatus
3435
from .models import Incident, IncidentCreate, IncidentRead, IncidentUpdate
@@ -384,6 +385,16 @@ def update(*, db_session: Session, incident: Incident, incident_in: IncidentUpda
384385
incident_cost_service.update_incident_response_cost(
385386
incident_id=incident.id, db_session=db_session
386387
)
388+
# if the new incident type has plugin metadata and the
389+
# project key of the ticket is the same, also update the ticket with the new metadata
390+
if incident_type.plugin_metadata:
391+
ticket_flows.update_incident_ticket_metadata(
392+
db_session=db_session,
393+
ticket_id=incident.ticket.resource_id,
394+
project_id=incident.project.id,
395+
incident_id=incident.id,
396+
incident_type=incident_type,
397+
)
387398

388399
update_data = incident_in.dict(
389400
skip_defaults=True,

src/dispatch/plugins/dispatch_core/plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ def create_case_ticket(
249249
"resource_type": "dispatch-internal-ticket",
250250
}
251251

252+
def update_metadata(
253+
self,
254+
ticket_id: str,
255+
metadata: dict,
256+
):
257+
"""Updates the metadata of a Dispatch ticket."""
258+
return
259+
252260
def update_case_ticket(
253261
self,
254262
ticket_id: str,

src/dispatch/plugins/dispatch_jira/plugin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ def create(
315315
reporter = get_user_field(client, self.configuration, reporter_email)
316316

317317
project_id, issue_type_name = process_plugin_metadata(incident_type_plugin_metadata)
318+
other_fields = create_dict_from_plugin_metadata(incident_type_plugin_metadata)
318319

319320
if not project_id:
320321
project_id = self.configuration.default_project_id
@@ -335,6 +336,7 @@ def create(
335336
"assignee": assignee,
336337
"reporter": reporter,
337338
"summary": title,
339+
**other_fields,
338340
}
339341

340342
ticket = create(self.configuration, client, issue_fields)
@@ -401,6 +403,31 @@ def update(
401403

402404
return update(self.configuration, client, issue, issue_fields, status)
403405

406+
def update_metadata(
407+
self,
408+
ticket_id: str,
409+
metadata: dict,
410+
):
411+
"""Updates the metadata of a Jira issue."""
412+
client = create_client(self.configuration)
413+
issue = client.issue(ticket_id)
414+
415+
# check to make sure project id matches metadata
416+
project_id, issue_type_name = process_plugin_metadata(metadata)
417+
if project_id and issue.fields.project.key != project_id:
418+
log.warning(
419+
f"Project key mismatch between Jira issue {issue.fields.project.key} and metadata {project_id} for ticket {ticket_id}"
420+
)
421+
return
422+
other_fields = create_dict_from_plugin_metadata(metadata)
423+
issue_fields = {
424+
**other_fields,
425+
}
426+
if issue_type_name:
427+
issue_fields["issuetype"] = {"name": issue_type_name}
428+
429+
issue.update(fields=issue_fields)
430+
404431
def create_case_ticket(
405432
self,
406433
case_id: int,

src/dispatch/ticket/flows.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dispatch.event import service as event_service
1010
from dispatch.incident import service as incident_service
1111
from dispatch.incident.models import Incident
12+
from dispatch.incident.type.models import IncidentType
1213
from dispatch.incident.type import service as incident_type_service
1314
from dispatch.participant import service as participant_service
1415
from dispatch.plugin import service as plugin_service
@@ -334,3 +335,38 @@ def create_task_ticket(task: Task, db_session: Session):
334335
db_session.commit()
335336

336337
return external_ticket
338+
339+
340+
def update_incident_ticket_metadata(
341+
db_session: Session,
342+
ticket_id: str,
343+
project_id: int,
344+
incident_id: int,
345+
incident_type: IncidentType,
346+
):
347+
"""
348+
Updates the metadata of an incident ticket.
349+
"""
350+
plugin = plugin_service.get_active_instance(
351+
db_session=db_session, project_id=project_id, plugin_type="ticket"
352+
)
353+
if not plugin:
354+
log.warning("Incident ticket metadata not updated. No ticket plugin enabled.")
355+
return
356+
357+
# we update the external incident ticket
358+
try:
359+
plugin.instance.update_metadata(
360+
ticket_id=ticket_id,
361+
metadata=incident_type.get_meta(plugin.plugin.slug),
362+
)
363+
except Exception as e:
364+
log.exception(e)
365+
return
366+
367+
event_service.log_incident_event(
368+
db_session=db_session,
369+
source=plugin.plugin.title,
370+
description="Incident ticket metadata updated",
371+
incident_id=incident_id,
372+
)

0 commit comments

Comments
 (0)