import { IncidentsService, TasksService } from '@grafana/incident'; async function severityTask(_script, incidentID) { try { const incidentsService = new IncidentsService({ url: 'http://localhost:3000' }); const tasksService = new TasksService({ url: 'http://localhost:3000' }); // this script adds a task to the incident if the severity is // "critical", and removes that task if the severity changes to something else. const getIncidentResp = await incidentsService.getIncident({ incidentID, }); const incident = getIncidentResp.incident; const taskText = 'Notify support of critical incident'; if (incident.severity === 'critical') { // add task if (incident.taskList.tasks && incident.taskList.tasks.find((task) => task.text === taskText)) { console.log(incident.title + ': task already added'); return; } console.log(incident.title + ': this incident is critical, adding task to notify support'); const addTaskResp = await tasksService.addTask({ incidentID, text: taskText, }); console.log('addTaskResp:', JSON.stringify(addTaskResp)); } else { // remove task if (!incident.taskList.tasks) { return; } const existingTask = incident.taskList.tasks.find((task) => task.text === taskText); if (existingTask) { console.log(incident.title + ': removing task to notify support'); const removeTaskResp = await tasksService.deleteTask({ incidentID, taskID: existingTask.taskID, }); console.log('response: ' + JSON.stringify(removeTaskResp)); } } } catch (e) { console.log('Error:', e.message); } } severityTask(...args);