diff --git a/README.md b/README.md index fe0ffe4..3f39608 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,17 @@ This action validates YAML files using the `yaml.schemas` settings for the [VS C All you need is a **.vscode/settings.json** document at the root of the repository that contains the `yaml.schemas` setting +## Inputs + +### `settingsFile` (optional) + +Location of the schema configuration file. + +The default location is **.vscode/settings.json**, you can change it do a different location but it but still be a json document containing the `yaml.schemas` config. +See the [VS Code YAML Extension](https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml) for how to structure the config. + + + ## Outputs ### `invalidFiles` diff --git a/action.yml b/action.yml index 38e767a..d5d982a 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,10 @@ name: 'YAML Schema Validator' description: 'Validate YAML files in a repo according to settings' +inputs: + settingsFile: + description: 'Location of schema configuration file' + required: false + default: '.vscode/settings.json' outputs: invalidFiles: description: 'Comma separated list of files that failed the schema validation' diff --git a/lib/index.js b/lib/index.js index e531e05..f537ccc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -896,7 +896,7 @@ eval("\r\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\r\n /***/ (function(__unused_webpack_module, exports, __webpack_require__) { "use strict"; -eval("\r\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\r\nconst core = __webpack_require__(/*! @actions/core */ \"./node_modules/@actions/core/lib/core.js\");\r\nconst yaml_validator_1 = __webpack_require__(/*! ./yaml-validator */ \"./src/yaml-validator.ts\");\r\nfunction run() {\r\n return __awaiter(this, void 0, void 0, function* () {\r\n try {\r\n const workspaceRoot = process.env['GITHUB_WORKSPACE'];\r\n const validationResults = yield yaml_validator_1.validateYaml(workspaceRoot);\r\n const invalidResults = validationResults.filter(res => !res.valid).map(res => res.filePath);\r\n const invalidFiles = invalidResults.length > 0 ? invalidResults.join(',') : '';\r\n core.setOutput('invalidFiles', invalidFiles);\r\n if (invalidResults.length > 0) {\r\n core.warning('Invalid Files: ' + invalidFiles);\r\n core.setFailed('Schema validation failed on one or more YAML files.');\r\n }\r\n else {\r\n core.info(`✅ YAML Schema validation completed successfully`);\r\n }\r\n }\r\n catch (error) {\r\n core.setFailed(error.message);\r\n }\r\n });\r\n}\r\nrun();\r\n\n\n//# sourceURL=webpack://validate-yaml-schema/./src/main.ts?"); +eval("\r\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\r\nconst core = __webpack_require__(/*! @actions/core */ \"./node_modules/@actions/core/lib/core.js\");\r\nconst yaml_validator_1 = __webpack_require__(/*! ./yaml-validator */ \"./src/yaml-validator.ts\");\r\nfunction run() {\r\n return __awaiter(this, void 0, void 0, function* () {\r\n try {\r\n const workspaceRoot = process.env['GITHUB_WORKSPACE'];\r\n const settingsFile = core.getInput('settingsfile');\r\n const validationResults = yield yaml_validator_1.validateYaml(workspaceRoot, settingsFile);\r\n const invalidResults = validationResults.filter(res => !res.valid).map(res => res.filePath);\r\n const invalidFiles = invalidResults.length > 0 ? invalidResults.join(',') : '';\r\n core.setOutput('invalidFiles', invalidFiles);\r\n if (invalidResults.length > 0) {\r\n core.warning('Invalid Files: ' + invalidFiles);\r\n core.setFailed('Schema validation failed on one or more YAML files.');\r\n }\r\n else {\r\n core.info(`✅ YAML Schema validation completed successfully`);\r\n }\r\n }\r\n catch (error) {\r\n core.setFailed(error.message);\r\n }\r\n });\r\n}\r\nrun();\r\n\n\n//# sourceURL=webpack://validate-yaml-schema/./src/main.ts?"); /***/ }), @@ -940,7 +940,7 @@ eval("\r\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _argume /***/ (function(__unused_webpack_module, exports, __webpack_require__) { "use strict"; -eval("\r\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\r\nexports.validateYaml = void 0;\r\nconst path = __webpack_require__(/*! path */ \"path\");\r\nconst glob_1 = __webpack_require__(/*! glob */ \"./node_modules/glob/glob.js\");\r\nconst yaml_file_reader_1 = __webpack_require__(/*! ./yaml-file-reader */ \"./src/yaml-file-reader.ts\");\r\nconst schema_validator_1 = __webpack_require__(/*! ./schema-validator */ \"./src/schema-validator.ts\");\r\nconst logger_1 = __webpack_require__(/*! ./logger */ \"./src/logger.ts\");\r\nexports.validateYaml = (workspaceRoot) => __awaiter(void 0, void 0, void 0, function* () {\r\n try {\r\n //Get the schema settings\r\n const settings = yield yaml_file_reader_1.getJson(path.join(workspaceRoot, '.vscode/settings.json'));\r\n const schemas = settings ? settings['yaml.schemas'] : null;\r\n if (!schemas)\r\n throw 'no schema settings found';\r\n const schemaValidator = new schema_validator_1.SchemaValidator(schemas, workspaceRoot);\r\n //TODO: improve this implementation - e.g. use the glob patterns from the yaml.schemas settings \r\n const filePaths = yield new Promise((c, e) => {\r\n glob_1.glob('**/*.{yml,yaml}', {\r\n cwd: workspaceRoot,\r\n silent: true,\r\n nodir: true,\r\n }, (err, files) => {\r\n if (err) {\r\n e(err);\r\n }\r\n c(files);\r\n });\r\n });\r\n return yield Promise.all(filePaths.map((filePath) => __awaiter(void 0, void 0, void 0, function* () {\r\n try {\r\n const yamlDocument = yield yaml_file_reader_1.getYaml(path.join(workspaceRoot, filePath));\r\n const result = yield schemaValidator.isValid(yamlDocument);\r\n logger_1.prettyLog(filePath);\r\n return { filePath, valid: result };\r\n }\r\n catch (e) {\r\n logger_1.prettyLog(filePath, e);\r\n return { filePath, valid: false };\r\n }\r\n })));\r\n }\r\n catch (err) {\r\n logger_1.prettyLog(workspaceRoot, err);\r\n return [{ filePath: workspaceRoot, valid: false }];\r\n }\r\n});\r\n\n\n//# sourceURL=webpack://validate-yaml-schema/./src/yaml-validator.ts?"); +eval("\r\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n};\r\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\r\nexports.validateYaml = void 0;\r\nconst path = __webpack_require__(/*! path */ \"path\");\r\nconst glob_1 = __webpack_require__(/*! glob */ \"./node_modules/glob/glob.js\");\r\nconst yaml_file_reader_1 = __webpack_require__(/*! ./yaml-file-reader */ \"./src/yaml-file-reader.ts\");\r\nconst schema_validator_1 = __webpack_require__(/*! ./schema-validator */ \"./src/schema-validator.ts\");\r\nconst logger_1 = __webpack_require__(/*! ./logger */ \"./src/logger.ts\");\r\nexports.validateYaml = (workspaceRoot, settingsFile) => __awaiter(void 0, void 0, void 0, function* () {\r\n try {\r\n //Get the schema settings\r\n const settings = yield yaml_file_reader_1.getJson(path.join(workspaceRoot, settingsFile));\r\n const schemas = settings ? settings['yaml.schemas'] : null;\r\n if (!schemas)\r\n throw 'no schema settings found';\r\n const schemaValidator = new schema_validator_1.SchemaValidator(schemas, workspaceRoot);\r\n //TODO: improve this implementation - e.g. use the glob patterns from the yaml.schemas settings \r\n const filePaths = yield new Promise((c, e) => {\r\n glob_1.glob('**/*.{yml,yaml}', {\r\n cwd: workspaceRoot,\r\n silent: true,\r\n nodir: true,\r\n }, (err, files) => {\r\n if (err) {\r\n e(err);\r\n }\r\n c(files);\r\n });\r\n });\r\n return yield Promise.all(filePaths.map((filePath) => __awaiter(void 0, void 0, void 0, function* () {\r\n try {\r\n const yamlDocument = yield yaml_file_reader_1.getYaml(path.join(workspaceRoot, filePath));\r\n const result = yield schemaValidator.isValid(yamlDocument);\r\n logger_1.prettyLog(filePath);\r\n return { filePath, valid: result };\r\n }\r\n catch (e) {\r\n logger_1.prettyLog(filePath, e);\r\n return { filePath, valid: false };\r\n }\r\n })));\r\n }\r\n catch (err) {\r\n logger_1.prettyLog(workspaceRoot, err);\r\n return [{ filePath: workspaceRoot, valid: false }];\r\n }\r\n});\r\n\n\n//# sourceURL=webpack://validate-yaml-schema/./src/yaml-validator.ts?"); /***/ }), diff --git a/src/main.ts b/src/main.ts index 08c47e6..3c08daa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,9 +5,10 @@ async function run() { try { const workspaceRoot = process.env['GITHUB_WORKSPACE']; - - const validationResults = await validateYaml(workspaceRoot); + const settingsFile = core.getInput('settingsfile'); + + const validationResults = await validateYaml(workspaceRoot, settingsFile); const invalidResults = validationResults.filter(res => !res.valid).map(res => res.filePath); diff --git a/src/yaml-validator.ts b/src/yaml-validator.ts index c097107..f847e31 100644 --- a/src/yaml-validator.ts +++ b/src/yaml-validator.ts @@ -9,11 +9,11 @@ export interface ValidationResult { valid: boolean; } -export const validateYaml = async ( workspaceRoot: string): Promise => { +export const validateYaml = async ( workspaceRoot: string, settingsFile: any): Promise => { try { //Get the schema settings - const settings = await getJson(path.join(workspaceRoot, '.vscode/settings.json')); + const settings = await getJson(path.join(workspaceRoot, settingsFile)); const schemas = settings ? settings['yaml.schemas'] : null; if(!schemas)