Skip to content

Commit

Permalink
enhance: add PagerDuty tool.
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Maxwell <[email protected]>
  • Loading branch information
cloudnautique committed Feb 20, 2025
1 parent ba9760a commit c2dee84
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ tools:
reference: ./word
tavily:
reference: ./search/tavily
pagerduty:
reference: ./pagerduty

knowledgeDataSources:
notion-data-source:
Expand Down
32 changes: 32 additions & 0 deletions pagerduty/credential/tool.gpt
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."
}
}
102 changes: 102 additions & 0 deletions pagerduty/index.js
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()
80 changes: 80 additions & 0 deletions pagerduty/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pagerduty/package.json
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": ""
}
64 changes: 64 additions & 0 deletions pagerduty/src/incidents.js
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
}
8 changes: 8 additions & 0 deletions pagerduty/src/users.js
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
}
Loading

0 comments on commit c2dee84

Please sign in to comment.