nci/resources/projects.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
var Steppy = require('twostep').Steppy,
2015-07-09 20:12:24 +00:00
_ = require('underscore'),
helpers = require('./helpers'),
2016-01-06 20:24:41 +00:00
logger = require('../lib/logger')('projects resource');
2015-04-12 22:19:42 +00:00
2015-05-12 20:12:28 +00:00
module.exports = function(app) {
2016-01-06 20:24:41 +00:00
var resource = app.dataio.resource('projects');
2015-05-10 10:04:54 +00:00
2015-05-14 20:37:44 +00:00
resource.use('createBuildDataResource', function(req, res) {
helpers.createBuildDataResource(app, req.data.buildId);
2015-05-14 20:37:44 +00:00
res.send();
});
2015-05-17 13:48:16 +00:00
resource.use('readAll', function(req, res) {
2016-01-05 11:18:20 +00:00
var filteredProjects = app.projects.getAll(),
2015-10-15 22:13:10 +00:00
nameQuery = req.data && req.data.nameQuery;
if (nameQuery) {
2016-01-05 11:18:20 +00:00
filteredProjects = app.projects.filter(function(project) {
2015-10-15 22:13:10 +00:00
return project.name.indexOf(nameQuery) !== -1;
});
}
2016-01-05 11:18:20 +00:00
filteredProjects = _(filteredProjects).sortBy('name');
2015-10-15 22:13:10 +00:00
res.send(filteredProjects);
2015-04-12 22:41:09 +00:00
});
// get project with additional fields
var getProject = function(name, callback) {
var project;
Steppy(
function() {
project = _(app.projects.get(name)).clone();
app.builds.getRecent({
projectName: project.name,
status: 'done',
limit: 10
}, this.slot());
app.builds.getDoneStreak({projectName: project.name}, this.slot());
},
function(err, doneBuilds, doneBuildsStreak) {
project.avgBuildDuration = app.builds.getAvgBuildDuration(doneBuilds);
project.lastDoneBuild = doneBuilds[0];
2015-07-15 07:54:50 +00:00
project.doneBuildsStreak = doneBuildsStreak;
2015-09-26 20:49:03 +00:00
this.pass(project);
},
callback
);
};
// resource custom method which finds project by name
2015-09-26 20:49:03 +00:00
// and emits event about it change to clients
resource.clientEmitSyncChange = function(name) {
2015-09-26 20:49:03 +00:00
Steppy(
function() {
getProject(name, this.slot());
2015-09-26 20:49:03 +00:00
},
function(err, project) {
resource.clientEmitSync('change', {project: project});
2015-09-26 20:49:03 +00:00
},
function(err) {
console.error(
'Error during sync project change occurred:',
err.stack || err
);
}
);
};
resource.use('read', function(req, res) {
Steppy(
function() {
getProject(req.data.name, this.slot());
2015-09-26 20:49:03 +00:00
},
function(err, project) {
res.send(project);
}
);
2015-07-09 20:12:24 +00:00
});
resource.use('run', function(req, res) {
2015-06-13 18:59:32 +00:00
var projectName = req.data.projectName;
2015-07-05 18:03:58 +00:00
logger.log('Run the project: "%s"', projectName);
2016-01-06 20:24:41 +00:00
app.builds.create({
2015-06-14 14:21:04 +00:00
projectName: projectName,
initiator: {type: 'user'},
queueQueued: true
2015-07-05 18:03:58 +00:00
});
res.send();
});
return resource;
2015-04-10 19:23:52 +00:00
};