Skip to content

Commit a1ea4d0

Browse files
SAAS-5634 As a user I will see a warning in cli when I validate pipeline (#407)
* SAAS-5634 As a user I will see a warning in cli when I validate pipeline * SAAS-5634 As a user I will see a warning in cli when I validate pipeline * SAAS-5634 As a user I will see a warning in cli when I validate pipeline
1 parent edfd00c commit a1ea4d0

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const { pathExists, watchFile } = require('../../helpers/general');
88
const VALID_MESSAGE = Style.green('Yaml is valid!');
99

1010
function _getResultMessage(result = {}) {
11+
if (result.warning) {
12+
return result.valid
13+
? `${Style.yellow(result.warning)}`
14+
: `${Style.red(result.message)}\n${Style.yellow(result.warning)}\n`;
15+
}
1116
return result.valid
1217
? VALID_MESSAGE
1318
: `${Style.red(result.message)}\n`;
@@ -57,15 +62,13 @@ const validateCmd = new Command({
5762
return console.log('No filename provided!');
5863
}
5964

60-
const checkPromises = filenames.map((filename) => {
61-
return pathExists(filename)
62-
.then((exists) => {
63-
if (!exists) {
64-
console.log(`File does not exist: ${filename}`);
65-
}
66-
return exists;
67-
});
68-
});
65+
const checkPromises = filenames.map(filename => pathExists(filename)
66+
.then((exists) => {
67+
if (!exists) {
68+
console.log(`File does not exist: ${filename}`);
69+
}
70+
return exists;
71+
}));
6972
const allExist = (await Promise.all(checkPromises)).reduce((a, b) => a && b);
7073

7174
if (!allExist) {

lib/interface/cli/helpers/validation.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
const _ = require('lodash');
22
const yaml = require('js-yaml');
3+
const path = require('path');
34
const { readFile, ignoreHttpError } = require('./general');
45
const { sdk } = require('../../../logic');
56
const CFError = require('cf-errors');
67

78

89
function _buildFinalMessage(baseMessage, validationResult) {
9-
if (_.isArray(validationResult.details)) {
10-
const errors = validationResult.details
10+
if (_.isArray(validationResult)) {
11+
const errors = validationResult
1112
.map(({ message }) => ` - ${message}`)
1213
.join('\n');
1314
return `${baseMessage}:\n${errors}`;
1415
}
1516
return `${baseMessage}!`;
1617
}
1718

19+
function _getPipelineName(filename) {
20+
if (filename) {
21+
return path.basename(filename, path.extname(filename));
22+
}
23+
return filename;
24+
}
25+
1826
async function validatePipelineSpec(data) {
1927
const steps = _.get(data, 'spec.steps');
2028
const stages = _.get(data, 'spec.stages');
@@ -31,20 +39,29 @@ async function validatePipelineSpec(data) {
3139
const validatedYaml = yaml.safeDump(yamlObj);
3240
const result = await sdk.pipelines.validateYaml({ yaml: validatedYaml });
3341
let message;
42+
let warning;
3443
if (!result.valid) {
35-
message = _buildFinalMessage('Provided spec is not valid', result);
44+
message = _buildFinalMessage('Provided spec is not valid', result.details);
3645
}
37-
return { valid: !!result.valid, message };
46+
if (!_.isEmpty(result.warningDetails)) {
47+
warning = _buildFinalMessage('Yaml has warnings', result.warningDetails);
48+
}
49+
return { valid: !!result.valid, message, warning };
3850
}
3951

4052
async function validatePipelineYaml(filename, fileContent) {
4153
const yamlContent = fileContent || await readFile(filename, 'UTF-8');
42-
const result = await sdk.pipelines.validateYaml({ yaml: yamlContent });
54+
const name = _getPipelineName(filename);
55+
const result = await sdk.pipelines.validateYaml({ yaml: yamlContent, name });
4356
let message;
57+
let warning;
4458
if (!result.valid) {
45-
message = _buildFinalMessage('Yaml not valid', result);
59+
message = _buildFinalMessage('Yaml not valid', result.details);
60+
}
61+
if (!_.isEmpty(result.warningDetails)) {
62+
warning = _buildFinalMessage('Yaml has warnings', result.warningDetails);
4663
}
47-
return { valid: !!result.valid, message };
64+
return { valid: !!result.valid, message, warning };
4865
}
4966

5067
async function checkOrProjectExists(projectName) {

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

0 commit comments

Comments
 (0)