From a8d3056eb6df6b36dcae24cf6e4e578d618ca0b9 Mon Sep 17 00:00:00 2001 From: oleg Date: Sat, 11 Jul 2015 13:41:01 +0300 Subject: [PATCH] introduce build with scm changes only feature --- httpApi/index.js | 3 ++- lib/distributor.js | 16 ++++++++++++++++ lib/executor/base.js | 5 +++++ lib/executor/local.js | 32 +++++++++++++++++++++++++++++--- lib/node.js | 4 ++++ test/executor.js | 25 +++++++++++++++++++++---- 6 files changed, 77 insertions(+), 8 deletions(-) diff --git a/httpApi/index.js b/httpApi/index.js index 8615b65..6800a27 100644 --- a/httpApi/index.js +++ b/httpApi/index.js @@ -42,8 +42,9 @@ exports.register = function(app) { logger.log('Run "' + projectName + '"'); distributor.run({ projectName: projectName, + withScmChangesOnly: body.withScmChangesOnly, initiator: {type: 'httpApi'} - }, _.noop); + }); } } diff --git a/lib/distributor.js b/lib/distributor.js index 15456a0..02da7c8 100644 --- a/lib/distributor.js +++ b/lib/distributor.js @@ -216,6 +216,22 @@ Distributor.prototype.run = function(params, callback) { Steppy( function() { project = _(self.projects).findWhere({name: params.projectName}); + + if (params.withScmChangesOnly) { + self.nodes[0].hasScmChanges(project, this.slot()); + } else { + this.pass(null); + } + }, + function(err, hasScmChanges) { + if (params.withScmChangesOnly && !hasScmChanges) { + logger.log( + 'Building of "%s" skipped coz no scm changes', + project.name + ); + return callback(); + } + self._updateBuild({}, { project: project, initiator: params.initiator, diff --git a/lib/executor/base.js b/lib/executor/base.js index 5a022aa..ddc6065 100644 --- a/lib/executor/base.js +++ b/lib/executor/base.js @@ -26,6 +26,11 @@ Executor.prototype._runStep = function(params, callback) { }; +// Does current project scm has new changes to build +Executor.prototype.hasScmChanges = function(callback) { + +}; + Executor.prototype.run = function(params, callback) { var self = this, project = _({}).extend(self.project, params); diff --git a/lib/executor/local.js b/lib/executor/local.js index a4be219..9696fbc 100644 --- a/lib/executor/local.js +++ b/lib/executor/local.js @@ -20,7 +20,7 @@ Executor.prototype._createScm = function(params) { return createScm(params); }; -Executor.prototype._getSources = function(params, callback) { +Executor.prototype._getChanges = function(params, callback) { var self = this, scm, isFirstRun, oldRev; Steppy( @@ -65,8 +65,34 @@ Executor.prototype._getSources = function(params, callback) { }, function(err, changes) { var target = self._getTarget(params.rev, changes); - this.pass(target.changes); - scm.update(target.rev, this.slot()); + this.pass({ + scm: scm, + oldRev: oldRev, + rev: target.rev, + changes: target.changes + }); + }, + callback + ); +}; + +Executor.prototype.hasScmChanges = function(callback) { + this._getChanges(this.project.scm, function(err, data) { + callback(err, !err && data.changes.length > 0); + }); +}; + +Executor.prototype._getSources = function(params, callback) { + var self = this, + scm; + Steppy( + function() { + self._getChanges(params, this.slot()); + }, + function(err, data) { + scm = data.scm; + this.pass(data.changes); + scm.update(data.rev, this.slot()); }, function(err, changes) { scm.getCurrent(this.slot()); diff --git a/lib/node.js b/lib/node.js index 6da29ff..0238375 100644 --- a/lib/node.js +++ b/lib/node.js @@ -83,6 +83,10 @@ Node.prototype._createExecutor = function(project) { }); }; +Node.prototype.hasScmChanges = function(project, callback) { + this._createExecutor(project).hasScmChanges(callback); +}; + Node.prototype.run = function(project, params, callback) { var self = this; diff --git a/test/executor.js b/test/executor.js index ae622fb..bec2423 100644 --- a/test/executor.js +++ b/test/executor.js @@ -46,10 +46,7 @@ var expect = require('expect.js'), }); it('should run without errors', function(done) { - executor.run({}, function(err) { - expect(err).not.ok(); - done(); - }); + executor.run({}, done); executor.on('scmData', function(data) { scmData = data; }); @@ -67,6 +64,18 @@ var expect = require('expect.js'), ); }); + var itHasScmChanges = function(value) { + it((value ? 'should' : 'should`t') + ' has scm changes', + function(done) { + executor.hasScmChanges(function(err, hasScmChanges) { + if (err) return done(err); + expect(hasScmChanges).equal(value); + done(); + }); + } + ); + }; + _(['first revision', /^first revision$/]).each(function(comment) { describe('with scm rev default and catch rev ' + comment, function() { @@ -99,6 +108,8 @@ var expect = require('expect.js'), }); }); + itHasScmChanges(true); + it('should run it again without errors', function(done) { executor.run({}, done); }); @@ -113,6 +124,8 @@ var expect = require('expect.js'), }); } ); + + itHasScmChanges(false); }); }); @@ -140,6 +153,8 @@ var expect = require('expect.js'), }); }); + itHasScmChanges(true); + it('scm data should be rev: 2, changes: [0, 2], not latest', function() { expect(scmData).eql({ @@ -164,6 +179,8 @@ var expect = require('expect.js'), } ); + itHasScmChanges(false); + }); });