Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/firetower/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class LinearConfig:
"{% else %}all {{ total_action_items }} action "
"item{% if total_action_items != 1 %}s{% endif %} are complete.{% endif %}"
)
parent_status_comment_started: str = (
"Firetower set this issue to **Started**. "
"Incident {{ incident.incident_number }} is {{ incident.status }}. "
"{% if total_action_items == 0 %}There are no action items."
"{% else %}{{ completed_action_items }} of {{ total_action_items }} action "
"item{% if total_action_items != 1 %}s{% endif %} complete.{% endif %}"
)


@deserialize
Expand Down
10 changes: 6 additions & 4 deletions src/firetower/incidents/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,13 @@ def _sync_linear_assignee(incident: Incident) -> None:


LINEAR_PARENT_DESCRIPTION = (
"Relate action items to this ticket to have them tracked by Firetower. "
"Child issues or other relations (related, blocking, etc.) will all work. "
"Add action items as sub-issues (child issues) of this ticket to have "
"them tracked by Firetower. "
"Do not update title, status or captain here, use Firetower for that.\n\n"
"Firetower will mark this ticket completed once the incident is resolved and "
"all action items are done. "
"Firetower will mark this ticket as completed once the incident is "
"resolved and all action items are done. "
"Firetower will reopen this ticket if the incident is reopened, or if "
"there are still unfinished action items. "
"If you have questions, please reach out to #team-sre."
)

Expand Down
48 changes: 25 additions & 23 deletions src/firetower/incidents/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,31 @@ def _resolve_assignees(
_PARENT_STATUS_TEMPLATE_ENV = Environment(autoescape=False)


def _comment_parent_issue_completed(
def _comment_parent_issue_status_change(
incident: Incident,
linear_service: LinearService,
target_state: str,
statuses: list[str],
) -> None:
if not settings.LINEAR or not incident.linear_parent_issue_id:
return

template_source = settings.LINEAR.get("PARENT_STATUS_COMMENT_COMPLETED", "")
template_key = (
"PARENT_STATUS_COMMENT_COMPLETED"
if target_state == "completed"
else "PARENT_STATUS_COMMENT_STARTED"
)
template_source = settings.LINEAR.get(template_key, "")
if not template_source or not template_source.strip():
return

completed_action_items = sum(
1 for s in statuses if s in {ActionItemStatus.DONE, ActionItemStatus.CANCELED}
)
completed_action_items = sum(1 for s in statuses if s in COMPLETED_STATUSES)
try:
comment = _PARENT_STATUS_TEMPLATE_ENV.from_string(template_source).render(
incident=incident,
total_action_items=len(statuses),
completed_action_items=completed_action_items,
target_state=target_state,
)
except TemplateError:
logger.exception(
Expand Down Expand Up @@ -239,22 +244,31 @@ def _update_parent_issue_status(
not statuses or all(s in COMPLETED_STATUSES for s in statuses)
)

if not all_complete:
return

states = linear_service.get_workflow_states(team_id)
if not states:
return

target_state = "completed" if all_complete else "started"

parent_issue = linear_service.get_issue(incident.linear_parent_issue_id)
if not parent_issue or parent_issue.get("state_type") == "completed":
if not parent_issue:
return

# Never override a manually-cancelled parent issue. Firetower only ever
# drives the parent to "started" or "completed", so a "canceled" state
# reflects a deliberate human decision and must not be reopened to
# "started" (or forced to "completed") on subsequent syncs.
current_state_type = parent_issue.get("state_type")
if current_state_type in ("canceled", target_state):
return

state_id = states.get("completed")
state_id = states.get(target_state)
if state_id and linear_service.update_issue(
incident.linear_parent_issue_id, state_id=state_id
):
_comment_parent_issue_completed(incident, linear_service, statuses)
_comment_parent_issue_status_change(
incident, linear_service, target_state, statuses
)
Comment thread
github-actions[bot] marked this conversation as resolved.


def sync_action_items_from_linear(
Expand Down Expand Up @@ -297,21 +311,9 @@ def sync_action_items_from_linear(
incident.save(update_fields=["action_items_last_synced_at"])
return stats

related = linear_service.get_related_issues(parent_id)
if related is None:
error_msg = f"Failed to fetch related issues for incident {incident.id}"
logger.warning(error_msg)
stats.errors.append(error_msg)
incident.action_items_last_synced_at = timezone.now()
incident.save(update_fields=["action_items_last_synced_at"])
return stats

all_issues: dict[str, dict] = {}
for issue in children:
all_issues[issue["id"]] = issue
for issue in related:
if issue["id"] not in all_issues:
all_issues[issue["id"]] = issue

logger.info(f"Syncing {len(all_issues)} Linear issues to incident {incident.id}")

Expand Down
Loading
Loading