Skip to content
This repository has been archived by the owner on Aug 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #54 from gregorylegarec/feat/mattermost-js
Browse files Browse the repository at this point in the history
Rewrite mattermost post hook in full NodeJS
  • Loading branch information
gregorylegarec committed Aug 1, 2018
2 parents 751b561 + 683a28e commit 5b61294
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 98 deletions.
36 changes: 36 additions & 0 deletions lib/hooks/helpers.js
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
}
87 changes: 77 additions & 10 deletions lib/hooks/post/mattermost.js
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
}
50 changes: 0 additions & 50 deletions lib/hooks/post/mattermost.sh

This file was deleted.

44 changes: 6 additions & 38 deletions lib/hooks/post/rundeck.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
const https = require('https')
const hookHelpers = require('../helpers')

const RUNDECK_TOKEN = process.env.RUNDECK_TOKEN

// Registry channels
const DEV = 'dev'
const BETA = 'beta'
const STABLE = 'stable'

const getChannel = version => {
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 getInstanceDomain = instance => {
return instance
.split('.')
.slice(1)
.join('.')
}

const ENV_TARGETS = {
[DEV]: process.env.TARGETS_DEV || [],
[BETA]: (process.env.TARGETS_BETA || []).concat(
process.env.TARGETS_DEV || []
),
[STABLE]: (process.env.TARGETS_STABLE || [])
.concat(process.env.TARGETS_BETA || [])
.concat(process.env.TARGETS_DEV || [])
}

const RUNDECK_JOBS = {
'cozy.works': '87a668f7-eff1-422d-aa84-92b3bcc62c8f',
'cozy.rocks': 'ad27f2f6-63d9-4a16-ab62-e25c957875b5',
Expand All @@ -39,7 +11,7 @@ const RUNDECK_JOBS = {

const runRundeckJob = (token, instance, slug) =>
new Promise((resolve, reject) => {
const job = RUNDECK_JOBS[getInstanceDomain(instance)]
const job = RUNDECK_JOBS[hookHelpers.getInstanceDomain(instance)]

if (!job) {
console.log(`↳ ⚠️ No rundeck job available for ${instance}`)
Expand Down Expand Up @@ -81,22 +53,18 @@ module.exports = async options => {
const { appSlug, appVersion } = options

// Check if version is dev, beta, or stable.
const channel = getChannel(appVersion)
const targets = ENV_TARGETS[channel] && ENV_TARGETS[channel].replace(' ', '')
const channel = hookHelpers.getChannel(appVersion)
const instances = hookHelpers.getEnvInstances(channel)

if (!targets) {
if (!instances) {
console.log(`↳ ℹ️ No instance to upgrade for channel ${channel}`)
return options
}

console.log(
`↳ ℹ️ Updating channel ${channel} in instances ${targets.replace(
',',
', '
)}`
`↳ ℹ️ Updating channel ${channel} in instances ${instances.join(', ')}`
)

const instances = targets.split(',')
const failedDeployments = []

for (const instance of instances) {
Expand Down

0 comments on commit 5b61294

Please sign in to comment.