id: stackoverflow-unanswered-question-sla-sentinel
namespace: company.support
description: |
Monitor unanswered Stack Overflow questions past an answer SLA, create assigned
GitHub tickets with explicit opt-in, and remember ticketed question IDs in the KV
Store to prevent repeat alerts.
concurrency:
limit: 1
behavior: QUEUE
triggers:
- id: hourly_sla_check
type: io.kestra.plugin.core.trigger.Schedule
description: "Runs once an hour - unanswered-question SLAs are measured in
hours, so hourly resolution is enough to catch a breach shortly after it
crosses the threshold. Shipped disabled; enable it once a manual run has
been verified against your tag."
cron: "0 * * * *"
disabled: true
inputs:
so_tag: "kestra"
sla_hours: 24
max_questions: 50
ticket_repository: "your-org/support"
ticket_owner: "support-owner"
create_tickets: false
inputs:
- id: so_tag
type: STRING
displayName: "Stack Overflow Tag"
defaults: "kestra"
description: "The Stack Overflow tag to watch, passed to the Stack Exchange
API's `tagged` filter. The API matches a single tag per request."
- id: sla_hours
type: INT
displayName: "Answer SLA (hours)"
defaults: 24
description: "A question with no answers older than this many hours is reported
as an SLA breach."
- id: max_questions
type: INT
displayName: "Max Questions per Run"
defaults: 50
description: "How many of the newest questions to check each run (Stack Exchange
API `pagesize`, capped at 100 by the API itself)."
- id: ticket_repository
type: STRING
defaults: your-org/support
description: GitHub repository receiving SLA tickets (owner/repo).
- id: ticket_owner
type: STRING
defaults: support-owner
description: GitHub login to assign. Must have access to the ticket repository.
- id: create_tickets
type: BOOL
defaults: false
description: Explicit opt-in to create assigned GitHub issues. False previews
breaches without marking them alerted.
tasks:
- id: fetch_tagged_questions
type: io.kestra.plugin.core.http.Download
description: "GET the newest questions for the tag from the public Stack
Exchange API (/2.3/questions with site=stackoverflow, sorted by creation
date). The endpoint needs no API key for anonymous quota (300
requests/day), and the response is stored in internal storage as JSON
rather than inlined, so question titles or bodies containing template-like
braces can never break expression rendering downstream."
uri: "https://api.stackexchange.com/2.3/questions?site=stackoverflow&tagged={{
inputs.so_tag }}&pagesize={{ inputs.max_questions
}}&sort=creation&order=desc"
- id: score_sla_breaches
type: io.kestra.plugin.scripts.python.Script
description: "Score every fetched question in a dependency-free Python script: a
question breaches the SLA when it is still unanswered (`is_answered` false
and `answer_count` zero, per the Stack Exchange API's own fields) and its
age exceeds `sla_hours`. Emits `breach_count`, `checked_count`,
`has_breaches`, and a rendered `digest_markdown` as task outputs, and
writes the breach records to `breaches.json` as single-line JSON - the
serdes JSON tasks parse files line by line, so pretty-printed multi-line
JSON must not be written here."
containerImage: python:3.11-slim
inputFiles:
"questions.json": "{{ outputs.fetch_tagged_questions.uri }}"
env:
SO_TAG: "{{ inputs.so_tag }}"
outputFiles:
- "breaches.json"
script: |
import json
import os
from datetime import datetime, timezone
with open("questions.json") as f:
response = json.load(f)
items = response.get("items", [])
now = datetime.now(timezone.utc)
sla_seconds = {{ inputs.sla_hours }} * 3600
breaches = []
for q in items:
if q.get("is_answered") or q.get("answer_count", 0) > 0:
continue
age = now.timestamp() - q["creation_date"]
if age > sla_seconds:
breaches.append({
"question_id": q["question_id"],
"title": q["title"],
"link": q["link"],
"age_hours": round(age / 3600, 1),
"score": q.get("score", 0),
"view_count": q.get("view_count", 0),
"tags": q.get("tags", []),
})
lines = [
"# Unanswered Stack Overflow questions past SLA",
"",
"Tag: `" + os.environ["SO_TAG"] + "` - SLA: {{ inputs.sla_hours }}h",
"",
]
if breaches:
for b in breaches:
lines.append("- [{title}]({link}) - {age}h old, score {score}, {views} views".format(
title=b["title"], link=b["link"], age=b["age_hours"],
score=b["score"], views=b["view_count"]))
else:
lines.append("All unanswered questions are within the SLA.")
digest = "\n".join(lines)
with open("breaches.json", "w") as f:
json.dump(breaches, f)
print('::{"outputs":{"breach_count":' + str(len(breaches))
+ ',"breaches":' + json.dumps(breaches)
+ ',"checked_count":' + str(len(items))
+ ',"has_breaches":"' + str(bool(breaches)).lower()
+ '","digest_markdown":' + json.dumps(digest) + '}}::')
- id: normalize_breach_records
type: io.kestra.plugin.serdes.json.JsonToIon
description: "Convert the breach records to Kestra's internal ION format so
downstream tasks (file conversions, warehouse loads, alerts built by
whoever adopts this blueprint) can consume them as typed rows."
from: "{{ outputs.score_sla_breaches.outputFiles['breaches.json'] }}"
- id: escalate_new_breaches
type: io.kestra.plugin.core.flow.Loop
values: "{{ outputs.score_sla_breaches.vars.breaches }}"
concurrencyLimit: 1
tasks:
- id: only_unalerted
type: io.kestra.plugin.core.flow.If
condition: >-
{{ kv('so_sla_' ~ (inputs.ticket_repository | replace({'/': '_'})) ~
'_' ~ (item.value | jq('.question_id') | first), errorOnMissing=false)
is null }}
then:
- id: ticket_opt_in
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.create_tickets }}"
then:
- id: create_assigned_ticket
type: io.kestra.plugin.core.http.Request
uri: "https://api.github.com/repos/{{ inputs.ticket_repository }}/issues"
method: POST
contentType: application/json
headers:
Authorization: "Bearer {{ secret('GITHUB_TOKEN') }}"
Accept: application/vnd.github+json
X-GitHub-Api-Version: "2022-11-28"
body: >-
{{ {'title': '[Stack Overflow SLA] ' ~ (item.value |
jq('.title') | first), 'body': 'Unanswered question past the
answer SLA: ' ~ (item.value | jq('.link') | first) ~ ' - Age:
' ~ (item.value | jq('.age_hours') | first) ~ ' hours. Please
triage and answer the question.', 'assignees':
[inputs.ticket_owner]} | toJson }}
- id: verify_assignment
type: io.kestra.plugin.core.flow.If
condition: >-
{{ outputs.create_assigned_ticket.code == 201 and
(outputs.create_assigned_ticket.body |
jq('.assignees[].login')) contains inputs.ticket_owner }}
then:
- id: remember_alerted_question
type: io.kestra.plugin.core.kv.Set
key: >-
{{ 'so_sla_' ~ (inputs.ticket_repository | replace({'/':
'_'})) ~ '_' ~ (item.value | jq('.question_id') | first)
}}
value: "{{ outputs.create_assigned_ticket.body }}"
description: Record only after GitHub confirms issue creation and the requested
assignee.
else:
- id: assignment_not_confirmed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Ticket creation or assignment was not confirmed. Inspect the
GitHub issue and reconcile KV before retrying."
else:
- id: preview_ticket
type: io.kestra.plugin.core.log.Log
message: >-
Would assign question {{ item.value | jq('.question_id') |
first }} to {{ inputs.ticket_owner }} in {{
inputs.ticket_repository }}. Set create_tickets=true after
configuring GITHUB_TOKEN.
- id: report
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.create_tickets }}"
then:
- id: report_escalation_complete
type: io.kestra.plugin.core.log.Log
message: >-
Checked {{ outputs.score_sla_breaches.vars.checked_count }} questions:
{{ outputs.score_sla_breaches.vars.breach_count }} current breaches
processed. Only previously unticketed questions were escalated;
details are in the loop executions and KV Store.
else:
- id: publish_preview_digest
type: io.kestra.plugin.core.log.Log
message: "{{ outputs.score_sla_breaches.vars.digest_markdown }}"