diff --git a/.gitignore b/.gitignore index 59d842b..410c0a0 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,7 @@ node_modules # Users Environment Variables .lock-wscript + +.idea/* +package-lock.json +.idea/vcs.xml diff --git a/main.js b/main.js index 52be0dc..3d97f51 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,11 @@ -var express = require('express'), +var express = require('express'), http = require('http'), bodyParser = require('body-parser'), settings = require('./settings'), _ = require("underscore"), app = express(); - app.use( bodyParser.json() ); // to support JSON-encoded bodies + app.use(bodyParser.json()); app.use( bodyParser.urlencoded({ extended: true }) ); // to support URL-encoded bodies var server = app.listen(settings.port, function() { @@ -13,45 +13,53 @@ var express = require('express'), }); // handle the post messages - app.post('/deploy/', function (req, res) { - - var data = JSON.parse(req.body.payload); + app.post('/deploy/', function (req, res) { + + var stringify = JSON.stringify(req.body); + var data = JSON.parse(stringify); console.log("Validating if commit is part of monitored scope"); - - var repository = _.find(settings.targets, function(record){ - return record.url == data.repository.absolute_url; - }); - - // found a configured repository to update - if (repository) { - console.log("Repository target found: " + repository.url); - console.log('Detecting branch: ' + repository.branch); - - // look for any branch commits configured. - var matched = _.find(data.commits, function(commit){ - return commit.branch == repository.branch; - }); - - - if (matched) { - console.log("Trigger update!"); - var spawn = require('child_process').spawn, - deploy = spawn('sh', [ 'deploy.sh', repository.folder, repository.branch ], { - cwd: settings.workingfolder - }); - - deploy.stdout.on('data', function (data) { - console.log(''+data); - }); - - deploy.on('close', function (code) { - console.log('Child process exited with code ' + code); - }); - } - else console.log("Don't update!"); - } else console.log("Repository target not found: " + data.repository.absolute_url); - - res.status(200).json({message: 'Bitbucket Hook received!'}); + + var repository = _.find(settings.targets, function(record){ + console.log(" data.repository.full_name", record.url, " origin::", data.repository.full_name); + return record.url === data.repository.full_name; + }); + + // found a configured repository to update + if (repository) { + console.log("Repository target found: " + repository.url); + console.log('Detecting branch: ' + repository.branch); + + // look for any branch commits configured. + var matched = _.find(data.push.changes, function(changes){ + return changes.new.name === repository.branch; + }); + + + if (matched) { + console.log("Trigger update!"); + var spawn = require('child_process').spawn, + deploy = spawn('sh', [ 'deploy.sh', repository.folder, repository.branch ], { + cwd: settings.workingfolder + }); + + deploy.stdout.on('data', function (data) { + console.log(''+data); + }); + + deploy.on('close', function (code) { + console.log('Child process exited with code ' + code); + }); + + res.status(200).json({message: 'Bitbucket Hook received! update!'}); + + } else { + console.log("Don't update!"); + res.status(200).json({message: "Don't update!"}); + } + } else { + console.log("Repository target not found: " + data.repository.full_name); + res.status(200).json({message: "Repository target not found: " + data.repository.full_name}); + } }); diff --git a/package.json b/package.json index dfc7748..3365bcb 100644 --- a/package.json +++ b/package.json @@ -10,4 +10,4 @@ "underscore": ">= 1.7.0" }, "engine": "node 0.10.25" -} \ No newline at end of file +}