Skip to content

Instantly share code, notes, and snippets.

@joawan
Created October 29, 2021 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joawan/0a1a97cccef2cfdfaabdae6d024afa73 to your computer and use it in GitHub Desktop.
Save joawan/0a1a97cccef2cfdfaabdae6d024afa73 to your computer and use it in GitHub Desktop.
Get oncall from PagerDuty
const getOncallForSchedule = async (id, pd) => {
const userOnCall = await pd.schedules.listUsersOnCall(id, {
time_zone: 'UTC',
since: new Date().toISOString(),
until: new Date(Date.now() + 1000).toISOString(),
});
const user = JSON.parse(userOnCall.body).users.pop();
const contactMethods = await pd.users.listContactMethods(user.id);
const userDetails = JSON.parse(contactMethods.body);
const contact = userDetails.contact_methods.find((e) => e.type === 'phone_contact_method');
return {
id: user.id,
name: user.name,
email: user.email,
phone: contact ? `+${contact.country_code}${contact.address}` : null,
time_zone: user.time_zone,
};
};
// Usage
const PD = require('node-pagerduty');
const pagerdutyClient = new PD(process.env.PD_TOKEN);
getOncallForSchedule('PCBO9T1', pagerdutyClient).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment