-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bill Maxwell <[email protected]>
- Loading branch information
1 parent
ba9760a
commit c2dee84
Showing
8 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Name: PagerDuty OAuth Credential | ||
Share Credential: pagerduty-cred as pagerduty | ||
Type: credential | ||
|
||
--- | ||
Name: pagerduty-cred | ||
Tools: ../../oauth2 | ||
|
||
#!sys.call ../../oauth2 | ||
|
||
{ | ||
"oauthInfo": { | ||
"integration": "pagerduty", | ||
"token": "PAGERDUTY_BEARER_TOKEN", | ||
"scope": [ | ||
"incidents.read", | ||
"incidents.write", | ||
"users.read" | ||
] | ||
}, | ||
"promptInfo": { | ||
"fields" : [ | ||
{ | ||
"name": "PagerDuty API Key", | ||
"description": "A personal access token for your PagerDuty account.", | ||
"sensitive": true, | ||
"env": "PAGERDUTY_API_TOKEN" | ||
} | ||
], | ||
"message": "Enter your PagerDuty personal API token." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
const { api } = require('@pagerduty/pdjs'); | ||
const { | ||
listIncidents, | ||
getIncident, | ||
updateIncidentStatus, | ||
addIncidentNote, | ||
listIncidentNotes, | ||
listIncidentAlerts, | ||
} = require('./src/incidents.js'); | ||
const { getMe } = require('./src/users.js'); | ||
|
||
if (process.argv.length !== 3) { | ||
console.error('Usage: node index.js <command>') | ||
process.exit(1) | ||
} | ||
|
||
const command = process.argv[2] | ||
let token = process.env.PAGERDUTY_BEARER_TOKEN | ||
let tokenType = "bearer" | ||
if (token === undefined || token === "") { | ||
token = process.env.PAGERDUTY_API_TOKEN | ||
tokenType = "token" | ||
} | ||
|
||
if (token === undefined || token === "") { | ||
console.error('Please set the PAGERDUTY_BEARER_TOKEN or PAGERDUTY_API_TOKEN environment variable') | ||
process.exit(1) | ||
} | ||
|
||
const pd = api({ token: token, tokenType: tokenType }); | ||
|
||
async function main() { | ||
try { | ||
let incidentId = ""; | ||
switch (command) { | ||
case "listIncidents": | ||
const things = await listIncidents(pd); | ||
console.log("INCIDENTS: ", things); | ||
break | ||
case "getIncident": | ||
incidentId = getIncidentId() | ||
const incident = await getIncident(pd, incidentId); | ||
console.log("INCIDENT: ", incident); | ||
break | ||
case "acknowledgeIncident": | ||
incidentId = getIncidentId(); | ||
if (incidentId === undefined || incidentId === "") { | ||
console.error('Please set the INCIDENT_ID environment variable') | ||
process.exit(1) | ||
} | ||
const ackIncident = await updateIncidentStatus(pd, incidentId, 'acknowledged'); | ||
console.log("ACKNOWLEDGED INCIDENT: ", ackIncident); | ||
break | ||
case "resolveIncident": | ||
incidentId = getIncidentId(); | ||
const resolvedIncident = await updateIncidentStatus(pd, incidentId, 'resolved'); | ||
console.log("RESOLVED INCIDENT: ", resolvedIncident); | ||
break | ||
case "addIncidentNote": | ||
incidentId = getIncidentId(); | ||
const note = process.env.NOTE | ||
if (note === undefined || note === "") { | ||
console.error('Please set the NOTE_CONTENT environment variable') | ||
process.exit(1) | ||
} | ||
const noteResp = await addIncidentNote(pd, incidentId, note); | ||
console.log("NOTE ADDED: ", noteResp); | ||
break | ||
case "listIncidentNotes": | ||
incidentId = getIncidentId(); | ||
const noteList = await listIncidentNotes(pd, incidentId); | ||
console.log("NOTES: ", noteList); | ||
case "listIncidentAlerts": | ||
incidentId = getIncidentId(); | ||
const alerts = await listIncidentAlerts(pd, incidentId); | ||
console.log(JSON.stringify(alerts.alerts)); | ||
break | ||
case "getMe": | ||
const user = await getMe(pd); | ||
console.log("USER: ", user); | ||
break; | ||
default: | ||
console.log(`Unknown command: ${command}`) | ||
process.exit(1) | ||
} | ||
} catch (error) { | ||
// We use console.log instead of console.error here so that it goes to stdout | ||
console.log("Got the following error: ", error); | ||
process.exit(1) | ||
} | ||
} | ||
|
||
function getIncidentId() { | ||
incidentId = process.env.INCIDENT_ID | ||
if (incidentId === undefined || incidentId === "") { | ||
console.error('Please set the INCIDENT_ID environment variable') | ||
process.exit(1) | ||
} | ||
return incidentId | ||
} | ||
|
||
main() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"dependencies": { | ||
"@pagerduty/pdjs": "^2.2.4" | ||
}, | ||
"name": "pd-tool", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"description": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
function getEmail() { | ||
return process.env.USER_EMAIL; | ||
} | ||
|
||
async function listIncidents(client) { | ||
const resp = await client.get('/incidents'); | ||
return resp.resource; | ||
} | ||
|
||
async function getIncident(client, id) { | ||
const resp = await client.get(`/incidents/${id}`); | ||
return resp.data; | ||
} | ||
|
||
async function updateIncidentStatus(client, id, status) { | ||
const orig = await getIncident(client, id); | ||
console.log("Original: ", orig.incident.id); | ||
const resp = await client.put(`/incidents/${orig.incident.id}`, { | ||
headers: { | ||
Accept: 'application/vnd.pagerduty+json;version=2', | ||
From: getEmail(), | ||
}, | ||
data: { | ||
incident: { | ||
type: 'incident_reference', | ||
status: status | ||
} | ||
}, | ||
}); | ||
return resp.data; | ||
} | ||
|
||
async function listIncidentNotes(client, id) { | ||
const resp = await client.get(`/incidents/${id}/notes`); | ||
return resp.resource; | ||
} | ||
|
||
async function addIncidentNote(client, id, contents) { | ||
const resp = await client.post(`/incidents/${id}/notes`, { | ||
headers: { | ||
From: getEmail(), | ||
}, | ||
data: { | ||
note: { | ||
content: contents | ||
} | ||
} | ||
}); | ||
return resp.data; | ||
} | ||
|
||
async function listIncidentAlerts(client, id) { | ||
const resp = await client.get(`/incidents/${id}/alerts`); | ||
return resp.data; | ||
} | ||
|
||
module.exports = { | ||
listIncidents, | ||
getIncident, | ||
updateIncidentStatus, | ||
addIncidentNote, | ||
listIncidentNotes, | ||
listIncidentAlerts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
async function getMe(client) { | ||
const resp = await client.get('/users/me'); | ||
return resp.data; | ||
} | ||
|
||
module.exports = { | ||
getMe | ||
} |
Oops, something went wrong.