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 32 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
58 changes: 58 additions & 0 deletions .github/scripts/validate-workflow-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Dependencies
const core = require('@actions/core');
const Ajv = require('ajv');
const yaml = require('js-yaml');
const fs = require('fs');
const axios = require('axios').default;

function validateYmlSchema(filename, validator){
// Read the schema and workflow file synchronously
const file = fs.readFileSync(filename, 'utf8');
try{
const target = yaml.load(file);
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
}
}
}

module.exports = async (allFiles) => {
const response = await axios.get('https://json.schemastore.org/github-workflow.json',{responseType: 'application/json'});
const schema = response.data;
const ajv = new Ajv({ strict: false, allErrors: true });
const validator = ajv.compile(schema);
const allLogs = {}
allFiles = allFiles.split(' ');
for(file of allFiles){
let log = validateYmlSchema(file, validator);
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');
}
}
3 changes: 3 additions & 0 deletions .github/workflows/sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: Erroneous yml file
This is a sample file that throws errors
Sample yml that throws errors
43 changes: 43 additions & 0 deletions .github/workflows/validate-workflow-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: GitHub Workflows Schema Validation

on:
pull_request:
paths:
- '.github/workflows/*.yml'
- '.github/workflows/*.yaml'
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
name: Validate workflow schema
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@a59f800cbb60ed483623848e31be67659a2940f8 #version https://github.com/tj-actions/changed-files/releases/tag/v18.7
with:
path: .github/workflows
files: |
.github/workflows/*.yml
.github/workflows/*.yaml

- name: Install dependencies
run: |
npm install [email protected]
npm install [email protected]
npm install [email protected]
npm install @actions/[email protected]
Comment on lines +32 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid of using versions here, maybe switch to commitId or just use package.json like I did here and do something like https://github.com/asyncapi/community/blob/master/.github/workflows/create-event-workflow-reusable.yml#L87-L95

Last few weeks I'm pretty picky to use versions in GitHub actions. This is very insecure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @Namyalg, do you want to make this change? let's not keep this PR open for a year 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will fix it asap

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it has been addressed


- name: Run script to validate schema
uses: actions/github-script@v6
with:
script: |
let files = `${{ steps.changed-files.outputs.all_changed_files }}`
const script = require('.github/scripts/validate-workflow-schema.js');
script(files);
2 changes: 2 additions & 0 deletions sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Sample yml
Sample yml file will not be tested