Skip to content
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

feat: validate workflow schema #159

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6d3a56e
feat: workflow to verify yml schema
Namyalg Apr 22, 2022
6e8189c
Added sample yml files
Namyalg Apr 23, 2022
6d6ac5c
Added demo yml files
Namyalg Apr 23, 2022
b48c71b
Added sample yml files
Namyalg Apr 23, 2022
3a4bf10
add error message in schema validation
Namyalg Apr 23, 2022
97787ed
added erroneous yml files
Namyalg Apr 23, 2022
eafbd93
delete test workflow files
Namyalg Apr 23, 2022
b0445a5
Update validate-workflow-schema.js
Namyalg Apr 26, 2022
870fab7
Update validate-workflow-schema.yml
Namyalg Apr 27, 2022
9e5fa60
Update validate-workflow-schema.yml
Namyalg Apr 27, 2022
b0e347d
Update validate-workflow-schema.js
Namyalg Apr 27, 2022
b90a1bc
Create yml-schema.json
Namyalg Apr 27, 2022
fd03860
Update validate-workflow-schema.js
Namyalg Apr 27, 2022
89af6a9
Update validate-workflow-schema.yml
Namyalg Apr 27, 2022
97e49ca
Update validate-workflow-schema.yml
Namyalg Apr 29, 2022
4fecd8f
Update validate-workflow-schema.yml
Namyalg May 1, 2022
749560c
update validate-workflow-schema.yml
Namyalg May 9, 2022
8658d77
update: validate-workflow-schema.yml
Namyalg May 10, 2022
77ac7dd
update: validate-workflow-schema.yml
Namyalg May 10, 2022
402ae79
update: remove check for file extension in script
Namyalg May 10, 2022
4340301
Update validate-workflow-schema.yml
Namyalg May 10, 2022
49eae52
Update validate-workflow-schema.js
Namyalg May 10, 2022
ecc24d8
Update validate-workflow-schema.yml
Namyalg May 10, 2022
1efb3a7
Update validate-workflow-schema.js
Namyalg May 10, 2022
64b171b
Update validate-workflow-schema.yml
Namyalg May 11, 2022
9ef7472
added sample files to test
Namyalg May 11, 2022
fe6ba3d
added erroneous file for test
Namyalg May 11, 2022
f7fe980
test paths allowed on PR
Namyalg May 11, 2022
02a5182
Merge branch 'master' into validate-workflow-schema
derberg Sep 20, 2022
6c6498a
Merge branch 'master' into validate-workflow-schema
derberg Sep 21, 2022
871d46f
Update .github/workflows/validate-workflow-schema.yml
Namyalg Oct 22, 2022
38ecbd0
Update workflow validation script to read yml schema from URL
Namyalg Jan 6, 2023
24a3c8c
Delete sample file
Namyalg Jan 13, 2023
dde4d07
Delete sample file
Namyalg Jan 13, 2023
eb39090
Merge branch 'master' into validate-workflow-schema
KhudaDad414 Mar 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/scripts/validate-workflow-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const core = require('@actions/core');
const Ajv = require('ajv');
const yaml = require('js-yaml');
const fs = require('fs');


function getFileExtension(filename){
return filename.split('.').pop();
}

function validateYmlSchema(filename){
const fileExtensions = ['yml', 'yaml'];
if(fileExtensions.includes(getFileExtension(filename))){
// Read the schema file and workflow file synchronously
let schema = fs.readFileSync('.github/scripts/yml-schema.json', {encoding:'utf8', flag:'r'});
schema = JSON.parse(schema);
const file = fs.readFileSync(filename, 'utf8');
try{
const target = yaml.load(file);
const ajv = new Ajv({ strict: false, allErrors: true });
const validator = ajv.compile(schema);
const valid = validator(target);
// Return the status and log for each workflow file validated
if (!valid) {
return {
'status' : false,
'log': validator.errors
}
} else {
return {
'status' : true,
'log': 'Validation successful'
}
}
}
catch(err){
return {
'status' : false,
'log': err
}
}
} else {
return {
'status' : true,
'log': 'Not a yml/yaml file'
}
}
}

module.exports = (allFiles) => {
const allLogs = {}
allFiles = allFiles.split(' ');
for(file of allFiles){
let log = validateYmlSchema(file);
if(!log['status']){
allLogs[file] = log['log']
}
}
// Workflow fails if an error is detected in any file
if(Object.keys(allLogs).length > 0){
for(file in allLogs){
console.log("ERROR IN FILE " + file)
console.log(allLogs[file]);
}
core.setFailed('There are errors in the workflow files');
} else {
console.log('No errors detected in the yml/yaml files');
}
}
Loading