Skip to content

Instantly share code, notes, and snippets.

@ojame
Created October 14, 2015 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ojame/2c2eec0195a91bed1869 to your computer and use it in GitHub Desktop.
Save ojame/2c2eec0195a91bed1869 to your computer and use it in GitHub Desktop.
function createGifPostToSlack(fs, request, createImageSizeStream, easyimg, GIFEncoder, pngFileStream, browserString, browserName) {
var imageWidth;
var imageHeight;
var screenshotsPath = 'tests/screenshots/'+browserName+'/';
var resultPath = screenshotsPath+'result.gif';
var resultPathCompressed = screenshotsPath+'result_compressed.gif';
var stream;
var makeGif = function() {
var encodeGif = function(width, height) {
var encoder = new GIFEncoder(width, height);
pngFileStream(screenshotsPath+'?.png')
.on('end', function() {
easyimg.resize({src: resultPath, dst: resultPathCompressed, width: imageWidth/2, quality:10}).then(function(file) {
uploadFileToSlack(file);
});
})
.pipe(encoder.createWriteStream({ repeat: 0, delay: 500, quality: 10 }))
.pipe(fs.createWriteStream(resultPath));
};
encodeGif(imageWidth, imageHeight);
};
var uploadFileToSlack = function(file) {
var r = request.post('https://slack.com/api/files.upload', function optionalCallback (err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
if (process.env.TRAVIS_JOB_ID) {
sendSlackMessage('#' + process.env.TRAVIS_JOB_ID + ' in ' + browserString + ': ' + JSON.parse(body).file.url);
} else {
sendSlackMessage((browserString) + ' ' + JSON.parse(body).file.url);
}
});
var form = r.form();
form.append('token', 'xoxp-2188701673-2236842709-2545311152-1e043e');
form.append('channels', '#dev_stack');
form.append('file', fs.createReadStream(resultPathCompressed));
};
var sendSlackMessage = function(message) {
var webhook = (!process.env.TRAVIS_BRANCH || process.env.TRAVIS_BRANCH === 'test') ? 'https://bugherd.slack.com/services/hooks/incoming-webhook?token=wOrZWGjX2QONVT4vCdEBpDZh' : 'https://bugherd.slack.com/services/hooks/incoming-webhook?token=UeWCTRKhpJXRJv8IvI6rnAsw';
var payload = {
channel: (!process.env.TRAVIS_BRANCH || process.env.TRAVIS_BRANCH === 'test') ? 'testing' : '#dev_stack',
username: 'Travis',
text: message,
icon_url: 'http://i.imgur.com/2GOrjFe.png',
markdown: true
};
request.post({
url: webhook,
body: JSON.stringify(payload)
}, function(e,r,body) {
deleteFolderRecursive(screenshotsPath);
if (body !== 'ok') {
console.log('Could not post message to Slack');
}
});
};
var imageSizeStream = createImageSizeStream().on('size', function(dimensions) {
imageWidth = dimensions.width;
imageHeight = dimensions.height;
console.log('width: ' + imageWidth)
console.log('height: ' + imageHeight)
makeGif();
stream.destroy();
});
var startProcess = function() {
fs.exists(screenshotsPath+'0.png', function(exists) {
if (exists) {
stream = fs.createReadStream(screenshotsPath+'0.png').pipe(imageSizeStream);
} else {
console.log('No screenshots were taken');
}
});
};
startProcess();
function serialize(obj) {
var str = "";
for (var key in obj) {
if (str !== "") {
str += "&";
}
str += key + "=" + obj[key];
}
return str;
}
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment