Skip to content

Commit 5f8ef1a

Browse files
spec validation for pipeline (#245)
* spec validation for pipeline * version updated
1 parent 9ff395c commit 5f8ef1a

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Command = require('../../Command');
33
const { crudFilenameOption } = require('../../helpers/general');
44
const { context, pipeline } = require('../../../../logic').api;
55
const yargs = require('yargs');
6+
const { validatePipelineFile } = require('../../helpers/validation');
67

78
const get = new Command({
89
root: true,
@@ -22,6 +23,7 @@ const get = new Command({
2223
handler: async (argv) => {
2324
if (!argv.filename) {
2425
yargs.showHelp();
26+
return;
2527
}
2628

2729
const data = argv.filename;
@@ -38,13 +40,18 @@ const get = new Command({
3840
console.log(`Context: ${name} created`);
3941
break;
4042
case 'pipeline':
43+
try {
44+
await validatePipelineFile(data);
45+
} catch (e) {
46+
console.warn(e.message);
47+
return;
48+
}
4149
await pipeline.createPipeline(data);
4250
console.log(`Pipeline '${name}' created`);
4351
break;
4452
default:
4553
throw new CFError(`Entity: ${entity} not supported`);
4654
}
47-
4855
},
4956
});
5057

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Command = require('../../Command');
33
const { crudFilenameOption } = require('../../helpers/general');
44
const { context, pipeline } = require('../../../../logic').api;
55
const yargs = require('yargs');
6+
const { validatePipelineFile } = require('../../helpers/validation');
67

78
const annotate = new Command({
89
root: true,
@@ -38,6 +39,12 @@ const annotate = new Command({
3839
console.log(`Context: ${name} created`);
3940
break;
4041
case 'pipeline':
42+
try {
43+
await validatePipelineFile(data);
44+
} catch (e) {
45+
console.warn(e.message);
46+
return;
47+
}
4148
await pipeline.replaceByName(name, data);
4249
console.log(`Pipeline '${name}' updated`);
4350
break;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const _ = require('lodash');
2+
const yaml = require('js-yaml');
3+
const { pipeline } = require('../../../logic').api;
4+
5+
6+
async function validatePipelineFile(data) {
7+
const validatedYaml = yaml.safeDump(Object.assign({ version: data.version }, data.spec));
8+
const validationResult = await pipeline.validateYaml(validatedYaml);
9+
if (!validationResult.valid) {
10+
let finalMessage;
11+
if (_.isArray(validationResult.details)) {
12+
const errors = validationResult.details.map(({ message }) => ` - ${message}`).join('\n');
13+
finalMessage = `Provided spec is not valid:\n${errors}`;
14+
} else {
15+
finalMessage = 'Provided spec is not valid!';
16+
}
17+
throw new Error(finalMessage);
18+
}
19+
}
20+
21+
module.exports = {
22+
validatePipelineFile,
23+
};

lib/logic/api/pipeline.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ const createPipeline = async (data) => {
6666
return sendHttpRequest(options);
6767
};
6868

69+
const validateYaml = async (yaml) => {
70+
const optionsValidate = {
71+
url: '/api/pipelines/yaml/validator',
72+
method: 'POST',
73+
body: { yaml },
74+
};
75+
return sendHttpRequest(optionsValidate);
76+
};
77+
6978
const replaceByName = async (name, data) => {
7079
const body = data;
7180

@@ -164,6 +173,7 @@ module.exports = {
164173
getAll,
165174
getPipelineByName,
166175
createPipeline,
176+
validateYaml,
167177
replaceByName,
168178
patchPipelineByName,
169179
deletePipelineByName,

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.9.0",
3+
"version": "0.9.1",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)