Skip to content
16 changes: 16 additions & 0 deletions lib/models/apis/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ Runnable.prototype.buildBuild = function (build, opts, cb) {
buildModel.build(opts, cb);
};


Runnable.prototype.createContextVersion = function (contextId, cb) {
debug('createContextVersion', formatArgs(arguments));
this
.newContext(contextId.toString())
.createVersion(cb);
};

Runnable.prototype.copyVersionIcvFiles = function (contextId, cvId, icvId, cb) {
debug('copyVersionIcvFile', formatArgs(arguments));
this
.newContext(contextId)
.newVersion(cvId)
.copyFilesFromSource(icvId, cb);
};

Runnable.prototype.addAppCodeVersionsToContextVersion =
function (appCodeVersions, contextVersion, cb) {
debug('addAppCodeVersionsToContextVersion', formatArgs(arguments));
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ app.post('/builds/',
},
mw.req('body.owner.github')
.validate(validations.equalsKeypath('context.owner.github'))
.else(mw.next(Boom.badRequest('Context versions\' owners must match build owner')))),
.else(mw.next(Boom.badRequest('Context version\'s owner must match build owner')))),
function (req, res, next) {
req.contextIds = req.contextVersions.map(pluck('context'));
req.contextVersionIds = req.contextVersions.map(pluck('_id'));
Expand Down
39 changes: 38 additions & 1 deletion lib/routes/contexts/versions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var express = require('express');
var app = module.exports = express();
var uuid = require('uuid');

var mw = require('dat-middleware');
var flow = require('middleware-flow');
Expand All @@ -27,6 +28,7 @@ var isPopulatedArray = validations.isPopulatedArray;
var transformations = require('middlewares/transformations');
var mavis = require('middlewares/apis').mavis;
var docker = require('middlewares/apis').docker;
var runnable = require('middlewares/apis').runnable;
var error = require('error');
var ownerIsHelloRunnable = require('middlewares/owner-is-hello-runnable');

Expand Down Expand Up @@ -208,10 +210,45 @@ app.post('/contexts/:contextId/versions/:id/actions/copy',
checkFound('contextVersion'),
// only supports deep copy for now
mw.query('deep').require().validate(validations.equals('true')),
contextVersions.createDeepCopy('sessionUser', 'contextVersion'),
mw.req('context.owner.github').require()
.validate(validations.equals(process.env.HELLO_RUNNABLE_GITHUB_ID))
.validate(validations.notEqualsKeypath('sessionUser.accounts.github.id'))
.then(
// if the build owner is hello-runnable and user is not hello-runnable
deepCopyCvFromHelloRunnable('contextVersion')
).else(
contextVersions.createDeepCopy('sessionUser', 'contextVersion')),
mw.res.status(201),
mw.res.json('contextVersion'));

function deepCopyCvFromHelloRunnable (cvKey) {
return flow.series(
// maybe error if the contextVersion has acvs?
// make a new context
mw.req('contextVersion', cvKey),
mw.body().pick('owner'),
runnable.create({}, 'sessionUser'),
// create context requires name
function (req, res, next) {
req.body.name = uuid();
next();
},
runnable.model.createContext('body'),
mw.req().set('newContext', 'runnableResult'),
// make a new context-version
runnable.create({}, 'sessionUser'),
runnable.model.createContextVersion('newContext._id'),
mw.req().set('newContextVersion', 'runnableResult'),
// use infracodeversion copy route to copy the files
runnable.create({}, 'sessionUser'),
runnable.model.copyVersionIcvFiles(
'newContext._id',
'newContextVersion._id',
'contextVersion.infraCodeVersion'),
contextVersions.findById('newContextVersion._id')
);
}

/** Builds a context version
* used internally (github hook, build build), session user may not be real
* @param id versionId of the source version
Expand Down