diff --git a/index.js b/index.js index b5c3fed..a3f8f0e 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/lib/incoming.js b/lib/incoming.js index 9bdb58f..9db4fe5 100644 --- a/lib/incoming.js +++ b/lib/incoming.js @@ -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); @@ -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()); @@ -128,7 +106,6 @@ function runDeploy(deploy) { cmd.stderr.on('data', function(data) { webdep.log(data.toString()); }); - } exports.runDeploy = runDeploy; diff --git a/lib/webdep.js b/lib/webdep.js index 63bfdd8..5b51bc2 100644 --- a/lib/webdep.js +++ b/lib/webdep.js @@ -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'); @@ -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]; } @@ -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 = ""; @@ -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; @@ -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"); @@ -218,8 +216,6 @@ exports.init = function(newOptions, callback) { } - - // Start server server.listen(options.port, function(err) { if (!err) { @@ -245,7 +241,7 @@ exports.init = function(newOptions, callback) { } }); -} +}; exports.stop = function(callback) { server.close(); @@ -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; @@ -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(); @@ -320,7 +308,7 @@ function cookieSessions(name) { res.cookie(name, req.session, { signed: true }); }); next(); - } + }; } function getDeployFromId(id) { @@ -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 diff --git a/package.json b/package.json index 8616ae6..7254297 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/public/css/style.css b/public/css/style.css index 0e04e18..150c2f7 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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} diff --git a/public/css/style.styl b/public/css/style.styl index 54a7932..78c73cf 100644 --- a/public/css/style.styl +++ b/public/css/style.styl @@ -33,6 +33,9 @@ body .deploys-box display none + width 100% + overflow scroll + border 1px solid #eee .login-box display none @@ -82,6 +85,7 @@ table .log-container height 100% + overflow auto padding 30px float left width 60% diff --git a/public/js/main.js b/public/js/main.js index b25974f..4e3c392 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,20 +1,26 @@ (function() { var socket = io(); + var template = '{{#deploys}}
Your auto-deploy companion
-| - | repo | -branch | +Name | +Commane | +Repo | +Branch |
|---|---|---|---|---|---|---|
| + | ||||||
| + | {{deploy.name}} | +{{deploy.command}} | {{deploy.repo}} | {{deploy.branch}} |
{{log}}