SlackNotificationManager.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const logger = require('@overleaf/logger')
  2. const Settings = require('@overleaf/settings')
  3. const { IncomingWebhook } = require('@slack/webhook')
  4. const moment = require('moment')
  5. const SplitTestUtils = require('./SplitTestUtils')
  6. async function sendNotification(splitTest, action, user) {
  7. const lastVersion = SplitTestUtils.getCurrentVersion(splitTest)
  8. const url = lastVersion.analyticsEnabled
  9. ? Settings.splitTest.notification.splitTestSlackWebhookUrl
  10. : Settings.splitTest.notification.gradualRolloutSlackWebhookUrl
  11. if (!url) {
  12. logger.info('Skipping slack notification as webhook URL is not configured')
  13. return
  14. }
  15. const webhook = new IncomingWebhook(url)
  16. const defaultRolloutPercent =
  17. 100 -
  18. lastVersion.variants.reduce(
  19. (total, variant) => total + variant.rolloutPercent,
  20. 0
  21. )
  22. const variantsConfig = [
  23. `- default: ${defaultRolloutPercent}%`,
  24. ...lastVersion.variants.map(
  25. variant => `- ${variant.name}: ${variant.rolloutPercent}%`
  26. ),
  27. ].join('\n')
  28. const date = splitTest.archived ? splitTest.archivedAt : lastVersion.createdAt
  29. const comment =
  30. action !== 'archived' && lastVersion.comment
  31. ? `with comment: ${lastVersion.comment}`
  32. : ''
  33. const payload = {
  34. name: splitTest.name,
  35. action,
  36. phase: lastVersion.phase,
  37. description: splitTest.description,
  38. ticketURL: splitTest.ticketUrl,
  39. variantsConfig,
  40. active: lastVersion.active.toString(),
  41. author: user.email,
  42. date: moment(date).utc().format('Do MMM YYYY, h:mm a') + ' UTC',
  43. comment,
  44. versionNumber: `${lastVersion.versionNumber}`,
  45. url: `${Settings.siteUrl}/admin/split-test/edit/${splitTest.name}`,
  46. }
  47. try {
  48. const { send: sendPayload } = webhook // workaround for the lint_flag_res_send_usage rule false-positive
  49. await sendPayload.call(webhook, payload)
  50. } catch (err) {
  51. logger.error(
  52. { err },
  53. 'Failed to notify split test notifications Slack webhook'
  54. )
  55. }
  56. }
  57. module.exports = {
  58. sendNotification,
  59. }