2015-04-10 19:23:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-09 19:53:19 +00:00
|
|
|
var Steppy = require('twostep').Steppy,
|
2015-07-09 20:12:24 +00:00
|
|
|
_ = require('underscore'),
|
2016-01-07 19:02:59 +00:00
|
|
|
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) {
|
2016-01-07 19:02:59 +00:00
|
|
|
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
|
|
|
});
|
2015-05-01 11:11:29 +00:00
|
|
|
|
2016-01-07 19:02:59 +00:00
|
|
|
// get project with additional fields
|
2016-01-06 18:14:25 +00:00
|
|
|
var getProject = function(name, callback) {
|
2015-07-12 20:24:09 +00:00
|
|
|
var project;
|
|
|
|
Steppy(
|
|
|
|
function() {
|
2016-01-07 19:02:59 +00:00
|
|
|
project = _(app.projects.get(name)).clone();
|
2015-07-12 20:24:09 +00:00
|
|
|
|
2016-01-07 08:57:45 +00:00
|
|
|
app.builds.getRecent({
|
|
|
|
projectName: project.name,
|
|
|
|
status: 'done',
|
2016-01-07 19:02:59 +00:00
|
|
|
limit: 10
|
2015-07-12 20:24:09 +00:00
|
|
|
}, this.slot());
|
|
|
|
|
2016-01-07 08:57:45 +00:00
|
|
|
app.builds.getDoneStreak({projectName: project.name}, this.slot());
|
2015-07-12 20:24:09 +00:00
|
|
|
},
|
2016-01-07 19:02:59 +00:00
|
|
|
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-07-12 20:24:09 +00:00
|
|
|
|
2015-09-26 20:49:03 +00:00
|
|
|
this.pass(project);
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-01-06 18:14:25 +00:00
|
|
|
// resource custom method which finds project by name
|
2015-09-26 20:49:03 +00:00
|
|
|
// and emits event about it change to clients
|
2016-01-06 18:14:25 +00:00
|
|
|
resource.clientEmitSyncChange = function(name) {
|
2015-09-26 20:49:03 +00:00
|
|
|
Steppy(
|
|
|
|
function() {
|
2016-01-06 18:14:25 +00:00
|
|
|
getProject(name, this.slot());
|
2015-09-26 20:49:03 +00:00
|
|
|
},
|
|
|
|
function(err, project) {
|
2015-09-27 07:57:53 +00:00
|
|
|
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() {
|
2016-01-06 18:14:25 +00:00
|
|
|
getProject(req.data.name, this.slot());
|
2015-09-26 20:49:03 +00:00
|
|
|
},
|
|
|
|
function(err, project) {
|
2015-07-12 20:24:09 +00:00
|
|
|
res.send(project);
|
|
|
|
}
|
|
|
|
);
|
2015-07-09 20:12:24 +00:00
|
|
|
});
|
|
|
|
|
2015-05-01 11:11:29 +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,
|
2015-12-25 20:57:59 +00:00
|
|
|
initiator: {type: 'user'},
|
2015-12-25 21:11:50 +00:00
|
|
|
queueQueued: true
|
2015-07-05 18:03:58 +00:00
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
res.send();
|
2015-05-01 11:11:29 +00:00
|
|
|
});
|
2015-05-03 15:23:01 +00:00
|
|
|
|
|
|
|
return resource;
|
2015-04-10 19:23:52 +00:00
|
|
|
};
|