Skip to content

Commit

Permalink
feat: Added integration with slack
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesrabo committed Apr 19, 2019
1 parent ce4c896 commit d541e59
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
39 changes: 39 additions & 0 deletions lib/postMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fetch = require('node-fetch')
const SemanticReleaseError = require('@semantic-release/error')
// require('dotenv').config()

async function postMessage(message) {
const slackHook = process.env.SLACK_WEBHOOK

if (!slackHook) {
throw new SemanticReleaseError(
'Slack web-hook is not defined in environment. Define SLACK_WEBHOOK!'
)
}

await fetch(slackHook, {
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')
throw new SemanticReleaseError(
new Error().stdout,
'INVALID SLACK COMMAND: ' + text
)
}
})
.catch(e => {
throw new SemanticReleaseError(
e.stdout,
'SLACK CONNECTION FAILED: '
)
})
}

module.exports = postMessage
57 changes: 54 additions & 3 deletions lib/success.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
module.exports = (pluginConfig, context) => {
const postMessage = require('./postMessage')

module.exports = async (pluginConfig, context) => {
if (!pluginConfig.notifyOnSuccess) {
return
}

const { logger, nextRelease } = context
logger.log(context.nextRelease)
const { nextRelease, options } = context

let messageBlocks = [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `A new version has been released!\n Current version is *#${
nextRelease.version
}*`
}
}
]

if (nextRelease.notes != '') {
messageBlocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: `*Notes*: ${nextRelease.notes}`
}
})
}

if (options.repositoryUrl.indexOf('git@github.com') != -1) {
const repoPath = options.repositoryUrl.split(':')[1].replace('.git', '')
const repoURL = `https://github.com/${repoPath}`
const gitTag = nextRelease.gitTag

// const divider = {
// type: 'divider'
// }
const metadata = {
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `*<${repoURL}|${repoPath}>:* <${repoURL}/releases/tag/${gitTag}|${gitTag}>`
}
]
}

messageBlocks.push(metadata)
// messageBlocks.push(divider)
}

let slackMessage = {
blocks: messageBlocks
}

await postMessage(slackMessage)
}
12 changes: 12 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.5.0",
"@semantic-release/error": "^2.2.0",
"commitizen": "^3.0.7",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^5.16.0",
Expand All @@ -53,6 +54,7 @@
"eslint-plugin-standard": "^4.0.0",
"husky": "^1.3.1",
"lint-staged": "^8.1.5",
"node-fetch": "^2.3.0",
"prettier": "^1.16.4"
},
"peerDependencies": {
Expand Down

0 comments on commit d541e59

Please sign in to comment.