Skip to content

Commit

Permalink
fix: simplify code and add tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil authored and juliuscc committed Nov 6, 2019
1 parent bc373f9 commit cff4304
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 72 deletions.
13 changes: 7 additions & 6 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ const postMessage = require('./postMessage')
const template = require('./template')

module.exports = async (pluginConfig, context) => {
const { logger, options, errors } = context
const { npm_package_name } = context.env
const {
logger,
options,
errors,
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context
const { slackWebhook = process.env.SLACK_WEBHOOK } = pluginConfig

let package_name = context.env.SEMANTIC_RELEASE_PACKAGE
if (!package_name) package_name = context.env.npm_package_name
const package_name = SEMANTIC_RELEASE_PACKAGE || npm_package_name

if (!pluginConfig.notifyOnFail) {
logger.log('Notifying on fail skipped')
Expand Down Expand Up @@ -66,8 +69,6 @@ module.exports = async (pluginConfig, context) => {
messageBlocks.push(metadata)
}

// messageBlocks.push(divider)

const attachments = [
{
type: 'section',
Expand Down
44 changes: 19 additions & 25 deletions lib/postMessage.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
const fetch = require('node-fetch')
const SemanticReleaseError = require('@semantic-release/error')

async function postMessage(message, logger, slackWebhook) {
await fetch(slackWebhook, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(message)
})
.then(res => res.text())
.then(text => {
if (text !== 'ok') {
logger.log('JSON message format invalid: ' + text)
throw new SemanticReleaseError(
new Error().stdout,
'INVALID SLACK COMMAND: ' + text
)
}
module.exports = async (message, logger, slackWebhook) => {
let response
let bodyText
try {
response = await fetch(slackWebhook, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(message)
})
.catch(e => {
throw new SemanticReleaseError(
e.stdout,
'SLACK CONNECTION FAILED: '
)
})
}
bodyText = await response.text()
} catch (e) {
throw new SemanticReleaseError(e.message, 'SLACK CONNECTION FAILED')
}

module.exports = postMessage
if (!response.ok || bodyText !== 'ok') {
logger.log('JSON message format invalid: ' + bodyText)
throw new SemanticReleaseError(bodyText, 'INVALID SLACK COMMAND')
}
}
10 changes: 7 additions & 3 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const truncate = require('./truncate')
const MAX_LENGTH = 2900

module.exports = async (pluginConfig, context) => {
const { logger, nextRelease, options } = context
const {
logger,
nextRelease,
options,
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context
const { slackWebhook = process.env.SLACK_WEBHOOK } = pluginConfig

let package_name = context.env.SEMANTIC_RELEASE_PACKAGE
if (!package_name) package_name = context.env.npm_package_name
const package_name = SEMANTIC_RELEASE_PACKAGE || npm_package_name

if (!pluginConfig.notifyOnSuccess) {
logger.log('Notifying on success skipped')
Expand Down
47 changes: 22 additions & 25 deletions lib/template.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
function template(input, variables) {
const type = typeof input
if (type === 'string') {
return Object.keys(variables).reduce(
(output, variable) =>
variables[variable]
? output.replace(`$${variable}`, variables[variable])
: output,
input
)
} else if (type === 'object') {
if (Array.isArray(input)) {
const out = []
for (const value of input) {
out.push(template(value, variables))
switch (typeof input) {
case 'string':
return Object.keys(variables).reduce(
(output, variable) =>
variables[variable]
? output.replace(`$${variable}`, variables[variable])
: output,
input
)
case 'object':
if (Array.isArray(input)) {
return input.map(value => template(value, variables))
} else {
return Object.entries(input).reduce(
(out, [key, value]) => ({
...out,
[key]: template(value, variables)
}),
{}
)
}
return out
} else {
const out = {}
for (let key of Object.keys(input)) {
const value = input[key]
out[key] = template(value, variables)
}
return out
}
} else {
return input
default:
return input
}
}

Expand Down
22 changes: 11 additions & 11 deletions lib/truncate.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = (messageText, maxLength) => {
if (messageText.length <= maxLength) return messageText

const delimiter = '\n'
if (messageText.length > maxLength) {
messageText = messageText.substring(0, maxLength).split(delimiter)
// if no newlines, we don't remove anything, we keep the truncated message as is
if (messageText.length > 1) {
// remove all text after the last newline in the truncated message
// this avoids truncating in the middle of markdown
messageText.splice(-1, 1)
}
messageText = messageText.join(delimiter) + '*[...]*'
}
return messageText
// split the truncated message into the
// first element and an array with the rest
const [firstLine, ...restLines] = messageText
.substring(0, maxLength)
.split(delimiter)
// if the array restLines is not empty, remove the last element
const truncatedLines = [firstLine, ...restLines.slice(0, -1)]

return `${truncatedLines.join(delimiter)}*[...]*`
}
90 changes: 90 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
"eslint-plugin-standard": "^4.0.0",
"husky": "^1.3.1",
"lint-staged": "^8.1.5",
"mocha": "^6.2.2",
"nock": "^11.7.0",
"prettier": "^1.16.4",
"semantic-release": "^15.13.3",
"mocha": "^6.2.2"
"semantic-release": "^15.13.3"
},
"peerDependencies": {
"semantic-release": ">=11.0.0 <16.0.0"
Expand Down
53 changes: 53 additions & 0 deletions test/test_postMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const assert = require('assert')
const nock = require('nock')
const postMessage = require('../lib/postMessage')
const SemanticReleaseError = require('@semantic-release/error')

const slackWebhook = 'https://www.webhook.com'

async function post(url) {
url = url || slackWebhook
await postMessage('message', { log: () => undefined }, url)
}

describe('test postMessage', () => {
it('should pass if response is 200 "ok"', async () => {
nock(slackWebhook)
.post('/')
.reply(200, 'ok')
assert.ifError(await post())
})

it('should fail if response text is not "ok"', async () => {
const response = 'not ok'
nock(slackWebhook)
.post('/')
.reply(200, response)
await assert.rejects(
post(),
new SemanticReleaseError(response, 'INVALID SLACK COMMAND')
)
})

it('should fail if response status code is not 200', async () => {
const response = 'error message'
nock(slackWebhook)
.post('/')
.reply(500, response)
await assert.rejects(
post(),
new SemanticReleaseError(response, 'INVALID SLACK COMMAND')
)
})

it('should fail if incorrect url', async () => {
const incorrectUrl = 'https://sekhfskdfdjksfkjdhfsd.com'
await assert.rejects(post(incorrectUrl), {
name: 'SemanticReleaseError',
code: 'SLACK CONNECTION FAILED',
details: undefined,
message: /ENOTFOUND/,
semanticRelease: true
})
})
})
Loading

0 comments on commit cff4304

Please sign in to comment.