Skip to content

Commit 6f04765

Browse files
Feauture/olegz/saas 4818 refactored root creating (#380)
* SAAS-4712 #time 2h 10m Made some small refactoring - moved checking existing project to the helper. * SAAS-4712 #time 0h 50m Added manifest file supporting for creating command * SAAS-4712 #time 0h 50m Added manifest file supporting for creating command * SAAS-4712 #time 0h 50m removed `creating entities` from cmd to the helper * SAAS-4712 #time 0h 5m bump * SAAS-4818 #time 0h 1m rename var * SAAS-4818 #time 0h 40m write tests for creatingEntities.js * SAAS-4818 #time 0h 10m fixed tests * rebuild Co-authored-by: Denis Melnik <58072595+denis-codefresh@users.noreply.github.com>
1 parent 6334c12 commit 6f04765

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

lib/interface/cli/commands/root/create.cmd.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ const _ = require('lodash');
22
const CFError = require('cf-errors');
33
const Command = require('../../Command');
44
const { crudFilenameOption } = require('../../helpers/general');
5+
const creatingEntities = require('../../helpers/creatingEntities');
56
const yargs = require('yargs');
6-
const { validatePipelineSpec, checkOrProjectExists } = require('../../helpers/validation');
7-
const { sdk } = require('../../../../logic');
87

98
const get = new Command({
109
root: true,
@@ -35,32 +34,14 @@ const get = new Command({
3534
throw new CFError('Name is missing');
3635
}
3736

38-
switch (entity) {
39-
case 'context':
40-
await sdk.contexts.create(data);
41-
console.log(`Context: ${name} created`);
42-
break;
43-
case 'pipeline':
44-
const result = await validatePipelineSpec(data);
45-
if (!result.valid) {
46-
console.warn(result.message);
47-
return;
48-
}
49-
await sdk.pipelines.create(data);
50-
console.log(`Pipeline '${name}' created`);
51-
break;
52-
case 'step-type':
53-
await sdk.steps.create(data);
54-
console.log(`Step-type '${name}' created`);
55-
break;
56-
case 'project':
57-
await checkOrProjectExists(name);
58-
await sdk.projects.create({ projectName: name, ..._.pick(data.metadata, ['tags', 'variables']) });
59-
console.log(`Project: ${name} created`);
60-
break;
61-
default:
62-
throw new CFError(`Entity: ${entity} not supported`);
37+
const entities = creatingEntities({ name, data });
38+
39+
if (!entities[entity]) {
40+
throw new CFError(`Entity: ${entity} not supported`);
6341
}
42+
43+
await entities[entity]();
44+
console.log(`${_.capitalize(entity)}: ${name} created`);
6445
},
6546
});
6647

lib/interface/cli/commands/root/root.sdk.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ validateCmd = validateCmd.toCommand();
2424

2525
jest.mock('../../helpers/validation', () => { // eslint-disable-line
2626
return {
27-
validatePipelineSpec: () => ({ valid: true }),
28-
validatePipelineYaml: () => ({ valid: false }),
27+
validatePipelineSpec: async () => ({ valid: true }),
28+
validatePipelineYaml: async () => ({ valid: false }),
2929
};
3030
});
3131

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const _ = require('lodash');
2+
const { validatePipelineSpec, checkOrProjectExists } = require('./validation');
3+
const { sdk } = require('../../../logic');
4+
5+
module.exports = ({ name, data }) => ({
6+
7+
context: () => sdk.contexts.create(data),
8+
'step-type': () => sdk.steps.create(data),
9+
project: () => checkOrProjectExists(name)
10+
.then(() => sdk.projects.create({ projectName: name, ..._.pick(data.metadata, ['tags', 'variables']) })),
11+
pipeline: () => validatePipelineSpec(data)
12+
.then((result = {}) => (result.valid ? sdk.pipelines.create(data) : console.warn(result.message))),
13+
14+
});

lib/interface/cli/helpers/helpers.unit.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const _ = require('lodash');
22
const helper = require('./image');
33
const moment = require('moment');
4+
const creatingEntities = require('./creatingEntities');
45

56
const { extractImages } = helper;
67

@@ -11,6 +12,22 @@ jest.mock('../../../logic/entities/Image', () => class {
1112
});
1213

1314
describe('helpers unit tests', () => {
15+
describe('creating entites', () => {
16+
const request = require('requestretry');
17+
beforeEach(async () => {
18+
request.__reset();
19+
request.mockClear();
20+
await configureSdk(); // eslint-disable-line
21+
});
22+
const entities = creatingEntities({});
23+
it('should be an Object', () => expect(entities instanceof Object).toBe(true));
24+
it('should have some size', () => expect(_.size(entities)).not.toBe(0));
25+
for (const entity in entities) {
26+
it(`${entity} should be a function`, () => expect(entities[entity] instanceof Function).toBe(true));
27+
it(`${entity} should be promise`, () => expect(entities[entity]().catch(() => null) instanceof Promise).toBe(true));
28+
}
29+
});
30+
1431
describe('images', () => {
1532
describe('#extractImages', () => {
1633
beforeAll(() => {
@@ -21,7 +38,7 @@ describe('helpers unit tests', () => {
2138
const image = {
2239
tags: [{ tag: 'test', registry: 'r.cfcr.io' }],
2340
};
24-
expect(extractImages(image)).toEqual([{tag: 'test'}]);
41+
expect(extractImages(image)).toEqual([{ tag: 'test' }]);
2542
});
2643

2744
it('should extract image tags as separate images', () => {

lib/logic/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ const sdk = require('./sdk');
33
module.exports = {
44
sdk,
55
};
6+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.39.2",
3+
"version": "0.39.3",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)