From 8efc8a2bec1c9c764b01c62a5639e3073ee099d0 Mon Sep 17 00:00:00 2001 From: Matt Harris Date: Fri, 10 Apr 2015 19:13:50 -0700 Subject: [PATCH] Initial test of CI hook --- .gitignore | 3 +++ app.js | 1 + lib/gitlog.js | 2 +- package.json | 3 ++- routes/ci.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 routes/ci.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ddf371 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +node_modules +credentials.js diff --git a/app.js b/app.js index 14a7602..5d725d2 100755 --- a/app.js +++ b/app.js @@ -62,6 +62,7 @@ auth.registerRoutes(app); app.use(require('./routes/index')); app.use(require('./routes/user')); app.use(require('./routes/package')); +app.use(require('./routes/ci')); app.use(function(req, res) { res.status(404); diff --git a/lib/gitlog.js b/lib/gitlog.js index 5857548..ec3a2dc 100644 --- a/lib/gitlog.js +++ b/lib/gitlog.js @@ -7,7 +7,7 @@ var fs = require('fs'); var options = { repo: fs.realpathSync(__dirname + '/../'), number: 20, - fields: [ 'subject', 'authorName', 'authorDateRel', 'authorDate' ] + fields: [ 'subject', 'authorName', 'authorDateRel', 'authorDate', 'hash' ] }; var log = []; diff --git a/package.json b/package.json index 2836882..3d82852 100755 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "serve-favicon": "^2.2.0", "shipping-fedex": "git+https://github.com/MatthaeusHarris/node-shipping-fedex.git", "shipping-ups": "^0.4.26", - "shipping-usps": "0.0.3" + "shipping-usps": "0.0.3", + "node-github": "0.0.3" } } diff --git a/routes/ci.js b/routes/ci.js new file mode 100644 index 0000000..90c6501 --- /dev/null +++ b/routes/ci.js @@ -0,0 +1,45 @@ +/** + * Created by matt on 4/10/15. + */ + +var githubApi = require('node-github'); +var express = require('express'); +var router = express.Router(); +var gitlog = require('../lib/gitlog'); + +var githubInstance = new githubApi({ + version: '3.0.0' +}); + +router.get('/ci/hook/update', function(req, res) { + githubInstance.repos.getCommits({ + user: "MatthaeusHarris", + repo: "packagetracker" + }, function(err, data) { + if (err) { + return res.render('500', err); + } + + if (data[0].sha !== gitlog.log[0].hash) { + // We need to update + child_process.exec('git pull', function(err, stdout, stderr) { + if (err) { + return res.render('500', [err, stdout, stderr].join('\n')); + } + child_process.exec('npm install', function(err, stdout, stderr) { + if (err) { + return res.render('500', [err, stdout, stderr].join('\n')) + } + process.nextTick(function() { + console.log("Process exiting to restart after CI chain trigger"); + process.exit(0); + }); + }); + }); + } else { + res.json({'status': 'ok'}); + } + }); +}); + +module.exports = router;