Skip to content

Commit 1a70c22

Browse files
enhance: add PagerDuty tool. (#452)
Signed-off-by: Bill Maxwell <[email protected]>
1 parent ba9760a commit 1a70c22

File tree

8 files changed

+413
-0
lines changed

8 files changed

+413
-0
lines changed

index.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ tools:
6161
reference: ./word
6262
tavily:
6363
reference: ./search/tavily
64+
pagerduty:
65+
reference: ./pagerduty
6466

6567
knowledgeDataSources:
6668
notion-data-source:

pagerduty/credential/tool.gpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Name: PagerDuty OAuth Credential
2+
Share Credential: pagerduty-cred as pagerduty
3+
Type: credential
4+
5+
---
6+
Name: pagerduty-cred
7+
Tools: ../../oauth2
8+
9+
#!sys.call ../../oauth2
10+
11+
{
12+
"oauthInfo": {
13+
"integration": "pagerduty",
14+
"token": "PAGERDUTY_BEARER_TOKEN",
15+
"scope": [
16+
"incidents.read",
17+
"incidents.write",
18+
"users.read"
19+
]
20+
},
21+
"promptInfo": {
22+
"fields" : [
23+
{
24+
"name": "PagerDuty API Key",
25+
"description": "A personal access token for your PagerDuty account.",
26+
"sensitive": true,
27+
"env": "PAGERDUTY_API_TOKEN"
28+
}
29+
],
30+
"message": "Enter your PagerDuty personal API token."
31+
}
32+
}

pagerduty/index.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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()

pagerduty/package-lock.json

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pagerduty/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"dependencies": {
3+
"@pagerduty/pdjs": "^2.2.4"
4+
},
5+
"name": "pd-tool",
6+
"version": "1.0.0",
7+
"main": "index.js",
8+
"devDependencies": {},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"description": ""
15+
}

pagerduty/src/incidents.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function getEmail() {
2+
return process.env.USER_EMAIL;
3+
}
4+
5+
async function listIncidents(client) {
6+
const resp = await client.get('/incidents');
7+
return resp.resource;
8+
}
9+
10+
async function getIncident(client, id) {
11+
const resp = await client.get(`/incidents/${id}`);
12+
return resp.data;
13+
}
14+
15+
async function updateIncidentStatus(client, id, status) {
16+
const orig = await getIncident(client, id);
17+
console.log("Original: ", orig.incident.id);
18+
const resp = await client.put(`/incidents/${orig.incident.id}`, {
19+
headers: {
20+
Accept: 'application/vnd.pagerduty+json;version=2',
21+
From: getEmail(),
22+
},
23+
data: {
24+
incident: {
25+
type: 'incident_reference',
26+
status: status
27+
}
28+
},
29+
});
30+
return resp.data;
31+
}
32+
33+
async function listIncidentNotes(client, id) {
34+
const resp = await client.get(`/incidents/${id}/notes`);
35+
return resp.resource;
36+
}
37+
38+
async function addIncidentNote(client, id, contents) {
39+
const resp = await client.post(`/incidents/${id}/notes`, {
40+
headers: {
41+
From: getEmail(),
42+
},
43+
data: {
44+
note: {
45+
content: contents
46+
}
47+
}
48+
});
49+
return resp.data;
50+
}
51+
52+
async function listIncidentAlerts(client, id) {
53+
const resp = await client.get(`/incidents/${id}/alerts`);
54+
return resp.data;
55+
}
56+
57+
module.exports = {
58+
listIncidents,
59+
getIncident,
60+
updateIncidentStatus,
61+
addIncidentNote,
62+
listIncidentNotes,
63+
listIncidentAlerts
64+
}

pagerduty/src/users.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async function getMe(client) {
2+
const resp = await client.get('/users/me');
3+
return resp.data;
4+
}
5+
6+
module.exports = {
7+
getMe
8+
}

0 commit comments

Comments
 (0)