Skip to content

Instantly share code, notes, and snippets.

@joawan
Created September 16, 2021 08:55
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/8ee273986b2a4b2217ecc5a865f8706e to your computer and use it in GitHub Desktop.
Save joawan/8ee273986b2a4b2217ecc5a865f8706e to your computer and use it in GitHub Desktop.
Grabbing contact for people oncall
const getOncallForSchedule = async (team, 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,
team,
};
};
const getOncallForTeams = async (teams, pd) => {
const promises = teams.map((team) => team.schedules.map(async (scheduleId) => {
try {
return await getOncallForSchedule(team.name, scheduleId, pd);
} catch (e) {
return { error: `Unexpected error when fetching oncallee for ${team.name}` };
}
})).flat();
return Promise.all(promises);
};
// await getOncallForTeams([... filtered teams ...], PagerDutySDK) => [userX, userY, userZ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment