From 2a178f6873641bc251e505282727581db4f17471 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 13:45:10 -0600 Subject: [PATCH 01/11] two changes to incident triggers Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 09ecad1..45ffa28 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -4,7 +4,7 @@ // Commands: // hubot pager me as - remember your pager email is // hubot pager forget me - forget your pager email -// hubot Am I on call - return if I'm currently on call or not +// hubot am I on call - return if I'm currently on call or not // hubot who's on call - return a list of services and who is on call for them // hubot who's on call for - return the username of who's on call for any schedule matching // hubot pager trigger - create a new incident with and assign it to @@ -45,6 +45,7 @@ const moment = require('moment-timezone'); const pagerDutyUserId = process.env.HUBOT_PAGERDUTY_USER_ID; const pagerDutyServiceApiKey = process.env.HUBOT_PAGERDUTY_SERVICE_API_KEY; const pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES; +const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; module.exports = function (robot) { let campfireUserToPagerDutyUser; @@ -145,19 +146,31 @@ module.exports = function (robot) { robot.respond(/(pager|major)( me)? (?:trigger|page) ([\w\-]+)$/i, (msg) => msg.reply("Please include a user or schedule to page, like 'hubot pager infrastructure everything is on fire'.") ); + robot.respond( - /(pager|major)( me)? (?:trigger|page) ((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+)) (.+)$/i, + /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i, function (msg) { msg.finish(); if (pagerduty.missingEnvironmentForApi(msg)) { return; } - const fromUserName = msg.message.user.name; - let query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; - const reason = msg.match[9]; - const description = `${reason} - @${fromUserName}`; + let query, description; + if (!msg.match[4] && !msg.match[5] && !msg.match[6] && !msg.match[7] && !msg.match[8] && !msg.match[9]) { + robot.logger.info(`Triggering a default page to ${pagerDutyDefaultSchedule}!`); + if (!pagerDutyDefaultSchedule) { + msg.send("No default schedule configured! Cannot send a page! Please set HUBOT_PAGERDUTY_DEFAULT_SCHEDULE"); + return; + } + query = pagerDutyDefaultSchedule; + description = `Generic Page - @${fromUserName}`; + } else { + query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; + robot.logger.info(`Triggering a page to ${query}!`); + const reason = msg.match[9]; + description = `${reason} - @${fromUserName}`; + } // Figure out who we are campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { @@ -697,7 +710,7 @@ module.exports = function (robot) { }); }); - robot.respond(/pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)(.+) (\d+)$/i, function (msg) { + robot.respond(/pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)"?(.+)"? (\d+)$/i, function (msg) { msg.finish(); if (pagerduty.missingEnvironmentForApi(msg)) { From 540126e3a3c054ba829da6e3e252ac2bd961254d Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 13:55:31 -0600 Subject: [PATCH 02/11] fix tests Signed-off-by: John Seekins --- test/pager-me-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/pager-me-test.js b/test/pager-me-test.js index 5812039..25e410c 100644 --- a/test/pager-me-test.js +++ b/test/pager-me-test.js @@ -7,7 +7,7 @@ const { expect } = chai; describe('pagerduty', function () { before(function () { this.triggerRegex = - /(pager|major)( me)? (?:trigger|page) ((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+)) (.+)$/i; + /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i; this.schedulesRegex = /(pager|major)( me)? schedules( ((["'])([^]*?)\5|(.+)))?$/i; this.whosOnCallRegex = /who(?:’s|'s|s| is|se)? (?:on call|oncall|on-call)(?:\?)?(?: (?:for )?((["'])([^]*?)\2|(.*?))(?:\?|$))?$/i; @@ -98,7 +98,7 @@ describe('pagerduty', function () { it('registers a pager link listener', function () { expect(this.robot.respond).to.have.been.calledWith( - /pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)(.+) (\d+)$/i + /pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)"?(.+)"? (\d+)$/i ); }); From 73e022183c8679fc0de0ceab8a6ed68f72e124d0 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 14:14:34 -0600 Subject: [PATCH 03/11] strip quotes Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 5 +++-- test/pager-me-test.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index a6187b0..660b0b0 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -49,6 +49,7 @@ const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; module.exports = function (robot) { let campfireUserToPagerDutyUser; + robot.respond(/pager( me)?$/i, function (msg) { if (pagerduty.missingEnvironmentForApi(msg)) { return; @@ -716,7 +717,7 @@ module.exports = function (robot) { }); }); - robot.respond(/pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)"?(.+)"? (\d+)$/i, function (msg) { + robot.respond(/pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)(.+) (\d+)$/i, function (msg) { msg.finish(); if (pagerduty.missingEnvironmentForApi(msg)) { @@ -735,7 +736,7 @@ module.exports = function (robot) { ); return; } - + const schedule = msg.match[2].replace(/(^"|"$)/mg, ''); withScheduleMatching(msg, msg.match[2], function (matchingSchedule) { if (!matchingSchedule.id) { return; diff --git a/test/pager-me-test.js b/test/pager-me-test.js index 25e410c..9f9afa8 100644 --- a/test/pager-me-test.js +++ b/test/pager-me-test.js @@ -98,7 +98,7 @@ describe('pagerduty', function () { it('registers a pager link listener', function () { expect(this.robot.respond).to.have.been.calledWith( - /pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)"?(.+)"? (\d+)$/i + /pager( me)? (?!schedules?\b|overrides?\b|my schedule\b)(.+) (\d+)$/i ); }); From b4817c33f0f2e6da58123540619f632b8543aa04 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 14:18:55 -0600 Subject: [PATCH 04/11] actually *use* stripped string Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 660b0b0..d2607d0 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -737,13 +737,13 @@ module.exports = function (robot) { return; } const schedule = msg.match[2].replace(/(^"|"$)/mg, ''); - withScheduleMatching(msg, msg.match[2], function (matchingSchedule) { + const minutes = parseInt(msg.match[3]); + withScheduleMatching(msg, schedule, function (matchingSchedule) { if (!matchingSchedule.id) { return; } let start = moment().format(); - const minutes = parseInt(msg.match[3]); let end = moment().add(minutes, 'minutes').format(); const override = { start, From 3c5f07c48bd9cfaa57fdc7f4cb838d80f5624015 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 14:48:15 -0600 Subject: [PATCH 05/11] shared code Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 222 +++++++++++++++++++++------------------ test/pager-me-test.js | 4 + 2 files changed, 122 insertions(+), 104 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index d2607d0..6f0a0a4 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -8,6 +8,7 @@ // hubot who's on call - return a list of services and who is on call for them // hubot who's on call for - return the username of who's on call for any schedule matching // hubot pager trigger - create a new incident with and assign it to +// hubot pager default trigger - create a new incident with and assign it the user currently on call for our default schedule // hubot pager trigger - create a new incident with and assign it the user currently on call for // hubot pager incidents - return the current incidents // hubot pager sup - return the current incidents @@ -47,6 +48,99 @@ const pagerDutyServiceApiKey = process.env.HUBOT_PAGERDUTY_SERVICE_API_KEY; const pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES; const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; +function incidentTrigger(robot, msg, query, description) { + // Figure out who we are + campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { + const triggerdByPagerDutyUserId = (() => { + if (triggerdByPagerDutyUser != null) { + return triggerdByPagerDutyUser.id; + } else if (pagerDutyUserId) { + return pagerDutyUserId; + } + })(); + if (!triggerdByPagerDutyUserId) { + msg.send( + `Sorry, I can't figure your PagerDuty account, and I don't have my own :( Can you tell me your PagerDuty email with \`${robot.name} pager me as you@yourdomain.com\` or make sure you've set the HUBOT_PAGERDUTY_USER_ID environment variable?` + ); + return; + } + + // Figure out what we're trying to page + reassignmentParametersForUserOrScheduleOrEscalationPolicy(msg, query, function (results) { + if (!(results.assigned_to_user || results.escalation_policy)) { + msg.reply(`Couldn't find a user or unique schedule or escalation policy matching ${query} :/`); + return; + } + + return pagerDutyIntegrationAPI(msg, 'trigger', description, function (json) { + query = { incident_key: json.incident_key }; + + msg.reply(':pager: triggered! now assigning it to the right user...'); + + return setTimeout( + () => + pagerduty.get('/incidents', query, function (err, json) { + if (err != null) { + robot.emit('error', err, msg); + return; + } + + if ((json != null ? json.incidents.length : undefined) === 0) { + msg.reply("Couldn't find the incident we just created to reassign. Please try again :/"); + } else { + } + + let data = null; + if (results.escalation_policy) { + data = { + incidents: json.incidents.map((incident) => ({ + id: incident.id, + type: 'incident_reference', + + escalation_policy: { + id: results.escalation_policy, + type: 'escalation_policy_reference', + }, + })), + }; + } else { + data = { + incidents: json.incidents.map((incident) => ({ + id: incident.id, + type: 'incident_reference', + + assignments: [ + { + assignee: { + id: results.assigned_to_user, + type: 'user_reference', + }, + }, + ], + })), + }; + } + + return pagerduty.put('/incidents', data, function (err, json) { + if (err != null) { + robot.emit('error', err, msg); + return; + } + + if ((json != null ? json.incidents.length : undefined) === 1) { + return msg.reply(`:pager: assigned to ${results.name}!`); + } else { + return msg.reply('Problem reassigning the incident :/'); + } + }); + }), + 10000 + ); + }); + }); + }); +} + module.exports = function (robot) { let campfireUserToPagerDutyUser; @@ -155,7 +249,7 @@ module.exports = function (robot) { ); robot.respond( - /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i, + /(pager|major)( me)? default (?:trigger|page) ?(.+)?$/i, function (msg) { msg.finish(); @@ -163,112 +257,32 @@ module.exports = function (robot) { return; } const fromUserName = msg.message.user.name; - let query, description; - if (!msg.match[4] && !msg.match[5] && !msg.match[6] && !msg.match[7] && !msg.match[8] && !msg.match[9]) { - robot.logger.info(`Triggering a default page to ${pagerDutyDefaultSchedule}!`); - if (!pagerDutyDefaultSchedule) { - msg.send("No default schedule configured! Cannot send a page! Please set HUBOT_PAGERDUTY_DEFAULT_SCHEDULE"); - return; - } - query = pagerDutyDefaultSchedule; - description = `Generic Page - @${fromUserName}`; - } else { - query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; - robot.logger.info(`Triggering a page to ${query}!`); - const reason = msg.match[9]; - description = `${reason} - @${fromUserName}`; + robot.logger.debug(`Triggering a default page to ${pagerDutyDefaultSchedule}!`); + if (!pagerDutyDefaultSchedule) { + msg.send("No default schedule configured! Cannot send a page! Please set HUBOT_PAGERDUTY_DEFAULT_SCHEDULE"); + return; } + query = pagerDutyDefaultSchedule; + reason = msg.match[4] || "We Need Help!" + description = `${reason} - @${fromUserName}`; + incidentTrigger(robot, msg, query, description); + } + ); - // Figure out who we are - campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { - const triggerdByPagerDutyUserId = (() => { - if (triggerdByPagerDutyUser != null) { - return triggerdByPagerDutyUser.id; - } else if (pagerDutyUserId) { - return pagerDutyUserId; - } - })(); - if (!triggerdByPagerDutyUserId) { - msg.send( - `Sorry, I can't figure your PagerDuty account, and I don't have my own :( Can you tell me your PagerDuty email with \`${robot.name} pager me as you@yourdomain.com\` or make sure you've set the HUBOT_PAGERDUTY_USER_ID environment variable?` - ); - return; - } - - // Figure out what we're trying to page - reassignmentParametersForUserOrScheduleOrEscalationPolicy(msg, query, function (results) { - if (!(results.assigned_to_user || results.escalation_policy)) { - msg.reply(`Couldn't find a user or unique schedule or escalation policy matching ${query} :/`); - return; - } - - return pagerDutyIntegrationAPI(msg, 'trigger', description, function (json) { - query = { incident_key: json.incident_key }; - - msg.reply(':pager: triggered! now assigning it to the right user...'); - - return setTimeout( - () => - pagerduty.get('/incidents', query, function (err, json) { - if (err != null) { - robot.emit('error', err, msg); - return; - } - - if ((json != null ? json.incidents.length : undefined) === 0) { - msg.reply("Couldn't find the incident we just created to reassign. Please try again :/"); - } else { - } - - let data = null; - if (results.escalation_policy) { - data = { - incidents: json.incidents.map((incident) => ({ - id: incident.id, - type: 'incident_reference', - - escalation_policy: { - id: results.escalation_policy, - type: 'escalation_policy_reference', - }, - })), - }; - } else { - data = { - incidents: json.incidents.map((incident) => ({ - id: incident.id, - type: 'incident_reference', - - assignments: [ - { - assignee: { - id: results.assigned_to_user, - type: 'user_reference', - }, - }, - ], - })), - }; - } + robot.respond( + /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i, + function (msg) { + msg.finish(); - return pagerduty.put('/incidents', data, function (err, json) { - if (err != null) { - robot.emit('error', err, msg); - return; - } - - if ((json != null ? json.incidents.length : undefined) === 1) { - return msg.reply(`:pager: assigned to ${results.name}!`); - } else { - return msg.reply('Problem reassigning the incident :/'); - } - }); - }), - 10000 - ); - }); - }); - }); + if (pagerduty.missingEnvironmentForApi(msg)) { + return; + } + const fromUserName = msg.message.user.name; + const query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; + robot.logger.info(`Triggering a page to ${query}!`); + const reason = msg.match[9]; + const description = `${reason} - @${fromUserName}`; + incidentTrigger(robot, msg, user, query, description); } ); diff --git a/test/pager-me-test.js b/test/pager-me-test.js index 9f9afa8..c41e073 100644 --- a/test/pager-me-test.js +++ b/test/pager-me-test.js @@ -46,6 +46,10 @@ describe('pagerduty', function () { expect(this.robot.respond).to.have.been.calledWith(/(pager|major)( me)? (?:trigger|page) ([\w\-]+)$/i); }); + it('registers a pager default trigger with message listener', function () { + expect(this.robot.respond).to.have.been.calledWith(/(pager|major)( me)? default (?:trigger|page) ?(.+)?$/i); + }); + it('registers a pager trigger with message listener', function () { expect(this.robot.respond).to.have.been.calledWith(this.triggerRegex); }); From c8522b139ff31c30401f82c986d1430cf8628109 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 14:51:59 -0600 Subject: [PATCH 06/11] pass function through Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 6f0a0a4..8343b7f 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -48,7 +48,7 @@ const pagerDutyServiceApiKey = process.env.HUBOT_PAGERDUTY_SERVICE_API_KEY; const pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES; const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; -function incidentTrigger(robot, msg, query, description) { +function incidentTrigger(robot, campfileUserToPagerDutyUser, msg, query, description) { // Figure out who we are campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { const triggerdByPagerDutyUserId = (() => { @@ -265,7 +265,7 @@ module.exports = function (robot) { query = pagerDutyDefaultSchedule; reason = msg.match[4] || "We Need Help!" description = `${reason} - @${fromUserName}`; - incidentTrigger(robot, msg, query, description); + incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description); } ); @@ -282,7 +282,7 @@ module.exports = function (robot) { robot.logger.info(`Triggering a page to ${query}!`); const reason = msg.match[9]; const description = `${reason} - @${fromUserName}`; - incidentTrigger(robot, msg, user, query, description); + incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description); } ); From dcd4be92b6dfb83581536da8bfd92627b07095d3 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 14:55:24 -0600 Subject: [PATCH 07/11] fix name Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 8343b7f..9bd140c 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -48,7 +48,7 @@ const pagerDutyServiceApiKey = process.env.HUBOT_PAGERDUTY_SERVICE_API_KEY; const pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES; const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; -function incidentTrigger(robot, campfileUserToPagerDutyUser, msg, query, description) { +function incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description) { // Figure out who we are campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { const triggerdByPagerDutyUserId = (() => { From 20e95cc53486a6a38fd79486e7e73e0dd73c6ca5 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 15:07:08 -0600 Subject: [PATCH 08/11] move function inside robot Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 192 +++++++++++++++++++-------------------- test/pager-me-test.js | 2 +- 2 files changed, 97 insertions(+), 97 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 9bd140c..23a8c3e 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -48,98 +48,6 @@ const pagerDutyServiceApiKey = process.env.HUBOT_PAGERDUTY_SERVICE_API_KEY; const pagerDutySchedules = process.env.HUBOT_PAGERDUTY_SCHEDULES; const pagerDutyDefaultSchedule = process.env.HUBOT_PAGERDUTY_DEFAULT_SCHEDULE; -function incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description) { - // Figure out who we are - campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { - const triggerdByPagerDutyUserId = (() => { - if (triggerdByPagerDutyUser != null) { - return triggerdByPagerDutyUser.id; - } else if (pagerDutyUserId) { - return pagerDutyUserId; - } - })(); - if (!triggerdByPagerDutyUserId) { - msg.send( - `Sorry, I can't figure your PagerDuty account, and I don't have my own :( Can you tell me your PagerDuty email with \`${robot.name} pager me as you@yourdomain.com\` or make sure you've set the HUBOT_PAGERDUTY_USER_ID environment variable?` - ); - return; - } - - // Figure out what we're trying to page - reassignmentParametersForUserOrScheduleOrEscalationPolicy(msg, query, function (results) { - if (!(results.assigned_to_user || results.escalation_policy)) { - msg.reply(`Couldn't find a user or unique schedule or escalation policy matching ${query} :/`); - return; - } - - return pagerDutyIntegrationAPI(msg, 'trigger', description, function (json) { - query = { incident_key: json.incident_key }; - - msg.reply(':pager: triggered! now assigning it to the right user...'); - - return setTimeout( - () => - pagerduty.get('/incidents', query, function (err, json) { - if (err != null) { - robot.emit('error', err, msg); - return; - } - - if ((json != null ? json.incidents.length : undefined) === 0) { - msg.reply("Couldn't find the incident we just created to reassign. Please try again :/"); - } else { - } - - let data = null; - if (results.escalation_policy) { - data = { - incidents: json.incidents.map((incident) => ({ - id: incident.id, - type: 'incident_reference', - - escalation_policy: { - id: results.escalation_policy, - type: 'escalation_policy_reference', - }, - })), - }; - } else { - data = { - incidents: json.incidents.map((incident) => ({ - id: incident.id, - type: 'incident_reference', - - assignments: [ - { - assignee: { - id: results.assigned_to_user, - type: 'user_reference', - }, - }, - ], - })), - }; - } - - return pagerduty.put('/incidents', data, function (err, json) { - if (err != null) { - robot.emit('error', err, msg); - return; - } - - if ((json != null ? json.incidents.length : undefined) === 1) { - return msg.reply(`:pager: assigned to ${results.name}!`); - } else { - return msg.reply('Problem reassigning the incident :/'); - } - }); - }), - 10000 - ); - }); - }); - }); -} module.exports = function (robot) { let campfireUserToPagerDutyUser; @@ -265,12 +173,12 @@ module.exports = function (robot) { query = pagerDutyDefaultSchedule; reason = msg.match[4] || "We Need Help!" description = `${reason} - @${fromUserName}`; - incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description); + incidentTrigger(msg, query, description); } ); robot.respond( - /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i, + /(pager|major)( me)? (?:trigger|page) ((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+)) ?(.+)?$/i, function (msg) { msg.finish(); @@ -280,9 +188,9 @@ module.exports = function (robot) { const fromUserName = msg.message.user.name; const query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; robot.logger.info(`Triggering a page to ${query}!`); - const reason = msg.match[9]; + const reason = msg.match[9] || "We Need Help!"; const description = `${reason} - @${fromUserName}`; - incidentTrigger(robot, campfireUserToPagerDutyUser, msg, query, description); + incidentTrigger(msg, query, description); } ); @@ -1210,6 +1118,98 @@ module.exports = function (robot) { } }); + var incidentTrigger = (msg, query, description) => + // Figure out who we are + campfireUserToPagerDutyUser(msg, msg.message.user, false, function (triggerdByPagerDutyUser) { + const triggerdByPagerDutyUserId = (() => { + if (triggerdByPagerDutyUser != null) { + return triggerdByPagerDutyUser.id; + } else if (pagerDutyUserId) { + return pagerDutyUserId; + } + })(); + if (!triggerdByPagerDutyUserId) { + msg.send( + `Sorry, I can't figure your PagerDuty account, and I don't have my own :( Can you tell me your PagerDuty email with \`${robot.name} pager me as you@yourdomain.com\` or make sure you've set the HUBOT_PAGERDUTY_USER_ID environment variable?` + ); + return; + } + + // Figure out what we're trying to page + reassignmentParametersForUserOrScheduleOrEscalationPolicy(msg, query, function (results) { + if (!(results.assigned_to_user || results.escalation_policy)) { + msg.reply(`Couldn't find a user or unique schedule or escalation policy matching ${query} :/`); + return; + } + + return pagerDutyIntegrationAPI(msg, 'trigger', description, function (json) { + query = { incident_key: json.incident_key }; + + msg.reply(':pager: triggered! now assigning it to the right user...'); + + return setTimeout( + () => + pagerduty.get('/incidents', query, function (err, json) { + if (err != null) { + robot.emit('error', err, msg); + return; + } + + if ((json != null ? json.incidents.length : undefined) === 0) { + msg.reply("Couldn't find the incident we just created to reassign. Please try again :/"); + } else { + } + + let data = null; + if (results.escalation_policy) { + data = { + incidents: json.incidents.map((incident) => ({ + id: incident.id, + type: 'incident_reference', + + escalation_policy: { + id: results.escalation_policy, + type: 'escalation_policy_reference', + }, + })), + }; + } else { + data = { + incidents: json.incidents.map((incident) => ({ + id: incident.id, + type: 'incident_reference', + + assignments: [ + { + assignee: { + id: results.assigned_to_user, + type: 'user_reference', + }, + }, + ], + })), + }; + } + + return pagerduty.put('/incidents', data, function (err, json) { + if (err != null) { + robot.emit('error', err, msg); + return; + } + + if ((json != null ? json.incidents.length : undefined) === 1) { + return msg.reply(`:pager: assigned to ${results.name}!`); + } else { + return msg.reply('Problem reassigning the incident :/'); + } + }); + }), + 10000 + ); + }); + }); + }); + const userEmail = (user) => user.pagerdutyEmail || user.email_address || diff --git a/test/pager-me-test.js b/test/pager-me-test.js index c41e073..b2d3514 100644 --- a/test/pager-me-test.js +++ b/test/pager-me-test.js @@ -7,7 +7,7 @@ const { expect } = chai; describe('pagerduty', function () { before(function () { this.triggerRegex = - /(pager|major)( me)? (?:trigger|page) ?((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+))? ?(.+)?$/i; + /(pager|major)( me)? (?:trigger|page) ((["'])([^\4]*?)\4|“([^”]*?)”|‘([^’]*?)’|([\.\w\-]+)) ?(.+)?$/i; this.schedulesRegex = /(pager|major)( me)? schedules( ((["'])([^]*?)\5|(.+)))?$/i; this.whosOnCallRegex = /who(?:’s|'s|s| is|se)? (?:on call|oncall|on-call)(?:\?)?(?: (?:for )?((["'])([^]*?)\2|(.*?))(?:\?|$))?$/i; From ea7bf5378ae509c8d2c06085704d8dac6c847979 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 15:16:57 -0600 Subject: [PATCH 09/11] nicer debug logs Signed-off-by: John Seekins --- src/scripts/pagerduty.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripts/pagerduty.js b/src/scripts/pagerduty.js index 23a8c3e..9232bec 100644 --- a/src/scripts/pagerduty.js +++ b/src/scripts/pagerduty.js @@ -164,15 +164,15 @@ module.exports = function (robot) { if (pagerduty.missingEnvironmentForApi(msg)) { return; } - const fromUserName = msg.message.user.name; - robot.logger.debug(`Triggering a default page to ${pagerDutyDefaultSchedule}!`); if (!pagerDutyDefaultSchedule) { msg.send("No default schedule configured! Cannot send a page! Please set HUBOT_PAGERDUTY_DEFAULT_SCHEDULE"); return; } + const fromUserName = msg.message.user.name; query = pagerDutyDefaultSchedule; reason = msg.match[4] || "We Need Help!" description = `${reason} - @${fromUserName}`; + robot.logger.debug(`Triggering a default page to ${pagerDutyDefaultSchedule} saying ${description}!`); incidentTrigger(msg, query, description); } ); @@ -187,9 +187,9 @@ module.exports = function (robot) { } const fromUserName = msg.message.user.name; const query = msg.match[5] || msg.match[6] || msg.match[7] || msg.match[8]; - robot.logger.info(`Triggering a page to ${query}!`); const reason = msg.match[9] || "We Need Help!"; const description = `${reason} - @${fromUserName}`; + robot.logger.debug(`Triggering a page to ${query} saying ${description}!`); incidentTrigger(msg, query, description); } ); From c77217af43a1b2cabef1422869c9cff9acbe34ed Mon Sep 17 00:00:00 2001 From: John Seekins Date: Thu, 26 Sep 2024 16:03:43 -0600 Subject: [PATCH 10/11] add env var in docs Signed-off-by: John Seekins --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5eb918e..5b8e157 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,9 @@ Then add **hubot-pager-me** to your `external-scripts.json`: | `HUBOT_PAGERDUTY_FROM_EMAIL` | Yes | The email of the default "actor" user for incident creation and modification. | | `HUBOT_PAGERDUTY_USER_ID` | No`*` | The user ID of a PagerDuty user for your bot. This is only required if you want chat users to be able to trigger incidents without their own PagerDuty user. | `HUBOT_PAGERDUTY_SERVICE_API_KEY` | No`*` | The [Incident Service Key](https://v2.developer.pagerduty.com/docs/incident-creation-api) to use when creating a new incident. This should be assigned to a dummy escalation policy that doesn't actually notify, as Hubot will trigger on this before reassigning it. -| `HUBOT_PAGERDUTY_SERVICES` | No | Provide a comma separated list of service identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those services. | -| `HUBOT_PAGERDUTY_SCHEDULES` | No | Provide a comma separated list of schedules identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those schedules. | +| `HUBOT_PAGERDUTY_SERVICES` | No | Provide a comma separated list of service identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those services. +| `HUBOT_PAGERDUTY_SCHEDULES` | No | Provide a comma separated list of schedules identifiers (e.g. `PFGPBFY,AFBCGH`) to restrict queries to only those schedules. +| `HUBOT_PAGERDUTY_DEFAULT_SCHEDULE` | No | A specific schedule that can be triggered using `pager default trigger` to reduce typing for users. `*` - May be required for certain actions. From 028978e3a3497d3d489bdf85ac252dd23bd94c87 Mon Sep 17 00:00:00 2001 From: John Seekins Date: Tue, 1 Oct 2024 14:50:35 -0700 Subject: [PATCH 11/11] 4.1.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index a31122a..3a7ed6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hubot-pager-me", - "version": "4.0.4", + "version": "4.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hubot-pager-me", - "version": "4.0.4", + "version": "4.1.0", "license": "MIT", "dependencies": { "async": "^3.2.4", diff --git a/package.json b/package.json index 6048600..b9889de 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hubot-pager-me", "description": "PagerDuty integration for Hubot", - "version": "4.0.4", + "version": "4.1.0", "author": "Josh Nichols ", "license": "MIT", "keywords": [