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
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var webdep = require("./lib/webdep.js");
var runningScript = process.argv.length > 1 ? process.argv[1] : "";
if (runningScript == __filename) {
webdep.init();
}
else {
} else {
module.exports = webdep;
}
89 changes: 33 additions & 56 deletions lib/incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,73 @@ var path = require("path");
var exec = require('child_process').exec;

exports.control = function(options) {

webdep.clearLog();
return function incoming(req, res, next) {

webdep.clearLog();
webdep.log("Incoming " + (new Date()).toString());

var deploysList = webdep.getDeployList();
var error = false;

var repoData = {};
// github type.
var repoDataStr = req.body.payload;
// gitlab type
if(!repoDataStr) {
for(var i in req.body) {
if(req.body.hasOwnProperty(i)) {
repoDataStr = i;
break;
}
}
}
// parse the post body data.
try {
repoData = JSON.parse(req.body.payload);
repoData = JSON.parse(repoDataStr);
}
catch (err) {
repoData = { error: err };
error = true;
}

deploysList.forEach(function(deploy) {

if (deploy.type == "github") {

if (!repoData.error && !repoData.repository) {
webdep.log("Webhook payload was not for a push.");
return;
}

if (!repoData.error) {

webdep.log("Checking incoming repo " + repoData.repository.url + "...");
if (!repoData.error && !repoData.repository) {
webdep.log("Webhook payload was not for a push.");
}

if (repoData.error) {
webdep.log("");
webdep.log("Error while parsing post body");
} else {
webdep.log('Webhook from', repoData.repository.homepage, '(', repoData.repository.description, ').', 'Checking this incoming repo.');
deploysList.forEach(function(deploy) {
var branch;
if (["github", "gitlab"].indexOf(deploy.type) > -1) {
// remove .git from the repo string, since the hook will not have that address when it comes from github
// so instead of explaining that you shouldnt enter ".git", we just remove it here.
if (repoData.repository.url == deploy.repo.replace(".git$", "")) {

var branch = repoData.ref.split("/").pop();

webdep.log("> Checking branch '" + branch + "' ...");

branch = repoData.ref.split("/").pop();
if (branch == deploy.branch) {

webdep.log("Match branch", branch, ', begin deploy.');
runDeploy(deploy);

}
else {
webdep.log("> No, wrong branch, nothing to do");
webdep.log("> Got:");
webdep.log("> " + branch);
webdep.log("> But expected:");
webdep.log("> " + deploy.branch);
}
}
else {
webdep.log("No, wrong repo, nothing to do");
webdep.log("Got:");
webdep.log(repoData.repository.url);
webdep.log("But expected:");
webdep.log(deploy.repo.replace(".git", ""));
}

}
else {
webdep.log("");
webdep.log("Error while parsing post body");
}
}
});
});
}

if (error) {
res.statusCode = 500;
res.write("ERROR");
}
else {
} else {
res.write("OK");
}
next();




}
};

};


function runDeploy(deploy) {

webdep.log("> Run " + deploy.name + " with branch " + deploy.branch);
webdep.log("> Begin run " + deploy.name + " with branch " + deploy.branch);

var localPath = path.resolve(deploy.basepath);

Expand All @@ -105,10 +84,9 @@ function runDeploy(deploy) {
webdep.log("> ERROR (error): " + error);
}
webdep.log("--------------------------");
webdep.log("> DONE!");
webdep.log("> Deploy done!");
});


