-
Notifications
You must be signed in to change notification settings - Fork 38
old validation refactored + yaml validation command #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const Command = require('../../Command'); | ||
const _ = require('lodash'); | ||
const Style = require('../../../../output/style'); | ||
const path = require('path'); | ||
const { validatePipelineYaml } = require('../../helpers/validation'); | ||
const { pathExists, watchFile } = require('../../helpers/general'); | ||
|
||
const VALID_MESSAGE = Style.green('Yaml is valid!'); | ||
|
||
function _printResult(result) { | ||
console.log(result.valid ? VALID_MESSAGE : Style.red(result.message), '\n'); | ||
} | ||
|
||
const validateCmd = new Command({ | ||
root: true, | ||
command: 'validate [filenames..]', | ||
description: 'Validate codefresh pipeline yaml config files.', | ||
usage: 'Validate one or many pipeline yaml files or attach validator to one and validate on changes', | ||
webDocs: { | ||
description: 'Validate codefresh pipeline yaml config files', | ||
category: 'Validation', | ||
title: 'Validate pipeline yaml', | ||
weight: 100, | ||
}, | ||
builder: (yargs) => { | ||
yargs | ||
.positional('filenames', { | ||
describe: 'Paths to yaml files', | ||
required: true, | ||
}) | ||
.option('attach', { | ||
alias: 'a', | ||
describe: 'Attach validator to the file and validate on change', | ||
}); | ||
|
||
|
||
return yargs; | ||
}, | ||
handler: async (argv) => { | ||
let { filenames, attach } = argv; | ||
filenames = filenames.map(f => path.resolve(process.cwd(), f)); | ||
|
||
if (_.isEmpty(filenames)) { | ||
console.log('No filename provided!'); | ||
return; | ||
} | ||
|
||
const checkPromises = filenames.map((filename) => { | ||
return pathExists(filename) | ||
.then((exists) => { | ||
if (!exists) { | ||
console.log(`File does not exist: ${filename}`); | ||
} | ||
return exists; | ||
}); | ||
}); | ||
const allExist = (await Promise.all(checkPromises)).reduce((a, b) => a && b); | ||
|
||
if (!allExist) { | ||
return; | ||
} | ||
|
||
if (filenames.length > 1) { | ||
if (attach) { | ||
console.log('Cannot watch many files!'); | ||
return; | ||
} | ||
|
||
filenames.forEach(f => validatePipelineYaml(f).then((result) => { | ||
console.log(`Validation result for ${f}:`); | ||
_printResult(result); | ||
})); | ||
return; | ||
} | ||
|
||
const filename = filenames[0]; | ||
if (attach) { | ||
yaroslav-codefresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(`Validator attached to file: ${filename}`); | ||
watchFile(filename, async () => { | ||
console.log('File changed'); | ||
const result = await validatePipelineYaml(filename); | ||
_printResult(result); | ||
}); | ||
} | ||
|
||
// even with --attach option validates file for first time | ||
const result = await validatePipelineYaml(filename); | ||
_printResult(result); | ||
}, | ||
}); | ||
|
||
module.exports = validateCmd; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,40 @@ | ||
const _ = require('lodash'); | ||
const yaml = require('js-yaml'); | ||
const { pipeline } = require('../../../logic').api; | ||
const { readFile } = require('./general'); | ||
|
||
|
||
async function validatePipelineFile(data) { | ||
function _buildFinalMessage(baseMessage, validationResult) { | ||
if (_.isArray(validationResult.details)) { | ||
const errors = validationResult.details | ||
.map(({ message }) => ` - ${message}`) | ||
.join('\n'); | ||
return `${baseMessage}:\n${errors}`; | ||
} | ||
return `${baseMessage}!`; | ||
} | ||
|
||
async function validatePipelineSpec(data) { | ||
const validatedYaml = yaml.safeDump(Object.assign({ version: data.version }, data.spec)); | ||
const validationResult = await pipeline.validateYaml(validatedYaml); | ||
if (!validationResult.valid) { | ||
let finalMessage; | ||
if (_.isArray(validationResult.details)) { | ||
const errors = validationResult.details.map(({ message }) => ` - ${message}`).join('\n'); | ||
finalMessage = `Provided spec is not valid:\n${errors}`; | ||
} else { | ||
finalMessage = 'Provided spec is not valid!'; | ||
} | ||
throw new Error(finalMessage); | ||
const result = await pipeline.validateYaml(validatedYaml); | ||
let message; | ||
if (!result.valid) { | ||
message = _buildFinalMessage('Provided spec is not valid', result); | ||
} | ||
return { valid: !!result.valid, message }; | ||
} | ||
|
||
async function validatePipelineYaml(filename) { | ||
const yamlContent = await readFile(filename, 'UTF-8'); | ||
const result = await pipeline.validateYaml(yamlContent); | ||
let message; | ||
if (!result.valid) { | ||
message = _buildFinalMessage('Yaml not valid', result); | ||
} | ||
return { valid: !!result.valid, message }; | ||
} | ||
|
||
module.exports = { | ||
validatePipelineFile, | ||
validatePipelineSpec, | ||
validatePipelineYaml, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.