Close the osTicket ticket along with an alert

The Close drawer can also close the alert's osTicket ticket. osTicket's API
cannot, so this writes to its database with a separate, optional login: the
ticket is set to Closed, a note in its thread says who closed it and why, and
the closed event is recorded, in one transaction. Tables and columns are
checked first, only an open ticket is closed, and a ticket closed by somebody
else at the same moment is left alone. Summary tickets are not closed from here.

"Test closing" (Alert tickets settings) tries every write and rolls it back.
If the ticket cannot be closed the alert is still closed and the reason is
said, noted on the incident and audited.

Not run against a real osTicket: tested against a pretend database only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-25 21:27:38 +01:00
1 parent a94899865e
commit 5ae61a1ece
21 files changed
+8405 -23

No files matched your search

+5 -3
View File
@@ -1905,7 +1905,7 @@ export interface AlertItem {
/** On a host's alert: the servers on it whose own alerts are folded under this one. */
affects?: string[];
/** The osTicket ticket opened for it, if one was. */
ticket?: { number: string | null; url: string | null; grouped: boolean; openedAt: string | null } | null;
ticket?: { number: string | null; url: string | null; grouped: boolean; openedAt: string | null; closedAt: string | null; canClose: boolean } | null;
/** Somebody has said they are on it. */
ack?: AlertAck | null;
/** The incident record this alert is part of, while it lasts. */
@@ -2121,7 +2121,7 @@ export interface AlertTicketRunSummary {
export interface AlertTicketOverview {
settings: AlertTicketSettings;
integrations: { id: number; name: string; enabled: boolean; ticketApiReady: boolean }[];
integrations: { id: number; name: string; enabled: boolean; ticketApiReady: boolean; /** A login for closing tickets is set. */ closeReady: boolean }[];
categories: { value: string; label: string }[];
status: { lastRun: AlertTicketRunSummary | null; open: number; pending: number; baseline: number; baselinePending: boolean };
}
@@ -3187,7 +3187,8 @@ export const api = {
ack: (data: AckInput) => request<{ ack: AlertAck }>("/api/alerts/ack", { method: "PUT", body: JSON.stringify(data) }),
unack: (alertId: string) => request<void>(`/api/alerts/ack?alertId=${encodeURIComponent(alertId)}`, { method: "DELETE" }),
snooze: (alertId: string, until: string) => request<{ snooze: { until: string; snoozedBy: string } }>("/api/alerts/snooze", { method: "PUT", body: JSON.stringify({ alertId, until }) }),
close: (alertId: string, note: string | null) => request<{ closed: { by: string; at: string; note: string | null } }>("/api/alerts/close", { method: "PUT", body: JSON.stringify({ alertId, note }) }),
close: (alertId: string, note: string | null, closeTicket = false) =>
request<{ closed: { by: string; at: string; note: string | null }; ticket: { number: string | null; result: "closed" | "already_closed" | "failed"; message: string | null } | null }>("/api/alerts/close", { method: "PUT", body: JSON.stringify({ alertId, note, closeTicket }) }),
reopen: (alertId: string) => request<void>(`/api/alerts/close?alertId=${encodeURIComponent(alertId)}`, { method: "DELETE" }),
unsnooze: (alertId: string) => request<void>(`/api/alerts/snooze?alertId=${encodeURIComponent(alertId)}`, { method: "DELETE" }),
noise: (days: number) => request<NoiseReport>(`/api/alerts/noise?days=${days}`),
@@ -3220,6 +3221,7 @@ export const api = {
get: () => request<AlertTicketOverview>("/api/alert-tickets"),
update: (patch: Partial<AlertTicketSettings>) => request<AlertTicketOverview>("/api/alert-tickets", { method: "PUT", body: JSON.stringify(patch) }),
test: () => request<{ ok: boolean; ticketNumber: string | null }>("/api/alert-tickets/test", { method: "POST" }),
testClose: (integrationId: number) => request<{ ok: boolean; problems: string[]; notes: string[] }>("/api/alert-tickets/test-close", { method: "POST", body: JSON.stringify({ integrationId }) }),
run: () => request<{ summary: AlertTicketRunSummary; status: AlertTicketOverview["status"] }>("/api/alert-tickets/run", { method: "POST" }),
},
ports: {
+28 -2
View File
@@ -12,15 +12,20 @@ import { ErrorNotice } from "./Notice";
*/
export default function CloseDrawer({ alert, onClose, onSaved }: { alert: AlertItem; onClose: () => void; onSaved: () => void }) {
const [note, setNote] = useState("");
const [closeTicket, setCloseTicket] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const ticket = alert.ticket ?? null;
async function close() {
setBusy(true);
setError(null);
try {
await api.alerts.close(alert.id, note.trim() === "" ? null : note.trim());
toast.success("Closed.");
const res = await api.alerts.close(alert.id, note.trim() === "" ? null : note.trim(), closeTicket && !!ticket?.canClose);
if (res.ticket?.result === "closed") toast.success(`Closed, and osTicket ticket #${res.ticket.number} is closed too.`);
else if (res.ticket?.result === "already_closed") toast.success(`Closed. osTicket ticket #${res.ticket.number} was already closed.`);
else if (res.ticket?.result === "failed") toast.error(`The alert is closed, but the ticket was not: ${res.ticket.message ?? "it could not be closed"}`);
else toast.success("Closed.");
onSaved();
onClose();
} catch (err) {
@@ -41,6 +46,27 @@ export default function CloseDrawer({ alert, onClose, onSaved }: { alert: AlertI
Why? (optional)
</label>
<textarea id="close-note" className="form-control mb-3" rows={3} maxLength={500} value={note} onChange={(e) => setNote(e.target.value)} placeholder="Fixed outside the app · accepted · the disk is being replaced on Friday…" autoFocus />
{ticket && ticket.number && (
<div className="mb-3">
{ticket.closedAt ? (
<div className="text-secondary small">osTicket ticket #{ticket.number} has already been closed from here.</div>
) : ticket.canClose ? (
<label className="form-check">
<input type="checkbox" className="form-check-input" checked={closeTicket} onChange={(e) => setCloseTicket(e.target.checked)} />
<span className="form-check-label">
Also close osTicket ticket <strong>#{ticket.number}</strong>
<span className="d-block text-secondary small">It is set to Closed in osTicket, with a note in its thread saying who closed it from here and why (your reason above).</span>
</span>
</label>
) : ticket.grouped ? (
<div className="text-secondary small">Its osTicket ticket #{ticket.number} covers several problems, so it is not closed from here.</div>
) : (
<div className="text-secondary small">
Its osTicket ticket #{ticket.number} stays open: closing tickets from here needs a database login for it (an administrator adds one on the osTicket integration, under Integrations).
</div>
)}
</div>
)}
<div className="d-flex gap-2">
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => void close()}>
{busy ? "Closing…" : "Close the alert"}
+2 -2
View File
@@ -538,8 +538,8 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
)}
{a.ticket &&
(() => {
const label = a.ticket.number ? `Ticket #${a.ticket.number}` : "Ticket opened";
const title = a.ticket.grouped ? "Part of one ticket that covers several problems that appeared together" : "A ticket was opened for this in osTicket";
const label = a.ticket.number ? `Ticket #${a.ticket.number}${a.ticket.closedAt ? " (closed)" : ""}` : "Ticket opened";
const title = a.ticket.closedAt ? "This ticket was closed in osTicket from here" : a.ticket.grouped ? "Part of one ticket that covers several problems that appeared together" : "A ticket was opened for this in osTicket";
return a.ticket.url ? (
<a className="badge bg-blue-lt text-blue ms-2" href={a.ticket.url} target="_blank" rel="noopener noreferrer" title={title}>
{label}
+28 -2
View File
@@ -35,7 +35,7 @@ export default function AlertTicketSettings() {
const [draft, setDraft] = useState<Settings | null>(null);
const [error, setError] = useState<string | null>(null);
const [notice, setNotice] = useState<string | null>(null);
const [busy, setBusy] = useState<"save" | "test" | "run" | null>(null);
const [busy, setBusy] = useState<"save" | "test" | "run" | "close" | null>(null);
function show(next: AlertTicketOverview) {
setOverview(next);
@@ -89,6 +89,22 @@ export default function AlertTicketSettings() {
}
}
async function testClosing() {
if (!overview?.settings.integrationId) return;
setBusy("close");
setError(null);
setNotice(null);
try {
const res = await api.alertTickets.testClose(overview.settings.integrationId);
if (res.ok) setNotice(`Closing tickets works: the login, the tables and the writes were all tried and rolled back, so nothing changed in osTicket.${res.notes.length > 0 ? ` ${res.notes.join(" ")}` : ""}`);
else setError(res.problems.join(" "));
} catch (err) {
setError(readableError(err));
} finally {
setBusy(null);
}
}
async function checkNow() {
setBusy("run");
setError(null);
@@ -127,7 +143,7 @@ export default function AlertTicketSettings() {
<p className="text-secondary">
Every 15 minutes the problems on the <Link to="/alerts">Alerts page</Link> are checked, and one that is new and has lasted gets a ticket. One problem opens
one ticket, however many checks see it. Problems under a <Link to="/maintenance">maintenance window</Link> never do. osTicket's API can't close or update a
ticket, so the ticket is yours from then on: close it in osTicket when it is dealt with. The Alerts page shows which alerts have one.
ticket, so the ticket is yours from then on: close it in osTicket when it is dealt with, or — with a database login for it, set on the integration — from the Alerts page when you close the alert. The Alerts page shows which alerts have one.
</p>
<div className="alert alert-info">
<div>
@@ -156,6 +172,16 @@ export default function AlertTicketSettings() {
<button className="btn btn-outline-secondary" onClick={() => void sendTest()} disabled={busy !== null || dirty || !overview.settings.integrationId} title={dirty ? "Save first — the test uses the saved settings" : undefined}>
{busy === "test" ? "Sending…" : "Send a test ticket"}
</button>
<button className="btn btn-outline-secondary ms-2" onClick={() => void testClosing()} disabled={busy !== null || !overview.settings.integrationId || !chosen?.closeReady} title={chosen && !chosen.closeReady ? "This integration has no database login for closing tickets yet" : "Tries the login, the tables and the writes a close makes, and rolls them all back"}>
{busy === "close" ? "Trying…" : "Test closing"}
</button>
</div>
<div className="col-12">
<div className="form-hint text-secondary">
{chosen?.closeReady
? "Closing tickets from the Alerts page (the Close drawer's tick box) is set up for this integration."
: "Optional: to close a ticket from the Alerts page, give the osTicket integration a separate database login that may update tickets (see INTEGRATIONS.md). osTicket's own API cannot close tickets."}
</div>
</div>
<div className="col-md-4">
<label className="form-label">Tickets come from (name)</label>