This repository has been archived by the owner on Aug 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #54 from gregorylegarec/feat/mattermost-js
Rewrite mattermost post hook in full NodeJS
- Loading branch information
Showing
4 changed files
with
119 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Registry channels | ||
const DEV = 'dev' | ||
const BETA = 'beta' | ||
const STABLE = 'stable' | ||
|
||
const ENV_TARGETS = { | ||
[DEV]: process.env.TARGETS_DEV || '', | ||
[BETA]: process.env.TARGETS_BETA || '', | ||
[STABLE]: (process.env.TARGETS_STABLE || '').concat( | ||
process.env.TARGETS_BETA || '' | ||
) | ||
} | ||
|
||
const getChannel = version => { | ||
if (!version) throw new Error('App version is not specified') | ||
if (version.includes('-dev.')) return DEV | ||
if (version.includes('-beta.')) return BETA | ||
if (version.match(/\d+\.\d+\.\d+/)) return STABLE | ||
throw new Error('Unrecognized version channel') | ||
} | ||
|
||
const getEnvInstances = channel => | ||
ENV_TARGETS[channel] && ENV_TARGETS[channel].replace(' ', '').split(',') | ||
|
||
const getInstanceDomain = instance => { | ||
return instance | ||
.split('.') | ||
.slice(1) | ||
.join('.') | ||
} | ||
|
||
module.exports = { | ||
getChannel, | ||
getEnvInstances, | ||
getInstanceDomain | ||
} |
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,19 +1,86 @@ | ||
const spawn = require('cross-spawn') | ||
const https = require('https') | ||
const hookHelpers = require('../helpers') | ||
const url = require('url') | ||
|
||
const MATTERMOST_CHANNEL = process.env.MATTERMOST_CHANNEL | ||
const MATTERMOST_HOOK_URL = process.env.MATTERMOST_HOOK_URL | ||
const MATTERMOST_ICON = | ||
'https://travis-ci.com/images/logos/TravisCI-Mascot-1.png' | ||
const MATTERMOST_USERNAME = 'Travis' | ||
|
||
const sendMattermostReleaseMessage = (appSlug, appVersion, instances) => { | ||
let message = `__${appSlug}__ version \`${appVersion}\` has been published` | ||
|
||
if (instances.length) { | ||
message = `${message} and deployed on following instances: ${instances | ||
.map(instance => `[${instance}](http://${instance})`) | ||
.join(', ')}.` | ||
} else { | ||
message = `${message}.` | ||
} | ||
|
||
const mattermostHookUrl = url.parse(MATTERMOST_HOOK_URL) | ||
|
||
return new Promise((resolve, reject) => { | ||
console.log( | ||
`↳ ℹ️ Sending to mattermost channel ${MATTERMOST_CHANNEL} the following message: "${message}"` | ||
) | ||
const postData = `payload=${JSON.stringify({ | ||
channel: MATTERMOST_CHANNEL, | ||
icon_url: MATTERMOST_ICON, | ||
username: MATTERMOST_USERNAME, | ||
text: message | ||
})}` | ||
|
||
const request = https.request( | ||
{ | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': Buffer.byteLength(postData) | ||
}, | ||
hostname: mattermostHookUrl.hostname, | ||
method: 'post', | ||
path: mattermostHookUrl.pathname | ||
}, | ||
res => { | ||
if (res.statusCode === 200) { | ||
resolve(res) | ||
} else { | ||
reject( | ||
new Error( | ||
`Mattermost message sending failed (error status: ${ | ||
res.statusCode | ||
}).` | ||
) | ||
) | ||
} | ||
} | ||
) | ||
|
||
request.on('error', error => reject(error)) | ||
request.write(postData) | ||
request.end() | ||
}) | ||
} | ||
|
||
module.exports = async options => { | ||
console.log('↳ ℹ️ Sending message to Mattermost') | ||
|
||
if (!MATTERMOST_CHANNEL) { | ||
throw new Error('No MATTERMOST_CHANNEL environment variable defined') | ||
} | ||
|
||
if (!MATTERMOST_HOOK_URL) { | ||
throw new Error('No MATTERMOST_HOOK_URL environment variable defined') | ||
} | ||
|
||
const { appSlug, appVersion } = options | ||
|
||
const mattermostProcess = spawn.sync( | ||
'sh', | ||
['-c', `${__dirname}/mattermost.sh ${appSlug} ${appVersion}`], | ||
{ | ||
stdio: 'inherit' | ||
} | ||
const instances = hookHelpers.getEnvInstances( | ||
hookHelpers.getChannel(appVersion) | ||
) | ||
|
||
if (mattermostProcess.status !== 0) { | ||
throw new Error(`${mattermostProcess.stderr}`) | ||
} | ||
sendMattermostReleaseMessage(appSlug, appVersion, instances) | ||
|
||
return options | ||
} |
This file was deleted.
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