Move post to slack function to task helper

This commit is contained in:
Blade Barringer
2015-09-12 22:22:38 -05:00
parent fbfd2ef0ea
commit be6acd7c09
3 changed files with 53 additions and 34 deletions

View File

@@ -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",

View File

@@ -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';

View File

@@ -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);
});
}