-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify code and add tests (#22)
- Loading branch information
1 parent
bc373f9
commit cff4304
Showing
9 changed files
with
319 additions
and
72 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
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') | ||
} | ||
} |
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,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)}*[...]*` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.