Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ node_modules

# Users Environment Variables
.lock-wscript

.idea/*
package-lock.json
.idea/vcs.xml
88 changes: 48 additions & 40 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,65 @@
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() {
console.log('Listening on port %d', server.address().port);
});

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"underscore": ">= 1.7.0"
},
"engine": "node 0.10.25"
}
}