|
| 1 | +const { api } = require('@pagerduty/pdjs'); |
| 2 | +const { |
| 3 | + listIncidents, |
| 4 | + getIncident, |
| 5 | + updateIncidentStatus, |
| 6 | + addIncidentNote, |
| 7 | + listIncidentNotes, |
| 8 | + listIncidentAlerts, |
| 9 | +} = require('./src/incidents.js'); |
| 10 | +const { getMe } = require('./src/users.js'); |
| 11 | + |
| 12 | +if (process.argv.length !== 3) { |
| 13 | + console.error('Usage: node index.js <command>') |
| 14 | + process.exit(1) |
| 15 | +} |
| 16 | + |
| 17 | +const command = process.argv[2] |
| 18 | +let token = process.env.PAGERDUTY_BEARER_TOKEN |
| 19 | +let tokenType = "bearer" |
| 20 | +if (token === undefined || token === "") { |
| 21 | + token = process.env.PAGERDUTY_API_TOKEN |
| 22 | + tokenType = "token" |
| 23 | +} |
| 24 | + |
| 25 | +if (token === undefined || token === "") { |
| 26 | + console.error('Please set the PAGERDUTY_BEARER_TOKEN or PAGERDUTY_API_TOKEN environment variable') |
| 27 | + process.exit(1) |
| 28 | +} |
| 29 | + |
| 30 | +const pd = api({ token: token, tokenType: tokenType }); |
| 31 | + |
| 32 | +async function main() { |
| 33 | + try { |
| 34 | + let incidentId = ""; |
| 35 | + switch (command) { |
| 36 | + case "listIncidents": |
| 37 | + const things = await listIncidents(pd); |
| 38 | + console.log("INCIDENTS: ", things); |
| 39 | + break |
| 40 | + case "getIncident": |
| 41 | + incidentId = getIncidentId() |
| 42 | + const incident = await getIncident(pd, incidentId); |
| 43 | + console.log("INCIDENT: ", incident); |
| 44 | + break |
| 45 | + case "acknowledgeIncident": |
| 46 | + incidentId = getIncidentId(); |
| 47 | + if (incidentId === undefined || incidentId === "") { |
| 48 | + console.error('Please set the INCIDENT_ID environment variable') |
| 49 | + process.exit(1) |
| 50 | + } |
| 51 | + const ackIncident = await updateIncidentStatus(pd, incidentId, 'acknowledged'); |
| 52 | + console.log("ACKNOWLEDGED INCIDENT: ", ackIncident); |
| 53 | + break |
| 54 | + case "resolveIncident": |
| 55 | + incidentId = getIncidentId(); |
| 56 | + const resolvedIncident = await updateIncidentStatus(pd, incidentId, 'resolved'); |
| 57 | + console.log("RESOLVED INCIDENT: ", resolvedIncident); |
| 58 | + break |
| 59 | + case "addIncidentNote": |
| 60 | + incidentId = getIncidentId(); |
| 61 | + const note = process.env.NOTE |
| 62 | + if (note === undefined || note === "") { |
| 63 | + console.error('Please set the NOTE_CONTENT environment variable') |
| 64 | + process.exit(1) |
| 65 | + } |
| 66 | + const noteResp = await addIncidentNote(pd, incidentId, note); |
| 67 | + console.log("NOTE ADDED: ", noteResp); |
| 68 | + break |
| 69 | + case "listIncidentNotes": |
| 70 | + incidentId = getIncidentId(); |
| 71 | + const noteList = await listIncidentNotes(pd, incidentId); |
| 72 | + console.log("NOTES: ", noteList); |
| 73 | + case "listIncidentAlerts": |
| 74 | + incidentId = getIncidentId(); |
| 75 | + const alerts = await listIncidentAlerts(pd, incidentId); |
| 76 | + console.log(JSON.stringify(alerts.alerts)); |
| 77 | + break |
| 78 | + case "getMe": |
| 79 | + const user = await getMe(pd); |
| 80 | + console.log("USER: ", user); |
| 81 | + break; |
| 82 | + default: |
| 83 | + console.log(`Unknown command: ${command}`) |
| 84 | + process.exit(1) |
| 85 | + } |
| 86 | + } catch (error) { |
| 87 | + // We use console.log instead of console.error here so that it goes to stdout |
| 88 | + console.log("Got the following error: ", error); |
| 89 | + process.exit(1) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function getIncidentId() { |
| 94 | + incidentId = process.env.INCIDENT_ID |
| 95 | + if (incidentId === undefined || incidentId === "") { |
| 96 | + console.error('Please set the INCIDENT_ID environment variable') |
| 97 | + process.exit(1) |
| 98 | + } |
| 99 | + return incidentId |
| 100 | +} |
| 101 | + |
| 102 | +main() |
0 commit comments