diff --git a/.devfile/Dockerfile b/.devfile/Dockerfile index 8f468f24e..bd49a2f0f 100644 --- a/.devfile/Dockerfile +++ b/.devfile/Dockerfile @@ -3,7 +3,8 @@ ENV GOROOT /usr/lib/go RUN apk add --no-cache --update curl bash jq go git openssh \ && pip3 install yq \ -&& pip3 install jsonschema-cli +&& pip3 install pyyaml \ +&& pip3 install jsonschema RUN mkdir -p /home/user/go && chmod -R a+w /home/user ENV HOME /home/user diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e9a651966..453797dd2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,10 +17,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - name: Setup Go environment - uses: actions/setup-go@v2.1.3 + uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: # The Go version to download (if necessary) and use. Supports semver spec and ranges. go-version: 1.18 @@ -42,7 +42,7 @@ jobs: run: go test -coverprofile cover.out -v ./... - name: Upload coverage to Codecov - uses: codecov/codecov-action@v2.1.0 + uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 - name: Check typescript model generation run: bash ./build/typescript-model/generate.sh diff --git a/.github/workflows/codecov.yaml b/.github/workflows/codecov.yaml index eb9d82019..97910c52b 100644 --- a/.github/workflows/codecov.yaml +++ b/.github/workflows/codecov.yaml @@ -14,14 +14,14 @@ jobs: runs-on: ubuntu-20.04 steps: - name: Checkout - uses: actions/checkout@v2.3.1 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: persist-credentials: false - name: Set up Go 1.x - uses: actions/setup-go@v2 + uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: go-version: 1.17 - name: Run tests run: go test ./... -coverprofile cover.out - name: Codecov - uses: codecov/codecov-action@v2.1.0 + uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4 \ No newline at end of file diff --git a/.github/workflows/publish-devfile-schema.yaml b/.github/workflows/publish-devfile-schema.yaml index b2f52e900..4faf40617 100644 --- a/.github/workflows/publish-devfile-schema.yaml +++ b/.github/workflows/publish-devfile-schema.yaml @@ -20,14 +20,14 @@ jobs: python-version: '3.9' - name: Checkout devfile/devfile-web - uses: actions/checkout@v2 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: repository: devfile/devfile-web persist-credentials: false path: devfile-web-repo - name: Checkout devfile api - uses: actions/checkout@v2 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: path: api-repo diff --git a/.github/workflows/release-devfile-schema.yaml b/.github/workflows/release-devfile-schema.yaml index 102a64243..19eddbd77 100644 --- a/.github/workflows/release-devfile-schema.yaml +++ b/.github/workflows/release-devfile-schema.yaml @@ -20,14 +20,14 @@ jobs: python-version: '3.9' - name: Checkout devfile/devfile-web - uses: actions/checkout@v2 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: repository: devfile/devfile-web persist-credentials: false path: devfile-web-repo - name: Checkout devfile/api - uses: actions/checkout@v2 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: path: api-repo diff --git a/.github/workflows/release-typescript-models.yaml b/.github/workflows/release-typescript-models.yaml index 2831971ba..686e688bc 100644 --- a/.github/workflows/release-typescript-models.yaml +++ b/.github/workflows/release-typescript-models.yaml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout devfile/api - uses: actions/checkout@v2 + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 with: path: api diff --git a/validate-samples.sh b/validate-samples.sh index b5539018a..d4ea4649c 100755 --- a/validate-samples.sh +++ b/validate-samples.sh @@ -32,7 +32,7 @@ then exit 1 fi -if ! command -v jsonschema-cli &> /dev/null +if ! command -v jsonschema &> /dev/null then echo echo "#### ERROR ####" @@ -58,7 +58,8 @@ do echo "Validating $schema files against ${schemaPath}" for devfile in $devfiles do - if ! jsonschema-cli validate "${BASE_DIR}/${schemaPath}" "${BASE_DIR}/${devfile}" >> validate-output.txt + python3 validate_yaml.py "${BASE_DIR}/${schemaPath}" "${BASE_DIR}/${devfile}" >> validate-output.txt + if [ "$(cat validate-output.txt)" != "" ] then echo " - $devfile => INVALID" else diff --git a/validate_yaml.py b/validate_yaml.py new file mode 100644 index 000000000..d9b6fb14a --- /dev/null +++ b/validate_yaml.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +import json +import yaml +import sys + +from typing import Any +from jsonschema import validate, ValidationError + + +class ParseSchemaError(Exception): + pass + + +class YamlValidationError(Exception): + pass + + +class OpenFileError(Exception): + pass + + +class NotEnoughArgsError(Exception): + pass + + +class YamlValidator: + """ + Yaml validator validates a given yaml file against + a chosen template. + """ + def __init__(self, schema_path: str) -> None: + self.schema = self._parse_json_file(schema_path) + + def _open_file(self, path: str) -> Any: + try: + return open(path) + except OSError as exc: + raise OpenFileError(f"::error:: failed to open file {path}: {exc}") + + def _parse_json_file(self, json_path: str) -> dict[str, Any]: + return json.load(self._open_file(json_path)) + + def _get_yaml_file(self, yaml_path: str): + return yaml.load(self._open_file(yaml_path), Loader=yaml.SafeLoader) + + def validate(self, path: str) -> bool: + try: + _ = validate(instance=self._get_yaml_file(path), schema=self.schema) + except ValidationError as exc: + raise YamlValidationError(f"error:: validation failed: {str(exc.message)}") + return True + + +def parse_arg(index: int) -> str: + try: + return sys.argv[index] + except IndexError: + raise NotEnoughArgsError( + "Missing Args: Example usage -> validate-yaml.py " + ) + + +if __name__ == "__main__": + schema_path = parse_arg(1) + yaml_path = parse_arg(2) + validator = YamlValidator(schema_path=schema_path) + validator.validate(yaml_path) \ No newline at end of file