-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #737 from dhis2/feat/slack-webhook
feat(notifications): add slack webhook for new apps
- Loading branch information
Showing
12 changed files
with
12,083 additions
and
12,529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
const { | ||
convertAppToV1AppVersion, | ||
convertAppsToApiV1Format, | ||
getMediaUrl, | ||
} = require('./convertAppsToApiV1Format') | ||
|
||
module.exports = { | ||
convertAppToV1AppVersion: require('./convertAppsToApiV1Format') | ||
.convertAppToV1AppVersion, | ||
convertAppsToApiV1Format: require('./convertAppsToApiV1Format') | ||
.convertAppsToApiV1Format, | ||
convertAppToV1AppVersion, | ||
convertAppsToApiV1Format, | ||
getMediaUrl, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
server/src/services/NotificationService/NotificationMessager.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const messagerTypes = { | ||
WEBHOOK: 'webhook', | ||
email: 'email', | ||
} | ||
|
||
class NotificationMessager { | ||
constructor(name, { type } = {}) { | ||
if (this.constructor === NotificationMessager) { | ||
throw new TypeError( | ||
'Class "NotificationMessager" cannot be instantiated directly.' | ||
) | ||
} | ||
this.name = name | ||
this.type = type | ||
} | ||
|
||
send() { | ||
throw new Error('Method "sendNotification" must be implemented.') | ||
} | ||
} | ||
|
||
module.exports = { | ||
messagerTypes, | ||
NotificationMessager, | ||
} |
72 changes: 72 additions & 0 deletions
72
server/src/services/NotificationService/NotificationService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const Schmervice = require('@hapipal/schmervice') | ||
const { SlackWebhookMessager } = require('./SlackWebhookMessager.js') | ||
|
||
class NotificationService extends Schmervice.Service { | ||
constructor(server, schmerviceOptions, { messagers }) { | ||
super(server, schmerviceOptions) | ||
|
||
if (!messagers || messagers.length < 1) { | ||
server.logger.warn( | ||
'No messagers provided to NotificationService, notifications will not be sent.' | ||
) | ||
} else { | ||
const messagersName = messagers.map((m) => m.name).join(', ') | ||
server.logger.info( | ||
`Init NotificationService with messagers: ${messagersName}` | ||
) | ||
} | ||
|
||
this.messagers = messagers | ||
} | ||
|
||
async sendNewAppNotifications({ | ||
appName, | ||
imageUrl, | ||
link, | ||
organisationName, | ||
sourceUrl, | ||
}) { | ||
const newAppMessagers = this.messagers.filter( | ||
(m) => !!m.sendNewAppNotification | ||
) | ||
if (newAppMessagers.length < 1) { | ||
return Promise.resolve(null) | ||
} | ||
|
||
const promises = newAppMessagers.map((messager) => { | ||
this.server.logger.info( | ||
`Sending new app notification, using messager: ${messager.name}` | ||
) | ||
return messager.sendNewAppNotification({ | ||
appName, | ||
imageUrl, | ||
link, | ||
organisationName, | ||
sourceUrl, | ||
}) | ||
}) | ||
return Promise.all(promises) | ||
} | ||
} | ||
|
||
const createNotificationService = (server, schmerviceOptions) => { | ||
const service = new NotificationService(server, schmerviceOptions, { | ||
messagers: createMessagers(server), | ||
}) | ||
return Schmervice.withName('notificationService', service) | ||
} | ||
|
||
const createMessagers = (server) => { | ||
const messagers = [] | ||
const { config } = server.realm.settings.bind | ||
if (config.slack?.webhookUrl) { | ||
const slackMessager = new SlackWebhookMessager( | ||
'slack', | ||
config.slack.webhookUrl | ||
) | ||
messagers.push(slackMessager) | ||
} | ||
return messagers | ||
} | ||
|
||
module.exports = { NotificationService, createNotificationService } |
98 changes: 98 additions & 0 deletions
98
server/src/services/NotificationService/SlackWebhookMessager.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const { IncomingWebhook } = require('@slack/webhook') | ||
const debug = require('debug')( | ||
'apphub:server:services:NotificationService:SlackWebhookMessager' | ||
) | ||
const { | ||
NotificationMessager, | ||
messagerTypes, | ||
} = require('./NotificationMessager.js') | ||
|
||
class SlackWebhookMessager extends NotificationMessager { | ||
constructor(name, slackWebhookURL) { | ||
super(name, { type: messagerTypes.WEBHOOK }) | ||
this.webhook = new IncomingWebhook(slackWebhookURL) | ||
} | ||
|
||
/** | ||
* @param slackWebHookSendArguments | ||
* see https://github.com/slackapi/node-slack-sdk/blob/3498055d6c86beb82d51ddd583e123461379f5b7/packages/webhook/src/IncomingWebhook.ts#L99 | ||
* shape of { | ||
username?: string; | ||
icon_emoji?: string; | ||
icon_url?: string; | ||
channel?: string; | ||
text?: string; | ||
link_names?: boolean; | ||
agent?: Agent; | ||
timeout?: number; | ||
attachments?: MessageAttachment[]; | ||
blocks?: (KnownBlock | Block)[]; | ||
unfurl_links?: boolean; | ||
unfurl_media?: boolean; | ||
metadata?: { | ||
event_type: string; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
event_payload: Record<string, any>; | ||
} | ||
*/ | ||
async send(slackWebHookSendArguments) { | ||
debug( | ||
'Sending notification', | ||
JSON.stringify(slackWebHookSendArguments, null, 2) | ||
) | ||
return this.webhook.send(slackWebHookSendArguments) | ||
} | ||
|
||
async sendNewAppNotification({ | ||
appName, | ||
organisationName, | ||
imageUrl, | ||
sourceUrl, | ||
link, | ||
}) { | ||
// see https://app.slack.com/block-kit-builder | ||
const blocks = [ | ||
{ | ||
type: 'header', | ||
text: { | ||
type: 'plain_text', | ||
text: 'A new app has been uploaded', | ||
}, | ||
}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*Name:*\n${appName}\n*Organisation:*\n${organisationName}`, | ||
}, | ||
accessory: { | ||
type: 'image', | ||
image_url: imageUrl, | ||
alt_text: 'app logo', | ||
}, | ||
}, | ||
{ | ||
type: 'section', | ||
text: { | ||
type: 'mrkdwn', | ||
text: `*Source code*:\n <${sourceUrl}>`, | ||
}, | ||
accessory: { | ||
type: 'button', | ||
text: { | ||
type: 'plain_text', | ||
text: 'Review now', | ||
}, | ||
url: link, | ||
}, | ||
}, | ||
] | ||
|
||
return this.send({ blocks }) | ||
} | ||
} | ||
|
||
module.exports = { | ||
default: SlackWebhookMessager, | ||
SlackWebhookMessager, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.