Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
make settings location configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
velsietis committed Jan 12, 2021
1 parent 3ccf393 commit e085a7f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?");

/***/ }),

Expand Down Expand Up @@ -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?");

/***/ }),

Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ async function run() {
try {

const workspaceRoot = <string>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);

Expand Down
4 changes: 2 additions & 2 deletions src/yaml-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export interface ValidationResult {
valid: boolean;
}

export const validateYaml = async ( workspaceRoot: string): Promise<ValidationResult[]> => {
export const validateYaml = async ( workspaceRoot: string, settingsFile: any): Promise<ValidationResult[]> => {

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)
Expand Down

0 comments on commit e085a7f

Please sign in to comment.