0

I am experimenting with jsonschema to validate GitHub Action files. I've noticed that while loading the workflow YAML file using yaml.load(), the on key isn't read correctly. I've also tried using yaml.safe_load(), but the issue persists.

Code

import requests
import json
import yaml
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from pprint import pprint


_schema = "CICD\github-workflow"
# URL for the JSONSchema https://json.schemastore.org/github-workflow
schema = json.load(open(f'schemas\{_schema}.json', encoding='utf8'))

# Load your GitHub Workflow file
with open(f'tests\examples\{_schema}\example_1.yml', 'r', encoding='utf8') as file:
    workflow = yaml.load(file, Loader=yaml.CLoader)
    pprint(workflow)

# Convert workflow yaml data to json
workflow = json.loads(json.dumps(workflow))

# Validate
try:
    validate(instance=workflow, schema=schema)
    print("GitHub Workflow file is valid.")
except ValidationError as e:
    print("Validation Error:", e.message)

GitHub Worfklow - example_1.yml

YAML Files being tested

name: Example_1
# The below line is throwing pyyaml off. This comment does not exist in the actual file
on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Set up Python 3.8
        uses: actions/setup-python@v1
        with:
          python-version: 3.8
      - name: Install dependencies
        run: |
          make install

Error log

{True: ['push'],
 'jobs': {'build': {'runs-on': 'ubuntu-latest',
                    'steps': [{'uses': 'actions/checkout@v2'},
                              {'name': 'Set up Python 3.8',
                               'uses': 'actions/setup-python@v1',
                               'with': {'python-version': 3.8}},
                              {'name': 'Install dependencies',
                               'run': 'make install\n'},
                              {'name': 'Lint with pylint',
                               'run': 'make lint\n'},
                              {'name': 'Test with pytest',
                               'run': 'make test\n'},
                              {'name': 'Format code',
                               'run': 'make format\n'}]}}}
Validation Error: Additional properties are not allowed ('true' was unexpected)

Edit(s)

  • I just noticed that commenting the on key throws Validation Error: 'on' is a required property.

References

  1. JSONSchema for GitHub Workflow - https://json.schemastore.org/github-workflow
  2. https://github.com/python-jsonschema/jsonschema

0 Answers0