Skip to content

Commit 8afac41

Browse files
committed
Merge pull request #515 from CodeNow/getting-started-owner-maintenance
allow cv copy from hello-runnable to another user
2 parents e0792ae + 7b6e878 commit 8afac41

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

lib/models/apis/runnable.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ Runnable.prototype.buildBuild = function (build, opts, cb) {
181181
buildModel.build(opts, cb);
182182
};
183183

184+
185+
Runnable.prototype.createContextVersion = function (contextId, cb) {
186+
debug('createContextVersion', formatArgs(arguments));
187+
this
188+
.newContext(contextId.toString())
189+
.createVersion(cb);
190+
};
191+
192+
Runnable.prototype.copyVersionIcvFiles = function (contextId, cvId, icvId, cb) {
193+
debug('copyVersionIcvFile', formatArgs(arguments));
194+
this
195+
.newContext(contextId)
196+
.newVersion(cvId)
197+
.copyFilesFromSource(icvId, cb);
198+
};
199+
184200
Runnable.prototype.addAppCodeVersionsToContextVersion =
185201
function (appCodeVersions, contextVersion, cb) {
186202
debug('addAppCodeVersionsToContextVersion', formatArgs(arguments));

lib/routes/builds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ app.post('/builds/',
6363
},
6464
mw.req('body.owner.github')
6565
.validate(validations.equalsKeypath('context.owner.github'))
66-
.else(mw.next(Boom.badRequest('Context versions\' owners must match build owner')))),
66+
.else(mw.next(Boom.badRequest('Context version\'s owner must match build owner')))),
6767
function (req, res, next) {
6868
req.contextIds = req.contextVersions.map(pluck('context'));
6969
req.contextVersionIds = req.contextVersions.map(pluck('_id'));

lib/routes/contexts/versions/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
var express = require('express');
99
var app = module.exports = express();
10+
var uuid = require('uuid');
1011

1112
var mw = require('dat-middleware');
1213
var flow = require('middleware-flow');
@@ -27,6 +28,7 @@ var isPopulatedArray = validations.isPopulatedArray;
2728
var transformations = require('middlewares/transformations');
2829
var mavis = require('middlewares/apis').mavis;
2930
var docker = require('middlewares/apis').docker;
31+
var runnable = require('middlewares/apis').runnable;
3032
var error = require('error');
3133
var ownerIsHelloRunnable = require('middlewares/owner-is-hello-runnable');
3234

@@ -208,10 +210,45 @@ app.post('/contexts/:contextId/versions/:id/actions/copy',
208210
checkFound('contextVersion'),
209211
// only supports deep copy for now
210212
mw.query('deep').require().validate(validations.equals('true')),
211-
contextVersions.createDeepCopy('sessionUser', 'contextVersion'),
213+
mw.req('context.owner.github').require()
214+
.validate(validations.equals(process.env.HELLO_RUNNABLE_GITHUB_ID))
215+
.validate(validations.notEqualsKeypath('sessionUser.accounts.github.id'))
216+
.then(
217+
// if the build owner is hello-runnable and user is not hello-runnable
218+
deepCopyCvFromHelloRunnable('contextVersion')
219+
).else(
220+
contextVersions.createDeepCopy('sessionUser', 'contextVersion')),
212221
mw.res.status(201),
213222
mw.res.json('contextVersion'));
214223

224+
function deepCopyCvFromHelloRunnable (cvKey) {
225+
return flow.series(
226+
// maybe error if the contextVersion has acvs?
227+
// make a new context
228+
mw.req('contextVersion', cvKey),
229+
mw.body().pick('owner'),
230+
runnable.create({}, 'sessionUser'),
231+
// create context requires name
232+
function (req, res, next) {
233+
req.body.name = uuid();
234+
next();
235+
},
236+
runnable.model.createContext('body'),
237+
mw.req().set('newContext', 'runnableResult'),
238+
// make a new context-version
239+
runnable.create({}, 'sessionUser'),
240+
runnable.model.createContextVersion('newContext._id'),
241+
mw.req().set('newContextVersion', 'runnableResult'),
242+
// use infracodeversion copy route to copy the files
243+
runnable.create({}, 'sessionUser'),
244+
runnable.model.copyVersionIcvFiles(
245+
'newContext._id',
246+
'newContextVersion._id',
247+
'contextVersion.infraCodeVersion'),
248+
contextVersions.findById('newContextVersion._id')
249+
);
250+
}
251+
215252
/** Builds a context version
216253
* used internally (github hook, build build), session user may not be real
217254
* @param id versionId of the source version

0 commit comments

Comments
 (0)