-
Notifications
You must be signed in to change notification settings - Fork 70
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
Closed
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 6e8189c
Added sample yml files
Namyalg 6d6ac5c
Added demo yml files
Namyalg b48c71b
Added sample yml files
Namyalg 3a4bf10
add error message in schema validation
Namyalg 97787ed
added erroneous yml files
Namyalg eafbd93
delete test workflow files
Namyalg b0445a5
Update validate-workflow-schema.js
Namyalg 870fab7
Update validate-workflow-schema.yml
Namyalg 9e5fa60
Update validate-workflow-schema.yml
Namyalg b0e347d
Update validate-workflow-schema.js
Namyalg b90a1bc
Create yml-schema.json
Namyalg fd03860
Update validate-workflow-schema.js
Namyalg 89af6a9
Update validate-workflow-schema.yml
Namyalg 97e49ca
Update validate-workflow-schema.yml
Namyalg 4fecd8f
Update validate-workflow-schema.yml
Namyalg 749560c
update validate-workflow-schema.yml
Namyalg 8658d77
update: validate-workflow-schema.yml
Namyalg 77ac7dd
update: validate-workflow-schema.yml
Namyalg 402ae79
update: remove check for file extension in script
Namyalg 4340301
Update validate-workflow-schema.yml
Namyalg 49eae52
Update validate-workflow-schema.js
Namyalg ecc24d8
Update validate-workflow-schema.yml
Namyalg 1efb3a7
Update validate-workflow-schema.js
Namyalg 64b171b
Update validate-workflow-schema.yml
Namyalg 9ef7472
added sample files to test
Namyalg fe6ba3d
added erroneous file for test
Namyalg f7fe980
test paths allowed on PR
Namyalg 02a5182
Merge branch 'master' into validate-workflow-schema
derberg 6c6498a
Merge branch 'master' into validate-workflow-schema
derberg 871d46f
Update .github/workflows/validate-workflow-schema.yml
Namyalg 38ecbd0
Update workflow validation script to read yml schema from URL
Namyalg 24a3c8c
Delete sample file
Namyalg dde4d07
Delete sample file
Namyalg eb39090
Merge branch 'master' into validate-workflow-schema
KhudaDad414 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 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,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'); | ||
} | ||
} |
This file contains 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,3 @@ | ||
name: Erroneous yml file | ||
This is a sample file that throws errors | ||
Sample yml that throws errors |
This file contains 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,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] | ||
|
||
- 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); |
This file contains 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,2 @@ | ||
name: Sample yml | ||
Sample yml file will not be tested |
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.
There was a problem hiding this comment.
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 usepackage.json
like I did here and do something like https://github.com/asyncapi/community/blob/master/.github/workflows/create-event-workflow-reusable.yml#L87-L95Last few weeks I'm pretty picky to use versions in GitHub actions. This is very insecure.
There was a problem hiding this comment.
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 😆
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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