webdep.log("--------------------------");
cmd.stdout.on('data', function(data) {
webdep.log(data.toString());
Expand All @@ -128,7 +106,6 @@ function runDeploy(deploy) {
cmd.stderr.on('data', function(data) {
webdep.log(data.toString());
});

}

exports.runDeploy = runDeploy;
Expand Down
56 changes: 22 additions & 34 deletions lib/webdep.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var http = require("http");
var fs = require("fs");
var path = require("path");
var querystring = require("querystring");
var log = logStartText = "The log is empty.";
var logStartText = "The log is empty.";
var log = logStartText;
var Keygrip = require('keygrip');
var Cookies = require('cookies');
var SessionStore = require('./sessionstore');
Expand All @@ -36,16 +37,16 @@ exports.init = function(newOptions, callback) {
// Handle arguments
if (typeof newOptions == "function") {
callback = newOptions;
delete newOptions;
newOptions = {};
}

var key;

// Merge options
for(var key in defaultOptions) {
for(key in defaultOptions) {
options[key] = defaultOptions[key];
}

for(var key in newOptions) {
for(key in newOptions) {
options[key] = newOptions[key];
}

Expand Down Expand Up @@ -100,7 +101,7 @@ exports.init = function(newOptions, callback) {
// extend to have a render function
res.render = function(template, data, status) {
require("./render")(res, template, data, status);
}
};

// Handle requests
var data = "";
Expand Down Expand Up @@ -130,12 +131,13 @@ exports.init = function(newOptions, callback) {
if (match) {
foundRoute = true;
req.params.urlData = match && match.length > 0 ? match[1] : null;
/*jshint -W083*/
route.action(req, res, function() {
res.end();
});
break;
}
};
}

if (!foundRoute) {
res.statusCode = 404;
Expand Down Expand Up @@ -180,14 +182,10 @@ exports.init = function(newOptions, callback) {

// Handler login
socket.on("login", function(e) {


if (e.username == options.username && e.password == options.password) {

// create the session when the user has logged in
socket.request.session.authenticated = true;
sendLoginInfo(socket);

} else {
socket.request.session.authenticated = false;
socket.emit("login-error");
Expand Down Expand Up @@ -218,8 +216,6 @@ exports.init = function(newOptions, callback) {

}



// Start server
server.listen(options.port, function(err) {
if (!err) {
Expand All @@ -245,7 +241,7 @@ exports.init = function(newOptions, callback) {
}
});

}
};

exports.stop = function(callback) {
server.close();
Expand All @@ -257,33 +253,25 @@ exports.clearLog = function() {
};

exports.log = function(){

if (arguments.length == 0) {
if (arguments.length === 0) {
return log;
}

if (log == logStartText) {
log = "";
}

var message = util.format.apply(this, arguments);

if (process.stdout.destroyed !== false || process.stdout.isTTY) {
if (options.logToConsole !== false) {
console.log(message);
}
}
log += message + "\n";

// only send the login to those that are authenticated
/*if (io) {
var clients = io.sockets.clients();
clients.forEach(function(socket) {
if (socket.handshake.session.authenticated === true) {
socket.send("log", {sessionId: socket.id, cache: false, log: log});
}
});
}*/
// broadcast message to all client which is authenticated.
io.sockets.sockets.forEach(function(socket) {
if (socket.request.session && socket.request.session.authenticated === true) {
socket.emit("log", {sessionId: socket.id, cache: false, log: log});
}
});

return exports;

Expand All @@ -296,7 +284,7 @@ function sendLoginInfo(socket) {
var deploysList = getDeployList();

var deploys = [];
deploysList.forEach(function(deploy) {
deploysList.forEach(function(deploy, index) {
var repoUrlDetails = deploy.repo.split("/");
var repoName = repoUrlDetails.pop();
var userName = repoUrlDetails.pop();
Expand All @@ -320,7 +308,7 @@ function cookieSessions(name) {
res.cookie(name, req.session, { signed: true });
});
next();
}
};
}

function getDeployFromId(id) {
Expand Down Expand Up @@ -364,15 +352,15 @@ exports.getDeployList = getDeployList = function() {
}


deploysList.forEach(function(item) {
deploysList.forEach(function(item, index) {
if (!item.id) {
item.id = item.repo.replace(/[\/:]/g, '').toLowerCase();
item.id = '_auto_prefix_index_id_' + index;
}
});

return deploysList;

}
};


// randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webhook-deployer",
"version": "0.7.7",
"version": "0.7.11",
"description": "Deployer server app triggered by (github) webhooks",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,7 +32,7 @@
"grunt-cli": "~0.1.8",
"webdriverjs": "~0.7.3",
"webdriverio": "^2.2.3",
"selenium-standalone": "^2.43.1-5"
"selenium-standalone": "2.43.1-5"
},
"dependencies": {
"commander": "~1.1.1",
Expand Down
2 changes: 1 addition & 1 deletion public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ body{font-family:arial,helvetica;font-size:14px;color:#333;background-color:#fff
table{margin-top:30px;float:left;width:100%;border-collapse:collapse;}
table th{text-align:left;font-weight:normal;color:#aaa;border:1px solid #eee;font-style:italic;padding:5px 20px 5px 5px}
table td{padding:5px 20px 5px 5px;border:1px solid #eee}
.log-container{height:100%;padding:30px;float:left;width:50%;box-sizing:border-box;background-color:#000;color:#0f0;}
.log-container{height:100%;overflow:auto;padding:30px;float:left;width:50%;box-sizing:border-box;background-color:#000;color:#0f0;}
.log-container h2{margin:0;padding:0;opacity:.5}
4 changes: 4 additions & 0 deletions public/css/style.styl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ body

.deploys-box
display none
width 100%
overflow scroll
border 1px solid #eee

.login-box
display none
Expand Down Expand Up @@ -82,6 +85,7 @@ table

.log-container
height 100%
overflow auto
padding 30px
float left
width 60%
Expand Down
Loading