diff --git a/config.json.example b/config.json.example index 161e8a7b1b..75c234ad7f 100644 --- a/config.json.example +++ b/config.json.example @@ -39,10 +39,8 @@ "accessKeyId":"accessKeyId", "secretAccessKey":"secretAccessKey" }, - "TRANSIFEX_SLACK": { - "url": "https://hooks.slack.com/services/some-url", - "channel": "general" - }, + "SLACK_URL": "https://hooks.slack.com/services/some-url", + "TRANSIFEX_SLACK_CHANNEL": "transifex", "PAYPAL":{ "billing_plans": { "basic_earned":"basic_earned", diff --git a/tasks/gulp-transifex-test.js b/tasks/gulp-transifex-test.js index 72b51f54d6..d4e6cdc3c3 100644 --- a/tasks/gulp-transifex-test.js +++ b/tasks/gulp-transifex-test.js @@ -1,20 +1,19 @@ -import fs from 'fs'; -import _ from 'lodash'; -import nconf from 'nconf'; -import gulp from 'gulp'; -import request from 'superagent'; +import fs from 'fs'; +import _ from 'lodash'; +import nconf from 'nconf'; +import gulp from 'gulp'; +import { postToSlack, conf } from './taskHelper'; -nconf.argv().env().file({ file: 'config.json' }); +const SLACK_CONFIG = { + channel: conf.get('TRANSIFEX_SLACK_CHANNEL'), + username: 'Transifex', + emoji: 'transifex' +} const LOCALES = './common/locales/'; const ENGLISH_LOCALE = `${LOCALES}en/`; const ALL_LANGUAGES = getArrayOfLanguages(); -const SLACK_URL = nconf.get('TRANSIFEX_SLACK:url'); -const SLACK_CHANNEL = '#' + (nconf.get('TRANSIFEX_SLACK:channel') || 'general'); -const SLACK_USERNAME = 'Transifex'; -const SLACK_EMOJI = ':transifex:'; - const malformedStringExceptions = { messageDropFood: true, armoireFood: true, @@ -35,7 +34,8 @@ gulp.task('transifex:missingFiles', () => { if (!_.isEmpty(missingStrings)) { let message = 'the following files were missing from the translations folder'; - post(message, missingStrings); + let formattedMessage = formatMessageForPosting(message, missingStrings); + postToSlack(formattedMessage, SLACK_CONFIG); } }); @@ -52,7 +52,8 @@ gulp.task('transifex:missingStrings', () => { if (!_.isEmpty(missingStrings)) { let message = 'The following strings are not translated'; - post(message, missingStrings); + let formattedMessage = formatMessageForPosting(message, missingStrings); + postToSlack(formattedMessage, SLACK_CONFIG); } }); @@ -92,12 +93,14 @@ gulp.task('transifex:malformedStrings', () => { if (!_.isEmpty(stringsWithMalformedInterpolations)) { let message = 'The following strings have malformed or missing interpolations'; - post(message, stringsWithMalformedInterpolations); + let formattedMessage = formatMessageForPosting(message, stringsWithMalformedInterpolations); + postToSlack(formattedMessage, SLACK_CONFIG); } if (!_.isEmpty(stringsWithIncorrectNumberOfInterpolations)) { let message = 'The following strings have a different number of string interpolations'; - post(message, stringsWithIncorrectNumberOfInterpolations); + let formattedMessage = formatMessageForPosting(message, stringsWithIncorrectNumberOfInterpolations); + postToSlack(formattedMessage, SLACK_CONFIG); } }); @@ -138,21 +141,6 @@ function eachTranslationString(languages, cb) { }); } -function post(message, items) { - let formattedMessage = formatMessageForPosting(message, items); - - request.post(SLACK_URL) - .send({ - channel: SLACK_CHANNEL, - username: SLACK_USERNAME, - text: formattedMessage, - icon_emoji: SLACK_EMOJI - }) - .end((err, res) => { - if (err) console.error('Unable to post to slack', err); - }); -} - function formatMessageForPosting(msg, items) { let body = `*Warning:* ${msg}`; body += '\n\n```\n'; diff --git a/tasks/taskHelper.js b/tasks/taskHelper.js index 177b3b9f6f..a303421d0f 100644 --- a/tasks/taskHelper.js +++ b/tasks/taskHelper.js @@ -1,7 +1,15 @@ import { exec } from 'child_process'; import psTree from 'ps-tree'; +import nconf from 'nconf'; import net from 'net'; import Q from 'q'; +import { post } from 'superagent'; + +/* + * Get access to configruable values + */ +nconf.argv().env().file({ file: 'config.json' }); +export var conf = nconf; /* * Kill a child process and any sub-children that process may have spawned. @@ -58,3 +66,28 @@ export function pipe(child) { child.stdout.on('data', (data) => { process.stdout.write(data) }); child.stderr.on('data', (data) => { process.stderr.write(data) }); }; + +/* + * Post request to notify configured slack channel + */ +export function postToSlack(msg, config={}) { + let slackUrl = nconf.get('SLACK_URL'); + + if (!slackUrl) { + console.error('No slack post url specified. Your message was:'); + console.log(msg); + + return; + } + + post(slackUrl) + .send({ + channel: `#${config.channel || '#general'}`, + username: config.username || 'gulp task', + text: msg, + icon_emoji: `:${config.emoji || 'gulp'}:` + }) + .end((err, res) => { + if (err) console.error('Unable to post to slack', err); + }); +}