From c1215735eeb9770d66b502c927904a673d044460 Mon Sep 17 00:00:00 2001 From: Yulian andres zapata m Date: Thu, 22 Nov 2018 17:37:01 -0500 Subject: [PATCH 1/2] update of the manipulation of the json object that comes from bitbucket --- .gitignore | 3 ++ .idea/vcs.xml | 6 ++++ main.js | 88 ++++++++++++++++++++++++++++----------------------- package.json | 2 +- 4 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 59d842b..948f2b7 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ node_modules # Users Environment Variables .lock-wscript + +.idea/* +package-lock.json diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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 +} From 9001291f8c59c56e7025afbcd5dfe26793a5bf6b Mon Sep 17 00:00:00 2001 From: Yulian andres zapata m Date: Thu, 22 Nov 2018 18:03:30 -0500 Subject: [PATCH 2/2] code cleaning --- .gitignore | 1 + .idea/vcs.xml | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 948f2b7..410c0a0 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ node_modules .idea/* package-lock.json +.idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file