1
1
const _ = require ( 'lodash' ) ;
2
2
const yaml = require ( 'js-yaml' ) ;
3
+ const path = require ( 'path' ) ;
3
4
const { readFile, ignoreHttpError } = require ( './general' ) ;
4
5
const { sdk } = require ( '../../../logic' ) ;
5
6
const CFError = require ( 'cf-errors' ) ;
6
7
7
8
8
9
function _buildFinalMessage ( baseMessage , validationResult ) {
9
- if ( _ . isArray ( validationResult . details ) ) {
10
- const errors = validationResult . details
10
+ if ( _ . isArray ( validationResult ) ) {
11
+ const errors = validationResult
11
12
. map ( ( { message } ) => ` - ${ message } ` )
12
13
. join ( '\n' ) ;
13
14
return `${ baseMessage } :\n${ errors } ` ;
14
15
}
15
16
return `${ baseMessage } !` ;
16
17
}
17
18
19
+ function _getPipelineName ( filename ) {
20
+ if ( filename ) {
21
+ return path . basename ( filename , path . extname ( filename ) ) ;
22
+ }
23
+ return filename ;
24
+ }
25
+
18
26
async function validatePipelineSpec ( data ) {
19
27
const steps = _ . get ( data , 'spec.steps' ) ;
20
28
const stages = _ . get ( data , 'spec.stages' ) ;
@@ -31,20 +39,29 @@ async function validatePipelineSpec(data) {
31
39
const validatedYaml = yaml . safeDump ( yamlObj ) ;
32
40
const result = await sdk . pipelines . validateYaml ( { yaml : validatedYaml } ) ;
33
41
let message ;
42
+ let warning ;
34
43
if ( ! result . valid ) {
35
- message = _buildFinalMessage ( 'Provided spec is not valid' , result ) ;
44
+ message = _buildFinalMessage ( 'Provided spec is not valid' , result . details ) ;
36
45
}
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 } ;
38
50
}
39
51
40
52
async function validatePipelineYaml ( filename , fileContent ) {
41
53
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 } ) ;
43
56
let message ;
57
+ let warning ;
44
58
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 ) ;
46
63
}
47
- return { valid : ! ! result . valid , message } ;
64
+ return { valid : ! ! result . valid , message, warning } ;
48
65
}
49
66
50
67
async function checkOrProjectExists ( projectName ) {
0 commit comments