Skip to content

Commit

Permalink
Add template support for slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jul 26, 2018
1 parent 1f6003c commit 21fa811
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/commands/notification/slack.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { get } = require('lodash')
const { isNil, get } = require('lodash')
const got = require('got')

const { ward, wardCredential, is } = require('../../ward')
const compile = require('../../compile')

module.exports = ({ config }) => {
const errFn = wardCredential(config, {
Expand All @@ -14,13 +15,22 @@ module.exports = ({ config }) => {

const endpoint = get(config, 'slack.webhook')

const slack = async ({ text, attachments }) => {
ward(text, { label: 'text', test: is.string.nonEmpty })
const { template } = config.slack

const body = JSON.stringify({ text, attachments })
const response = await got(endpoint, { body })
const slack = async (opts, { printLog = true } = {}) => {
const slackOpts = compile({
config,
opts,
pickProps: ['text', 'attachments'],
template: get(template, opts.templateId)
})

return { log: response.body }
ward(slackOpts.from, { label: 'text', test: is.string.nonEmpty })
const { body: log } = await got(endpoint, {
body: JSON.stringify(slackOpts)
})

return { log }
}

return slack
Expand Down
14 changes: 14 additions & 0 deletions src/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const { pick } = require('lodash')
const deepMap = require('deep-map')

const pupa = require('pupa')

const compile = (template, opts) => deepMap(template, str => pupa(str, opts))

module.exports = ({ template, config, opts, pickProps }) => {
const tpl = compile(template, { ...config, props: opts })
// assign props in order to replace props over config
return { ...pick(tpl, pickProps), ...pick(opts, pickProps) }
}

0 comments on commit 21fa811

Please sign in to comment.