Skip to content

Commit

Permalink
Add latest commit info reachable through API
Browse files Browse the repository at this point in the history
After call to /api/peers/version return build info if lisk was installed from binaries or last commit if installed using source.

closes LiskArchive#340
  • Loading branch information
MaciejBaj committed Jan 25, 2017
1 parent 6ec3140 commit b87efb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
13 changes: 13 additions & 0 deletions helpers/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
var childProcess = require('child_process');

/**
@throws Will throw an error if the git is not installed or not a git repository.
*/
function getLastCommit() {
return childProcess.execSync('git rev-parse HEAD').toString().trim();
}

module.exports = {
getLastCommit: getLastCommit
};
10 changes: 9 additions & 1 deletion modules/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var constants = require('../helpers/constants.js');
var extend = require('extend');
var fs = require('fs');
var ip = require('ip');
var git = require('../helpers/git.js');
var OrderBy = require('../helpers/orderBy.js');
var path = require('path');
var Peer = require('../logic/peer.js');
Expand Down Expand Up @@ -544,7 +545,14 @@ shared.getPeer = function (req, cb) {
};

shared.version = function (req, cb) {
return setImmediate(cb, null, {version: library.config.version, build: library.build});
try {
var response = library.build ? {build: library} : {commit: git.getLastCommit()};
response.version = library.config.version;
return setImmediate(cb, null, response);
}
catch (err) {
return setImmediate(cb, 'Git is not installed or not a git repository');
}
};

// Export
Expand Down
4 changes: 2 additions & 2 deletions test/api/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var node = require('./../node.js');

describe('GET /api/peers/version', function () {

it('should be ok', function (done) {
it('should be working with sources', function (done) {
node.get('/api/peers/version', function (err, res) {
node.expect(res.body).to.have.property('success').to.be.ok;
node.expect(res.body).to.have.property('build').to.be.a('string');
node.expect(res.body).to.have.property('commit').to.be.a('string');
node.expect(res.body).to.have.property('version').to.be.a('string');
done();
});
Expand Down

0 comments on commit b87efb2

Please sign in to comment.