mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-01-11 06:15:08 +00:00
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
_ = require('underscore'),
|
|
logger = require('./lib/logger')('http api');
|
|
|
|
/*
|
|
* Pure rest api on pure nodejs follows below
|
|
*/
|
|
module.exports = function(app) {
|
|
return function(req, res) {
|
|
|
|
var projects = app.projects,
|
|
distributor = app.distributor;
|
|
|
|
Steppy(
|
|
function() {
|
|
var stepCallback = this.slot();
|
|
|
|
req.setEncoding('utf-8');
|
|
var bodyString = '';
|
|
req.on('data', function(data) {
|
|
bodyString += data;
|
|
});
|
|
req.on('end', function() {
|
|
var body = JSON.parse(bodyString);
|
|
stepCallback(null, body);
|
|
});
|
|
req.on('error', stepCallback);
|
|
},
|
|
function(err, body) {
|
|
res.statusCode = 404;
|
|
|
|
// run building of a project
|
|
if (req.url === '/api/builds' && req.method === 'POST') {
|
|
var projectName = body.project,
|
|
project = _(projects).findWhere({name: projectName});
|
|
|
|
if (project) {
|
|
res.statusCode = 204;
|
|
logger.log('Run project "%s"', projectName);
|
|
distributor.run({
|
|
projectName: projectName,
|
|
withScmChangesOnly: body.withScmChangesOnly,
|
|
initiator: {type: 'httpApi'}
|
|
});
|
|
}
|
|
|
|
}
|
|
res.end();
|
|
},
|
|
function(err) {
|
|
logger.error('Error occurred during request: ', err.stack || err);
|
|
}
|
|
);
|
|
};
|
|
|
|
};
|